diff --git a/core/modules/media/media.services.yml b/core/modules/media/media.services.yml index c8fb03c45409fd7127d74841243f9105c4d56fdb..0f3aeb6822e8c83fe871d4852b888e15651eeb51 100644 --- a/core/modules/media/media.services.yml +++ b/core/modules/media/media.services.yml @@ -1,3 +1,6 @@ +parameters: + media.resource_fetcher_timeout: 5 + services: _defaults: autoconfigure: true @@ -14,7 +17,7 @@ services: Drupal\media\OEmbed\ProviderRepositoryInterface: '@media.oembed.provider_repository' media.oembed.resource_fetcher: class: Drupal\media\OEmbed\ResourceFetcher - arguments: ['@http_client', '@media.oembed.provider_repository', '@cache.default'] + arguments: ['@http_client', '@media.oembed.provider_repository', '@cache.default', '%media.resource_fetcher_timeout%'] Drupal\media\OEmbed\ResourceFetcherInterface: '@media.oembed.resource_fetcher' media.oembed.iframe_url_helper: class: Drupal\media\IFrameUrlHelper diff --git a/core/modules/media/src/OEmbed/ResourceFetcher.php b/core/modules/media/src/OEmbed/ResourceFetcher.php index 76c2d8b3b7f11da3b77632dc54c842a13410f67a..4b482cbbc24ea3f41cf288d9af15e4d0e5d214d4 100644 --- a/core/modules/media/src/OEmbed/ResourceFetcher.php +++ b/core/modules/media/src/OEmbed/ResourceFetcher.php @@ -15,41 +15,25 @@ */ class ResourceFetcher implements ResourceFetcherInterface { - /** - * The HTTP client. - * - * @var \GuzzleHttp\Client - */ - protected $httpClient; - - /** - * The oEmbed provider repository service. - * - * @var \Drupal\media\OEmbed\ProviderRepositoryInterface - */ - protected $providers; - - /** - * The cache backend. - * - * @var \Drupal\Core\Cache\CacheBackendInterface - */ - protected $cacheBackend; - /** * Constructs a ResourceFetcher object. * - * @param \GuzzleHttp\ClientInterface $http_client + * @param \GuzzleHttp\ClientInterface $httpClient * The HTTP client. * @param \Drupal\media\OEmbed\ProviderRepositoryInterface $providers * The oEmbed provider repository service. - * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend + * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend * The cache backend. + * @param int $timeout + * The length of time to wait for the request before the request + * should time out. */ - public function __construct(ClientInterface $http_client, ProviderRepositoryInterface $providers, CacheBackendInterface $cache_backend) { - $this->httpClient = $http_client; - $this->providers = $providers; - $this->cacheBackend = $cache_backend; + public function __construct( + protected ClientInterface $httpClient, + protected ProviderRepositoryInterface $providers, + protected CacheBackendInterface $cacheBackend, + protected int $timeout = 5, + ) { } /** @@ -65,7 +49,7 @@ public function fetchResource($url) { try { $response = $this->httpClient->request('GET', $url, [ - RequestOptions::TIMEOUT => 5, + RequestOptions::TIMEOUT => $this->timeout, ]); } catch (ClientExceptionInterface $e) { diff --git a/core/modules/media/tests/src/Unit/ResourceFetcherTest.php b/core/modules/media/tests/src/Unit/ResourceFetcherTest.php index af8123ab3d1001e9079150f96b37c843f93ced05..e05aaa06b55d982f7d657fb3c1ef7855b537d222 100644 --- a/core/modules/media/tests/src/Unit/ResourceFetcherTest.php +++ b/core/modules/media/tests/src/Unit/ResourceFetcherTest.php @@ -37,15 +37,17 @@ public function testFetchTimeout(): void { ]); $response = new Response(200, $headers, $body); + $non_default_timeout = 10; $client = $this->prophesize(Client::class); - $client->request('GET', $url, [RequestOptions::TIMEOUT => 5]) + $client->request('GET', $url, [RequestOptions::TIMEOUT => $non_default_timeout]) ->shouldBeCalled() ->willReturn($response); $fetcher = new ResourceFetcher( $client->reveal(), $this->createMock('\Drupal\media\OEmbed\ProviderRepositoryInterface'), - new NullBackend('default') + new NullBackend('default'), + $non_default_timeout ); $fetcher->fetchResource($url); }