Chris@17: httpClient = $http_client; Chris@17: $this->providersUrl = $config_factory->get('media.settings')->get('oembed_providers_url'); Chris@17: $this->time = $time; Chris@17: $this->cacheBackend = $cache_backend; Chris@17: $this->maxAge = (int) $max_age; Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function getAll() { Chris@17: $cache_id = 'media:oembed_providers'; Chris@17: Chris@17: $cached = $this->cacheGet($cache_id); Chris@17: if ($cached) { Chris@17: return $cached->data; Chris@17: } Chris@17: Chris@17: try { Chris@17: $response = $this->httpClient->request('GET', $this->providersUrl); Chris@17: } Chris@17: catch (RequestException $e) { Chris@17: throw new ProviderException("Could not retrieve the oEmbed provider database from $this->providersUrl", NULL, $e); Chris@17: } Chris@17: Chris@17: $providers = Json::decode((string) $response->getBody()); Chris@17: Chris@17: if (!is_array($providers) || empty($providers)) { Chris@17: throw new ProviderException('Remote oEmbed providers database returned invalid or empty list.'); Chris@17: } Chris@17: Chris@17: $keyed_providers = []; Chris@17: foreach ($providers as $provider) { Chris@17: try { Chris@17: $name = (string) $provider['provider_name']; Chris@17: $keyed_providers[$name] = new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints']); Chris@17: } Chris@17: catch (ProviderException $e) { Chris@17: // Just skip all the invalid providers. Chris@17: // @todo Log the exception message to help with debugging. Chris@17: } Chris@17: } Chris@17: Chris@17: $this->cacheSet($cache_id, $keyed_providers, $this->time->getCurrentTime() + $this->maxAge); Chris@17: return $keyed_providers; Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public function get($provider_name) { Chris@17: $providers = $this->getAll(); Chris@17: Chris@17: if (!isset($providers[$provider_name])) { Chris@17: throw new \InvalidArgumentException("Unknown provider '$provider_name'"); Chris@17: } Chris@17: return $providers[$provider_name]; Chris@17: } Chris@17: Chris@17: }