View source
<?php
namespace Drupal\media\OEmbed;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\CacheBackendInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\RequestOptions;
class ResourceFetcher implements ResourceFetcherInterface {
protected $httpClient;
protected $providers;
protected $cacheBackend;
public function __construct(ClientInterface $http_client, ProviderRepositoryInterface $providers, CacheBackendInterface $cache_backend) {
$this->httpClient = $http_client;
$this->providers = $providers;
$this->cacheBackend = $cache_backend;
}
public function fetchResource($url) {
$cache_id = "media:oembed_resource:{$url}";
$cached = $this->cacheBackend
->get($cache_id);
if ($cached) {
return $this
->createResource($cached->data, $url);
}
try {
$response = $this->httpClient
->request('GET', $url, [
RequestOptions::TIMEOUT => 5,
]);
} catch (TransferException $e) {
throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
}
[
$format,
] = $response
->getHeader('Content-Type');
$content = (string) $response
->getBody();
if (strstr($format, 'text/xml') || strstr($format, 'application/xml')) {
$data = $this
->parseResourceXml($content, $url);
}
else {
$data = Json::decode($content);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new ResourceException('Error decoding oEmbed resource: ' . json_last_error_msg(), $url);
}
}
if (empty($data) || !is_array($data)) {
throw new ResourceException('The oEmbed resource could not be decoded.', $url);
}
$this->cacheBackend
->set($cache_id, $data);
return $this
->createResource($data, $url);
}
protected function createResource(array $data, $url) {
$data += [
'title' => NULL,
'author_name' => NULL,
'author_url' => NULL,
'provider_name' => NULL,
'cache_age' => NULL,
'thumbnail_url' => NULL,
'thumbnail_width' => NULL,
'thumbnail_height' => NULL,
'width' => NULL,
'height' => NULL,
'url' => NULL,
'html' => NULL,
'version' => NULL,
];
if ($data['version'] !== '1.0') {
throw new ResourceException("Resource version must be '1.0'", $url, $data);
}
$provider = $data['provider_name'] ? $this->providers
->get($data['provider_name']) : NULL;
try {
switch ($data['type']) {
case Resource::TYPE_LINK:
return Resource::link($data['url'], $provider, $data['title'], $data['author_name'], $data['author_url'], $data['cache_age'], $data['thumbnail_url'], $data['thumbnail_width'], $data['thumbnail_height']);
case Resource::TYPE_PHOTO:
return Resource::photo($data['url'], $data['width'], $data['height'], $provider, $data['title'], $data['author_name'], $data['author_url'], $data['cache_age'], $data['thumbnail_url'], $data['thumbnail_width'], $data['thumbnail_height']);
case Resource::TYPE_RICH:
return Resource::rich($data['html'], $data['width'], $data['height'], $provider, $data['title'], $data['author_name'], $data['author_url'], $data['cache_age'], $data['thumbnail_url'], $data['thumbnail_width'], $data['thumbnail_height']);
case Resource::TYPE_VIDEO:
return Resource::video($data['html'], $data['width'], $data['height'], $provider, $data['title'], $data['author_name'], $data['author_url'], $data['cache_age'], $data['thumbnail_url'], $data['thumbnail_width'], $data['thumbnail_height']);
default:
throw new ResourceException('Unknown resource type: ' . $data['type'], $url, $data);
}
} catch (\InvalidArgumentException $e) {
throw new ResourceException($e
->getMessage(), $url, $data, $e);
}
}
protected function parseResourceXml($data, $url) {
$was_using_internal_errors = libxml_use_internal_errors(TRUE);
libxml_clear_errors();
$content = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
libxml_use_internal_errors($was_using_internal_errors);
$error = libxml_get_last_error();
if ($error) {
libxml_clear_errors();
throw new ResourceException($error->message, $url);
}
elseif ($content === FALSE) {
throw new ResourceException('The fetched resource could not be parsed.', $url);
}
$data = Json::encode($content);
return Json::decode($data);
}
}