You are here

public function ResourceFetcher::fetchResource in Media Migration 8

Fetches an oEmbed resource.

Parameters

string $url: Endpoint-specific URL of the oEmbed resource.

Return value

\Drupal\media\OEmbed\Resource A resource object built from the oEmbed resource data.

Throws

\Drupal\media\OEmbed\ResourceException If the oEmbed endpoint is not reachable or the response returns an unexpected Content-Type header.

Overrides ResourceFetcher::fetchResource

See also

https://oembed.com/#section2

File

tests/modules/media_migration_test_oembed/src/ResourceFetcher.php, line 18

Class

ResourceFetcher
Simple oEmbed resource fetcher replacement for remote media migration tests.

Namespace

Drupal\media_migration_test_oembed

Code

public function fetchResource($oembed_url) {
  $hashed = Crypt::hashBase64($oembed_url);
  $resource_array = \Drupal::state()
    ->get("media_migration_test_oembed.{$hashed}", [
    'type' => 'video',
    'html' => urlencode($oembed_url),
    'width' => 320,
    'height' => 180,
  ]);
  $type = $resource_array['type'];
  unset($resource_array['type']);
  try {
    $resource = call_user_func_array([
      Resource::class,
      $type,
    ], $resource_array);
  } catch (\Exception $e) {
    throw new ResourceException(sprintf('Test media oembed resource %s cannot be fetched', $oembed_url), $oembed_url);
  }
  assert($resource instanceof Resource);
  return $resource;
}