X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmedia%2Fsrc%2FOEmbed%2FProviderRepository.php;fp=web%2Fcore%2Fmodules%2Fmedia%2Fsrc%2FOEmbed%2FProviderRepository.php;h=dada7fb2553e26f000709fc524257be6a846cc16;hp=0000000000000000000000000000000000000000;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/media/src/OEmbed/ProviderRepository.php b/web/core/modules/media/src/OEmbed/ProviderRepository.php new file mode 100644 index 000000000..dada7fb25 --- /dev/null +++ b/web/core/modules/media/src/OEmbed/ProviderRepository.php @@ -0,0 +1,122 @@ +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]; + } + +}