From 6fd954bb4b61fb7552e3c6b5e2f48452b9ec8bf5 Mon Sep 17 00:00:00 2001 From: Andreas Hennings Date: Tue, 12 Nov 2024 22:53:26 +0100 Subject: [PATCH] Issue #56: Convert CoolRequest into a service. --- collabora_online.services.yml | 4 ++++ src/Cool/CoolRequest.php | 2 +- src/Cool/CoolUtils.php | 4 ++-- .../ExistingSite/FetchClientUrlTestDISABLED.php | 17 +++++++++++++++-- 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 collabora_online.services.yml diff --git a/collabora_online.services.yml b/collabora_online.services.yml new file mode 100644 index 00000000..a8ff5a14 --- /dev/null +++ b/collabora_online.services.yml @@ -0,0 +1,4 @@ +services: + _defaults: + autowire: true + Drupal\collabora_online\Cool\CoolRequest: { } diff --git a/src/Cool/CoolRequest.php b/src/Cool/CoolRequest.php index 3e9a197e..d3ee13b7 100644 --- a/src/Cool/CoolRequest.php +++ b/src/Cool/CoolRequest.php @@ -85,7 +85,7 @@ function strStartsWith($s, $ss) { } /** - * Helper class to fetch a WOPI client url. + * Service to fetch a WOPI client url. */ class CoolRequest { diff --git a/src/Cool/CoolUtils.php b/src/Cool/CoolUtils.php index 97caa4eb..b7a7f109 100644 --- a/src/Cool/CoolUtils.php +++ b/src/Cool/CoolUtils.php @@ -248,9 +248,9 @@ public static function getViewerRender(Media $media, bool $can_write, $options = $wopi_base = $default_config->get('cool')['wopi_base']; $allowfullscreen = $default_config->get('cool')['allowfullscreen'] ?? FALSE; - $req = new CoolRequest(); + $discovery_xml_service = \Drupal::service(CoolRequest::class); try { - $wopi_client = $req->getWopiClientURL(); + $wopi_client = $discovery_xml_service->getWopiClientURL(); } catch (CoolRequestException $e) { return [ diff --git a/tests/src/ExistingSite/FetchClientUrlTestDISABLED.php b/tests/src/ExistingSite/FetchClientUrlTestDISABLED.php index 2bb4181f..9d97d1fd 100644 --- a/tests/src/ExistingSite/FetchClientUrlTestDISABLED.php +++ b/tests/src/ExistingSite/FetchClientUrlTestDISABLED.php @@ -16,7 +16,7 @@ class FetchClientUrlTestDISABLED extends ExistingSiteBase { * Tests fetching the client url. */ public function testFetchClientUrl(): void { - $cool_request = new CoolRequest(); + $cool_request = $this->service(CoolRequest::class); $client_url = $cool_request->getWopiClientURL(); // The protocol, domain and port are known when this test runs in the // docker-compose setup. @@ -27,11 +27,24 @@ public function testFetchClientUrl(): void { * Tests error behavior when fetching client url. */ public function testFetchClientUrlError(): void { - $cool_request = new CoolRequest(); + $cool_request = $this->service(CoolRequest::class); $client_url = $cool_request->getWopiClientURL(); // The protocol, domain and port are known when this test runs in the // docker-compose setup. $this->assertMatchesRegularExpression('@^http://collabora\.test:9980/browser/[0-9a-f]+/cool\.html\?$@', $client_url); } + /** + * @template T of object + * + * @param class-string $class + * + * @return T&object + */ + protected function service(string $class): object { + $service = \Drupal::service($class); + $this->assertInstanceOf($class, $service); + return $service; + } + }