You are here

class InstagramEmbedFetcher in Media entity Instagram 8

Same name and namespace in other branches
  1. 8.2 src/InstagramEmbedFetcher.php \Drupal\media_entity_instagram\InstagramEmbedFetcher

Fetches instagram post via oembed.

Fetches (and caches) instagram post data from free to use Instagram's oEmbed call.

Hierarchy

Expanded class hierarchy of InstagramEmbedFetcher

2 files declare their use of InstagramEmbedFetcher
Instagram.php in src/Plugin/MediaEntity/Type/Instagram.php
InstagramEmbedFormatter.php in src/Plugin/Field/FieldFormatter/InstagramEmbedFormatter.php
1 string reference to 'InstagramEmbedFetcher'
media_entity_instagram.services.yml in ./media_entity_instagram.services.yml
media_entity_instagram.services.yml
1 service uses InstagramEmbedFetcher
media_entity_instagram.instagram_embed_fetcher in ./media_entity_instagram.services.yml
\Drupal\media_entity_instagram\InstagramEmbedFetcher

File

src/InstagramEmbedFetcher.php, line 19

Namespace

Drupal\media_entity_instagram
View source
class InstagramEmbedFetcher implements InstagramEmbedFetcherInterface {
  const INSTAGRAM_URL = 'http://instagr.am/p/';
  const INSTAGRAM_API = 'http://api.instagram.com/oembed';

  /**
   * The optional cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * Guzzle client.
   *
   * @var \GuzzleHttp\Client
   */
  protected $httpClient;

  /**
   * Logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $loggerFactory;

  /**
   * InstagramEmbedFetcher constructor.
   *
   * @param \GuzzleHttp\Client $client
   *   A HTTP Client.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
   *   A logger factory.
   * @param \Drupal\Core\Cache\CacheBackendInterface|null $cache
   *   (optional) A cache bin for storing fetched instagram posts.
   */
  public function __construct(Client $client, LoggerChannelFactoryInterface $loggerFactory, CacheBackendInterface $cache = NULL) {
    $this->httpClient = $client;
    $this->loggerFactory = $loggerFactory;
    $this->cache = $cache;
  }

  /**
   * {@inheritdoc}
   */
  public function fetchInstagramEmbed($shortcode, $hidecaption = FALSE, $maxWidth = NULL) {
    $options = [
      'url' => self::INSTAGRAM_URL . $shortcode . '/',
      'hidecaption' => (int) $hidecaption,
      'omitscript' => 1,
    ];
    if ($maxWidth) {
      $options['maxwidth'] = $maxWidth;
    }

    // Tweets don't change much, so pull it out of the cache (if we have one)
    // if this one has already been fetched.
    $cacheKey = md5(serialize($options));
    if ($this->cache && ($cached_instagram_post = $this->cache
      ->get($cacheKey))) {
      return $cached_instagram_post->data;
    }
    $queryParameter = UrlHelper::buildQuery($options);
    try {
      $response = $this->httpClient
        ->request('GET', self::INSTAGRAM_API . '?' . $queryParameter, [
        'timeout' => 5,
      ]);
      if ($response
        ->getStatusCode() === 200) {
        $data = Json::decode($response
          ->getBody()
          ->getContents());
      }
    } catch (RequestException $e) {
      $this->loggerFactory
        ->get('media_entity_instagram')
        ->error("Could not retrieve Instagram post {$shortcode}.", Error::decodeException($e));
    }

    // If we got data from Instagram oEmbed request, return data.
    if (isset($data)) {

      // If we have a cache, store the response for future use.
      if ($this->cache) {

        // Instagram posts don't change often, so the response should expire
        // from the cache on its own in 90 days.
        $this->cache
          ->set($cacheKey, $data, time() + 86400 * 90);
      }
      return $data;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InstagramEmbedFetcher::$cache protected property The optional cache backend.
InstagramEmbedFetcher::$httpClient protected property Guzzle client.
InstagramEmbedFetcher::$loggerFactory protected property Logger.
InstagramEmbedFetcher::fetchInstagramEmbed public function Retrieves a instagram post by its shortcode. Overrides InstagramEmbedFetcherInterface::fetchInstagramEmbed
InstagramEmbedFetcher::INSTAGRAM_API constant
InstagramEmbedFetcher::INSTAGRAM_URL constant
InstagramEmbedFetcher::__construct public function InstagramEmbedFetcher constructor.