You are here

class ProviderRepository in Drupal 8

Same name in this branch
  1. 8 core/modules/media/src/OEmbed/ProviderRepository.php \Drupal\media\OEmbed\ProviderRepository
  2. 8 core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php \Drupal\media_test_oembed\ProviderRepository
Same name and namespace in other branches
  1. 9 core/modules/media/src/OEmbed/ProviderRepository.php \Drupal\media\OEmbed\ProviderRepository

Retrieves and caches information about oEmbed providers.

Hierarchy

Expanded class hierarchy of ProviderRepository

1 file declares its use of ProviderRepository
ProviderRepository.php in core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php
1 string reference to 'ProviderRepository'
media.services.yml in core/modules/media/media.services.yml
core/modules/media/media.services.yml
1 service uses ProviderRepository
media.oembed.provider_repository in core/modules/media/media.services.yml
Drupal\media\OEmbed\ProviderRepository

File

core/modules/media/src/OEmbed/ProviderRepository.php, line 16

Namespace

Drupal\media\OEmbed
View source
class ProviderRepository implements ProviderRepositoryInterface {
  use UseCacheBackendTrait;

  /**
   * How long the provider data should be cached, in seconds.
   *
   * @var int
   */
  protected $maxAge;

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

  /**
   * URL of a JSON document which contains a database of oEmbed providers.
   *
   * @var string
   */
  protected $providersUrl;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a ProviderRepository instance.
   *
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The HTTP client.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   (optional) The cache backend.
   * @param int $max_age
   *   (optional) How long the cache data should be kept. Defaults to a week.
   */
  public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, TimeInterface $time, CacheBackendInterface $cache_backend = NULL, $max_age = 604800) {
    $this->httpClient = $http_client;
    $this->providersUrl = $config_factory
      ->get('media.settings')
      ->get('oembed_providers_url');
    $this->time = $time;
    $this->cacheBackend = $cache_backend;
    $this->maxAge = (int) $max_age;
  }

  /**
   * {@inheritdoc}
   */
  public function getAll() {
    $cache_id = 'media:oembed_providers';
    $cached = $this
      ->cacheGet($cache_id);
    if ($cached) {
      return $cached->data;
    }
    try {
      $response = $this->httpClient
        ->request('GET', $this->providersUrl);
    } catch (RequestException $e) {
      throw new ProviderException("Could not retrieve the oEmbed provider database from {$this->providersUrl}", NULL, $e);
    }
    $providers = Json::decode((string) $response
      ->getBody());
    if (!is_array($providers) || empty($providers)) {
      throw new ProviderException('Remote oEmbed providers database returned invalid or empty list.');
    }
    $keyed_providers = [];
    foreach ($providers as $provider) {
      try {
        $name = (string) $provider['provider_name'];
        $keyed_providers[$name] = new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints']);
      } catch (ProviderException $e) {

        // Just skip all the invalid providers.
        // @todo Log the exception message to help with debugging.
      }
    }
    $this
      ->cacheSet($cache_id, $keyed_providers, $this->time
      ->getCurrentTime() + $this->maxAge);
    return $keyed_providers;
  }

  /**
   * {@inheritdoc}
   */
  public function get($provider_name) {
    $providers = $this
      ->getAll();
    if (!isset($providers[$provider_name])) {
      throw new \InvalidArgumentException("Unknown provider '{$provider_name}'");
    }
    return $providers[$provider_name];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProviderRepository::$httpClient protected property The HTTP client.
ProviderRepository::$maxAge protected property How long the provider data should be cached, in seconds.
ProviderRepository::$providersUrl protected property URL of a JSON document which contains a database of oEmbed providers.
ProviderRepository::$time protected property The time service.
ProviderRepository::get public function Returns information for a specific oEmbed provider. Overrides ProviderRepositoryInterface::get 1
ProviderRepository::getAll public function Returns information on all available oEmbed providers. Overrides ProviderRepositoryInterface::getAll 1
ProviderRepository::__construct public function Constructs a ProviderRepository instance.
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag. 1
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.