You are here

protected function ResourceFetcher::createResource in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::createResource()
  2. 9 core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::createResource()

Creates a Resource object from raw resource data.

Parameters

array $data: The resource data returned by the provider.

string $url: The URL of the resource.

Return value

\Drupal\media\OEmbed\Resource A value object representing the resource.

Throws

\Drupal\media\OEmbed\ResourceException If the resource cannot be created.

1 call to ResourceFetcher::createResource()
ResourceFetcher::fetchResource in core/modules/media/src/OEmbed/ResourceFetcher.php
Fetches an oEmbed resource.

File

core/modules/media/src/OEmbed/ResourceFetcher.php, line 110

Class

ResourceFetcher
Fetches and caches oEmbed resources.

Namespace

Drupal\media\OEmbed

Code

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

  // Prepare the arguments to pass to the factory method.
  $provider = $data['provider_name'] ? $this->providers
    ->get($data['provider_name']) : NULL;

  // The Resource object will validate the data we create it with and throw an
  // exception if anything looks wrong. For better debugging, catch those
  // exceptions and wrap them in a more specific and useful exception.
  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);
  }
}