You are here

public function VarbaseMediaRemoteVideo::appliesTo in Varbase Media 9.0.x

Same name and namespace in other branches
  1. 8.7 src/Plugin/media/Source/VarbaseMediaRemoteVideo.php \Drupal\varbase_media\Plugin\media\Source\VarbaseMediaRemoteVideo::appliesTo()
  2. 8.6 src/Plugin/media/Source/VarbaseMediaRemoteVideo.php \Drupal\varbase_media\Plugin\media\Source\VarbaseMediaRemoteVideo::appliesTo()

Checks if this media type can handle a given input value.

Parameters

mixed $value: The input value.

\Drupal\media\MediaTypeInterface $bundle: The media bundle that is using this plugin.

Return value

bool TRUE if the input can be handled by this plugin, FALSE otherwise.

Overrides InputMatchInterface::appliesTo

File

src/Plugin/media/Source/VarbaseMediaRemoteVideo.php, line 18

Class

VarbaseMediaRemoteVideo
Input-matching version of the Varbase Media Remote Video media source.

Namespace

Drupal\varbase_media\Plugin\media\Source

Code

public function appliesTo($value, MediaTypeInterface $bundle) {
  $url = $this
    ->toString($value);
  $constraint = new OEmbedResourceConstraint();

  // Ensure that the URL matches a provider.
  try {
    $provider = $this->urlResolver
      ->getProviderByUrl($url);
  } catch (ResourceException $e) {
    $this
      ->handleException($e, $constraint->unknownProviderMessage);
    return FALSE;
  } catch (ProviderException $e) {
    $this
      ->handleException($e, $constraint->providerErrorMessage);
    return FALSE;
  }

  // Ensure that the provider is allowed.
  if (!in_array($provider
    ->getName(), $this
    ->getProviders(), TRUE)) {
    return FALSE;
  }
  try {
    $endpoints = $provider
      ->getEndpoints();
    $resource_url = reset($endpoints)
      ->buildResourceUrl($url);
    $this->resourceFetcher
      ->fetchResource($resource_url);
    return TRUE;
  } catch (ResourceException $e) {
    $this
      ->handleException($e, $constraint->invalidResourceMessage);
  }
  return FALSE;
}