function ProviderRepository::getAll

Same name in this branch
  1. 11.x core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php \Drupal\media_test_oembed\ProviderRepository::getAll()
Same name in other branches
  1. 9 core/modules/media/src/OEmbed/ProviderRepository.php \Drupal\media\OEmbed\ProviderRepository::getAll()
  2. 9 core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php \Drupal\media_test_oembed\ProviderRepository::getAll()
  3. 8.9.x core/modules/media/src/OEmbed/ProviderRepository.php \Drupal\media\OEmbed\ProviderRepository::getAll()
  4. 8.9.x core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php \Drupal\media_test_oembed\ProviderRepository::getAll()
  5. 10 core/modules/media/src/OEmbed/ProviderRepository.php \Drupal\media\OEmbed\ProviderRepository::getAll()
  6. 10 core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php \Drupal\media_test_oembed\ProviderRepository::getAll()
2 calls to ProviderRepository::getAll()
ProviderRepository::get in core/modules/media/src/OEmbed/ProviderRepository.php
ProviderRepository::getAll in core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php
1 method overrides ProviderRepository::getAll()
ProviderRepository::getAll in core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php

File

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

Class

ProviderRepository
Retrieves and caches information about oEmbed providers.

Namespace

Drupal\media\OEmbed

Code

public function getAll() {
    $current_time = $this->time
        ->getCurrentTime();
    $stored = $this->keyValue
        ->get('oembed_providers');
    // If we have stored data that hasn't yet expired, return that. We need to
    // store the data in a key-value store because, if the remote provider
    // database is unavailable, we'd rather return stale data than throw an
    // exception. This means we cannot use a normal cache backend or expirable
    // key-value store, since those could delete the stale data at any time.
    if ($stored && $stored['expires'] > $current_time) {
        return $stored['data'];
    }
    try {
        $response = $this->httpClient
            ->request('GET', $this->providersUrl);
    } catch (ClientExceptionInterface $e) {
        if (isset($stored['data'])) {
            // Use the stale data to fall back gracefully, but warn site
            // administrators that we used stale data.
            $this->logger
                ->warning('Remote oEmbed providers could not be retrieved due to error: @error. Using previously stored data. This may contain out of date information.', [
                '@error' => $e->getMessage(),
            ]);
            return $stored['data'];
        }
        // We have no previous data and the request failed.
        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)) {
        if (isset($stored['data'])) {
            // Use the stale data to fall back gracefully, but as above, warn site
            // administrators that we used stale data.
            $this->logger
                ->warning('Remote oEmbed providers database returned invalid or empty list. Using previously stored data. This may contain out of date information.');
            return $stored['data'];
        }
        // We have no previous data and the current data is corrupt.
        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) {
            // Skip invalid providers, but log the exception message to help with
            // debugging.
            $this->logger
                ->warning($e->getMessage());
        }
    }
    $this->keyValue
        ->set('oembed_providers', [
        'data' => $keyed_providers,
        'expires' => $current_time + $this->maxAge,
    ]);
    return $keyed_providers;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.