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]; } }