trait InstagramApiTrait in Instagram Feeds 8
Instagram API Trait.
Hierarchy
- trait \Drupal\instagram_feeds\InstagramApiTrait
1 file declares its use of InstagramApiTrait
- InstagramPostsObtainedSubscriber.php in src/
EventSubscriber/ InstagramPostsObtainedSubscriber.php
File
- src/
InstagramApiTrait.php, line 14
Namespace
Drupal\instagram_feedsView source
trait InstagramApiTrait {
/**
* Guzzle HTTP Client instance.
*
* @var \GuzzleHttp\Client
*/
private $httpClient;
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
private $token;
/**
* The Instagram Feeds Logger channel.
*
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* Sends request to Instagram and validates response status code.
*
* @param string $url
* Url to get contents for.
* @param bool $is_json_expected
* Set true, in order to decode JSON data from response.
*
* @return string|array
* Response body as string if $is_json_expected is false, or JSON decoded body.
*
* @throws \Exception
*/
protected function getInstagramResponceContents($url, $is_json_expected = FALSE) {
try {
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $this
->httpClient()
->get($url);
if ($response
->getStatusCode() !== 200) {
throw new \Exception(t('Invalid responce code @code from Instagram.', [
'@code' => $response
->getStatusCode(),
]));
}
$body = $response
->getBody()
->getContents();
return $is_json_expected ? Json::decode($body) : $body;
} catch (\Exception $e) {
$this
->logger()
->error($e
->getMessage());
}
return '';
}
/**
* Returns a channel logger object.
*
* @return \Psr\Log\LoggerInterface
* The logger for instagram_feeds channel.
*/
protected function logger() : LoggerInterface {
if (!isset($this->logger)) {
$this->logger = \Drupal::logger('instagram_feeds');
}
return $this->logger;
}
/**
* Returns the Guzzle HTTP Client.
*
* @return \GuzzleHttp\Client
*/
protected function httpClient() : Client {
if (!isset($this->httpClient)) {
$this->httpClient = \Drupal::httpClient();
}
return $this->httpClient;
}
/**
* Returns the token service.
*
* @return \Drupal\Core\Utility\Token
* The token service.
*/
protected function token() : Token {
if (!isset($this->token)) {
$this->token = \Drupal::token();
}
return $this->token;
}
/**
* Sets logger for instagram_feeds channel.
*
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The Logger factory service.
*
* @return $this
*/
protected function setLogger(LoggerChannelFactoryInterface $logger_factory) {
$this->logger = $logger_factory
->get('instagram_feeds');
return $this;
}
/**
* Sets Guzzle HTTP client.
*
* @param \GuzzleHttp\Client $client
* Guzzle HTTP client.
*
* @return $this
*/
protected function setHttpClient(Client $client) {
$this->httpClient = $client;
return $this;
}
/**
* Sets the token service.
*
* @param \Drupal\Core\Utility\Token $token
* The token service.
*
* @return $this
*/
protected function setToken(Token $token) {
$this->token = $token;
return $this;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
InstagramApiTrait:: |
private | property | Guzzle HTTP Client instance. | |
InstagramApiTrait:: |
private | property | The Instagram Feeds Logger channel. | |
InstagramApiTrait:: |
private | property | The token service. | |
InstagramApiTrait:: |
protected | function | Sends request to Instagram and validates response status code. | |
InstagramApiTrait:: |
protected | function | Returns the Guzzle HTTP Client. | |
InstagramApiTrait:: |
protected | function | Returns a channel logger object. | |
InstagramApiTrait:: |
protected | function | Sets Guzzle HTTP client. | |
InstagramApiTrait:: |
protected | function | Sets logger for instagram_feeds channel. | |
InstagramApiTrait:: |
protected | function | Sets the token service. | |
InstagramApiTrait:: |
protected | function | Returns the token service. |