annotate core/modules/media/src/OEmbed/ProviderRepository.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@17 1 <?php
Chris@17 2
Chris@17 3 namespace Drupal\media\OEmbed;
Chris@17 4
Chris@17 5 use Drupal\Component\Datetime\TimeInterface;
Chris@17 6 use Drupal\Component\Serialization\Json;
Chris@17 7 use Drupal\Core\Cache\CacheBackendInterface;
Chris@17 8 use Drupal\Core\Cache\UseCacheBackendTrait;
Chris@17 9 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@17 10 use GuzzleHttp\ClientInterface;
Chris@17 11 use GuzzleHttp\Exception\RequestException;
Chris@17 12
Chris@17 13 /**
Chris@17 14 * Retrieves and caches information about oEmbed providers.
Chris@17 15 */
Chris@17 16 class ProviderRepository implements ProviderRepositoryInterface {
Chris@17 17
Chris@17 18 use UseCacheBackendTrait;
Chris@17 19
Chris@17 20 /**
Chris@17 21 * How long the provider data should be cached, in seconds.
Chris@17 22 *
Chris@17 23 * @var int
Chris@17 24 */
Chris@17 25 protected $maxAge;
Chris@17 26
Chris@17 27 /**
Chris@17 28 * The HTTP client.
Chris@17 29 *
Chris@17 30 * @var \GuzzleHttp\Client
Chris@17 31 */
Chris@17 32 protected $httpClient;
Chris@17 33
Chris@17 34 /**
Chris@17 35 * URL of a JSON document which contains a database of oEmbed providers.
Chris@17 36 *
Chris@17 37 * @var string
Chris@17 38 */
Chris@17 39 protected $providersUrl;
Chris@17 40
Chris@17 41 /**
Chris@17 42 * The time service.
Chris@17 43 *
Chris@17 44 * @var \Drupal\Component\Datetime\TimeInterface
Chris@17 45 */
Chris@17 46 protected $time;
Chris@17 47
Chris@17 48 /**
Chris@17 49 * Constructs a ProviderRepository instance.
Chris@17 50 *
Chris@17 51 * @param \GuzzleHttp\ClientInterface $http_client
Chris@17 52 * The HTTP client.
Chris@17 53 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@17 54 * The config factory service.
Chris@17 55 * @param \Drupal\Component\Datetime\TimeInterface $time
Chris@17 56 * The time service.
Chris@17 57 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
Chris@17 58 * (optional) The cache backend.
Chris@17 59 * @param int $max_age
Chris@17 60 * (optional) How long the cache data should be kept. Defaults to a week.
Chris@17 61 */
Chris@17 62 public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, TimeInterface $time, CacheBackendInterface $cache_backend = NULL, $max_age = 604800) {
Chris@17 63 $this->httpClient = $http_client;
Chris@17 64 $this->providersUrl = $config_factory->get('media.settings')->get('oembed_providers_url');
Chris@17 65 $this->time = $time;
Chris@17 66 $this->cacheBackend = $cache_backend;
Chris@17 67 $this->maxAge = (int) $max_age;
Chris@17 68 }
Chris@17 69
Chris@17 70 /**
Chris@17 71 * {@inheritdoc}
Chris@17 72 */
Chris@17 73 public function getAll() {
Chris@17 74 $cache_id = 'media:oembed_providers';
Chris@17 75
Chris@17 76 $cached = $this->cacheGet($cache_id);
Chris@17 77 if ($cached) {
Chris@17 78 return $cached->data;
Chris@17 79 }
Chris@17 80
Chris@17 81 try {
Chris@17 82 $response = $this->httpClient->request('GET', $this->providersUrl);
Chris@17 83 }
Chris@17 84 catch (RequestException $e) {
Chris@17 85 throw new ProviderException("Could not retrieve the oEmbed provider database from $this->providersUrl", NULL, $e);
Chris@17 86 }
Chris@17 87
Chris@17 88 $providers = Json::decode((string) $response->getBody());
Chris@17 89
Chris@17 90 if (!is_array($providers) || empty($providers)) {
Chris@17 91 throw new ProviderException('Remote oEmbed providers database returned invalid or empty list.');
Chris@17 92 }
Chris@17 93
Chris@17 94 $keyed_providers = [];
Chris@17 95 foreach ($providers as $provider) {
Chris@17 96 try {
Chris@17 97 $name = (string) $provider['provider_name'];
Chris@17 98 $keyed_providers[$name] = new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints']);
Chris@17 99 }
Chris@17 100 catch (ProviderException $e) {
Chris@17 101 // Just skip all the invalid providers.
Chris@17 102 // @todo Log the exception message to help with debugging.
Chris@17 103 }
Chris@17 104 }
Chris@17 105
Chris@17 106 $this->cacheSet($cache_id, $keyed_providers, $this->time->getCurrentTime() + $this->maxAge);
Chris@17 107 return $keyed_providers;
Chris@17 108 }
Chris@17 109
Chris@17 110 /**
Chris@17 111 * {@inheritdoc}
Chris@17 112 */
Chris@17 113 public function get($provider_name) {
Chris@17 114 $providers = $this->getAll();
Chris@17 115
Chris@17 116 if (!isset($providers[$provider_name])) {
Chris@17 117 throw new \InvalidArgumentException("Unknown provider '$provider_name'");
Chris@17 118 }
Chris@17 119 return $providers[$provider_name];
Chris@17 120 }
Chris@17 121
Chris@17 122 }