You are here

class FacebookFetcher in Media entity facebook 8

Same name and namespace in other branches
  1. 8.2 src/FacebookFetcher.php \Drupal\media_entity_facebook\FacebookFetcher
  2. 3.x src/FacebookFetcher.php \Drupal\media_entity_facebook\FacebookFetcher

Class FacebookFetcher.

Hierarchy

Expanded class hierarchy of FacebookFetcher

1 file declares its use of FacebookFetcher
Facebook.php in src/Plugin/MediaEntity/Type/Facebook.php
1 string reference to 'FacebookFetcher'
media_entity_facebook.services.yml in ./media_entity_facebook.services.yml
media_entity_facebook.services.yml
1 service uses FacebookFetcher
media_entity_facebook.facebook_fetcher in ./media_entity_facebook.services.yml
\Drupal\media_entity_facebook\FacebookFetcher

File

src/FacebookFetcher.php, line 12

Namespace

Drupal\media_entity_facebook
View source
class FacebookFetcher {

  /**
   * Stores logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannel
   */
  protected $loggerChannel;

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

  /**
   * Tracks when an error has occurred when interacting with the API.
   *
   * @var bool
   */
  protected $apiErrorEncountered = FALSE;

  /**
   * FacebookFetcher constructor.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_channel_factory
   *   The logger factory.
   * @param \GuzzleHttp\ClientInterface $client
   *   The Guzzle HTTP client.
   */
  public function __construct(LoggerChannelFactoryInterface $logger_channel_factory, ClientInterface $client) {
    $this->loggerChannel = $logger_channel_factory
      ->get('media_entity_facebook');
    $this->httpClient = $client;
  }

  /**
   * Fetch and return response from Facebook's oEmbed API endpoint.
   *
   * @param string $resource_url
   *   The URL to pass to Facebook's oembed API.
   */
  public function getOembedData($resource_url) {

    // Keep an in-memory cache of the response data for each URL, since this
    // data may be requested multiple times on the same request.
    static $memory_cache;
    if (!isset($memory_cache)) {
      $memory_cache = [];
    }
    if (!isset($memory_cache[$resource_url])) {

      // If there was an error interacting with the Facebook API, like a network
      // timeout due to Facebook being down, we don't want to clog up the Drupal
      // site's resources by making lots of API requests that may all timeout.
      // To do this, we mark when a request exception occurred and back out of
      // subsequent requests if so.
      if ($this->apiErrorEncountered) {
        $this->loggerChannel
          ->error('Aborting Facebook API request due to previously encountered error.');
        return FALSE;
      }
      $endpoint = $this
        ->getApiEndpointUrl($resource_url) . '?url=' . $resource_url;
      try {
        $response = $this->httpClient
          ->request('GET', $endpoint, [
          'timeout' => 5,
        ]);
        $decoded = json_decode((string) $response
          ->getBody(), TRUE);
        $memory_cache[$resource_url] = $decoded;
      } catch (TransferException $e) {
        \Drupal::logger('media_entity_facebook')
          ->error('Error retrieving oEmbed data for a Facebook media entity: @error', [
          '@error' => $e
            ->getMessage(),
        ]);
        $this->apiErrorEncountered = TRUE;
        return FALSE;
      }
    }
    return $memory_cache[$resource_url];
  }

  /**
   * Return the appropriate Facebook oEmbed API endpoint for the content URL.
   *
   * @param string $content_url
   *   The content URL contains the URL to the resource.
   *
   * @return string
   *   The oEmbed endpoint URL.
   */
  protected function getApiEndpointUrl($content_url) {
    if (preg_match('/\\/videos\\//', $content_url) || preg_match('/\\/video.php\\//', $content_url)) {
      return 'https://www.facebook.com/plugins/video/oembed.json/';
    }
    else {
      return 'https://www.facebook.com/plugins/post/oembed.json/';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacebookFetcher::$apiErrorEncountered protected property Tracks when an error has occurred when interacting with the API.
FacebookFetcher::$httpClient protected property Guzzle HTTP client.
FacebookFetcher::$loggerChannel protected property Stores logger.
FacebookFetcher::getApiEndpointUrl protected function Return the appropriate Facebook oEmbed API endpoint for the content URL.
FacebookFetcher::getOembedData public function Fetch and return response from Facebook's oEmbed API endpoint.
FacebookFetcher::__construct public function FacebookFetcher constructor.