comparison core/modules/media/src/OEmbed/ProviderRepository.php @ 17:129ea1e6d783

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