You are here

trait InstagramApiTrait in Instagram Feeds 8

Instagram API Trait.

Hierarchy

1 file declares its use of InstagramApiTrait
InstagramPostsObtainedSubscriber.php in src/EventSubscriber/InstagramPostsObtainedSubscriber.php

File

src/InstagramApiTrait.php, line 14

Namespace

Drupal\instagram_feeds
View 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

Namesort descending Modifiers Type Description Overrides
InstagramApiTrait::$httpClient private property Guzzle HTTP Client instance.
InstagramApiTrait::$logger private property The Instagram Feeds Logger channel.
InstagramApiTrait::$token private property The token service.
InstagramApiTrait::getInstagramResponceContents protected function Sends request to Instagram and validates response status code.
InstagramApiTrait::httpClient protected function Returns the Guzzle HTTP Client.
InstagramApiTrait::logger protected function Returns a channel logger object.
InstagramApiTrait::setHttpClient protected function Sets Guzzle HTTP client.
InstagramApiTrait::setLogger protected function Sets logger for instagram_feeds channel.
InstagramApiTrait::setToken protected function Sets the token service.
InstagramApiTrait::token protected function Returns the token service.