getProviderByUrl example

throw new \LogicException('Media source must implement ' . OEmbedInterface::class);
    }
    $url = $source->getSourceFieldValue($media);
    // The URL may be NULL if the source field is empty, which is invalid input.     if (empty($url)) {
      $this->context->addViolation($constraint->invalidResourceMessage);
      return;
    }

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

    // Ensure that the provider is allowed.


    // Try to get the resource URL from the persistent cache.     $cache_id = "media:oembed_resource_url:$url:$max_width:$max_height";

    $cached = $this->cacheBackend->get($cache_id);
    if ($cached) {
      $this->urlCache[$url] = $cached->data;
      return $this->urlCache[$url];
    }

    $provider = $this->getProviderByUrl($url);

    $resource_url = $this->getEndpointMatchingUrl($url$provider);
    $parsed_url = UrlHelper::parse($resource_url);
    if ($max_width) {
      $parsed_url['query']['maxwidth'] = $max_width;
    }
    if ($max_height) {
      $parsed_url['query']['maxheight'] = $max_height;
    }
    // Let other modules alter the resource URL, because some oEmbed providers     // provide extra parameters in the query string. For example, Instagram also
'bundle' => $this->createMediaType('oembed:video')->id(),
    ]);

    $constraint = new OEmbedResourceConstraint();

    // The media item has an empty source value, so the constraint validator     // should add a violation and return early before invoking the URL resolver.     $context = $this->prophesize(ExecutionContextInterface::class);
    $context->addViolation($constraint->invalidResourceMessage)->shouldBeCalled();

    $url_resolver = $this->prophesize(UrlResolverInterface::class);
    $url_resolver->getProviderByUrl(Argument::any())->shouldNotBeCalled();

    $validator = new OEmbedResourceConstraintValidator(
      $url_resolver->reveal(),
      $this->container->get('media.oembed.resource_fetcher'),
      $this->container->get('logger.factory')
    );
    $validator->initialize($context->reveal());
    $validator->validate($this->getValue($media)$constraint);
  }

  /** * @covers ::validate */
Home | Imprint | This part of the site doesn't use cookies.