I've written a small PHP library to connect with an Instagram application. The repository is on GitHub Instagram-php. Here I am adding only main class code. I require a code review and thoughts on performance and security issues.
namespace Haridarshan\Instagram;
use Haridarshan\Instagram\Exceptions\InstagramException;
use InvalidArgumentException;
use stdClass;
class Instagram
{
/** @var InstagramApp */
protected $app;
/** @var string */
protected $callbackUrl;
/** @var array */
protected $defaultScopes = ['basic', 'public_content', 'follower_list', 'comments', 'relationships', 'likes'];
/** @var array */
protected $scopes = [];
/**
* Random string indicating the state to prevent spoofing
*
* @var string
*/
protected $state;
/** @var \GuzzleHttp\Client $client */
protected $client;
/** @var InstagramOAuth $oauthResponse */
protected $oauthResponse;
/**
* Default Constructor
* Instagram Configuration Data
*
* @param array $config
*
* @throws InstagramException|InvalidArgumentException
*/
public function __construct(array $config = [])
{
if (!is_array($config)) {
throw new InstagramException('Invalid Instagram Configuration data');
}
$this->app = new InstagramApp($config['ClientId'], $config['ClientSecret']);
$this->setCallbackUrl($config['Callback']);
$this->state = isset($config['State']) ? $config['State'] : substr(md5(rand()), 0, 7);
$this->client = HelperFactory::getInstance()->client(Constants::API_HOST);
}
/**
* Returns InstagramApp entity.
*
* @return InstagramApp
*/
public function getApp()
{
return $this->app;
}
/**
* Make URLs for user browser navigation
*
* @param array $parameters
*
* @throws InstagramException
*
* @return string
*/
public function getLoginUrl(array $parameters)
{
if (!isset($parameters['scope'])) {
throw new InstagramException('Missing or Invalid Scope permission used', 400);
}
if (count(array_diff($parameters['scope'], $this->defaultScopes)) !== 0) {
throw new InstagramException('Missing or Invalid Scope permission used', 400);
}
$this->scopes = $parameters['scope'];
$loginUrl = new LoginUrl(
$this->getApp(),
$this->getCallbackUrl(),
$this->getState(),
$this->scopes
);
return $loginUrl->loginUrl();
}
/**
* Get the Oauth Access Token of a user from callback code
*
* @param string $code - Oauth2 Code returned with callback url after successfull login
*
* @return InstagramOAuth
*/
public function oauth($code)
{
$options = [
'grant_type' => 'authorization_code',
'client_id' => $this->app->getId(),
'client_secret' => $this->app->getSecret(),
'redirect_uri' => $this->getCallbackUrl(),
'code' => $code,
'state' => $this->state,
];
$response = HelperFactory::getInstance()->request($this->client, Constants::API_TOKEN, $options, 'POST');
$this->oauthResponse = new InstagramOAuth(
json_decode($response->getBody()->getContents())
);
return $this->oauthResponse;
}
/**
* Setter: Callback Url
*
* @param string $url
*/
public function setCallbackUrl($url)
{
$this->callbackUrl = $url;
}
/**
* Getter: Callback Url
*
* @return string
*/
public function getCallbackUrl()
{
return $this->callbackUrl;
}
/**
* Get InstagramOAuth
*
* @return InstagramOAuth
*/
public function getOAuth()
{
if ($this->oauthResponse instanceof InstagramOAuth) {
return $this->oauthResponse;
}
$accessToken = new stdClass;
$accessToken->access_token = null;
$this->oauthResponse = new InstagramOAuth($accessToken);
return $this->oauthResponse;
}
/**
* Get Http Client
*
* @return Client
*/
public function getHttpClient()
{
return $this->client;
}
/**
* Set User Access Token
*
* @param string $token
*/
public function setAccessToken($token)
{
if (!$this->oauthResponse instanceof InstagramOAuth) {
$this->oauthResponse = new InstagramOAuth(json_decode(json_encode(['access_token' => $token])));
}
}
/**
* Get state value
*
* @return string|mixed
*/
public function getState()
{
return $this->state;
}
/**
* Get a string containing the version of the library.
*
* @return string
*/
public function getLibraryVersion()
{
return Constants::VERSION;
}
}