From e13a51d1fa49603f06c3ea3dec05a151abd61731 Mon Sep 17 00:00:00 2001 From: Andreas Hennings Date: Thu, 14 Nov 2024 00:09:47 +0100 Subject: [PATCH] Issue #56: Split raw xml loading into separate service. --- collabora_online.services.yml | 1 + src/Cool/CoolDiscoveryXmlEndpoint.php | 133 ++++++++++++++++++++++++++ src/Cool/CoolRequest.php | 103 +------------------- 3 files changed, 138 insertions(+), 99 deletions(-) create mode 100644 src/Cool/CoolDiscoveryXmlEndpoint.php diff --git a/collabora_online.services.yml b/collabora_online.services.yml index d8ae3ea0..d0cd35a2 100644 --- a/collabora_online.services.yml +++ b/collabora_online.services.yml @@ -4,4 +4,5 @@ services: logger.channel.collabora_online: parent: logger.channel_base arguments: ['cool'] + Drupal\collabora_online\Cool\CoolDiscoveryXmlEndpoint: { } Drupal\collabora_online\Cool\CoolRequest: { } diff --git a/src/Cool/CoolDiscoveryXmlEndpoint.php b/src/Cool/CoolDiscoveryXmlEndpoint.php new file mode 100644 index 00000000..bfea32d2 --- /dev/null +++ b/src/Cool/CoolDiscoveryXmlEndpoint.php @@ -0,0 +1,133 @@ +configFactory->get('collabora_online.settings')->get('cool'); + if (!$cool_settings) { + throw new CoolRequestException( + 'The Collabora Online connection is not configured.', + // Use the same code as was previously used in this case. + 201, + ); + } + $wopi_client_server = $cool_settings['server'] ?? NULL; + if (!$wopi_client_server) { + throw new CoolRequestException( + 'The configured Collabora Online server address is empty.', + 201, + ); + } + $wopi_client_server = trim($wopi_client_server); + + if (!preg_match('@^https?://@', $wopi_client_server)) { + throw new CoolRequestException( + sprintf( + "The configured Collabora Online server address must begin with 'http://' or 'https://'. Found '%s'.", + $wopi_client_server, + ), + 204, + ); + } + + return $wopi_client_server; + } + + /** + * Gets the contents of discovery.xml from the Collabora server. + * + * @return string + * The full contents of discovery.xml. + * + * @throws \Drupal\collabora_online\Exception\CoolRequestException + * The client url cannot be retrieved. + */ + public function getDiscoveryXml(): string { + $discovery_url = $this->getWopiClientServerBaseUrl() . '/hosting/discovery'; + + $cool_settings = $this->configFactory->get('collabora_online.settings')->get('cool'); + if (!$cool_settings) { + throw new CoolRequestException( + 'The Collabora Online connection is not configured.', + // Use the same code as was previously used in this case. + 203, + ); + } + $disable_checks = !empty($cool_settings['disable_cert_check']); + + try { + $response = $this->client->get($discovery_url, [ + RequestOptions::VERIFY => !$disable_checks, + ]); + $xml = $response->getBody()->getContents(); + } + catch (ClientExceptionInterface $e) { + // The backtrace of a client exception is typically not very + // interesting. Just log the message. + $this->logger->error("Failed to fetch from '@url': @message.", [ + '@url' => $discovery_url, + '@message' => $e->getMessage(), + ]); + throw new CoolRequestException( + 'Not able to retrieve the discovery.xml file from the Collabora Online server.', + 203, + $e, + ); + } + return $xml; + } + +} diff --git a/src/Cool/CoolRequest.php b/src/Cool/CoolRequest.php index d3529013..25ccdb0f 100644 --- a/src/Cool/CoolRequest.php +++ b/src/Cool/CoolRequest.php @@ -13,12 +13,6 @@ namespace Drupal\collabora_online\Cool; use Drupal\collabora_online\Exception\CoolRequestException; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Logger\LoggerChannelInterface; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\RequestOptions; -use Psr\Http\Client\ClientExceptionInterface; -use Symfony\Component\DependencyInjection\Attribute\Autowire; /** * Service to fetch a WOPI client url. @@ -28,18 +22,11 @@ class CoolRequest { /** * Constructor. * - * @param \Drupal\Core\Logger\LoggerChannelInterface $logger - * Logger channel. - * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory - * Config factory. - * @param \GuzzleHttp\ClientInterface $client - * Http client. + * @param \Drupal\collabora_online\Cool\CoolDiscoveryXmlEndpoint $discoveryXmlEndpoint + * Service to load the discovery.xml from the Collabora server. */ public function __construct( - #[Autowire(service: 'logger.channel.collabora_online')] - protected readonly LoggerChannelInterface $logger, - protected readonly ConfigFactoryInterface $configFactory, - protected readonly ClientInterface $client, + protected readonly CoolDiscoveryXmlEndpoint $discoveryXmlEndpoint, ) {} /** @@ -56,7 +43,7 @@ public function __construct( * The client url cannot be retrieved. */ public function getWopiClientURL(string $mimetype = 'text/plain'): string { - $discovery = $this->getDiscoveryXml(); + $discovery = $this->discoveryXmlEndpoint->getDiscoveryXml(); $discovery_parsed = simplexml_load_string($discovery); if (!$discovery_parsed) { @@ -71,88 +58,6 @@ public function getWopiClientURL(string $mimetype = 'text/plain'): string { return $wopi_src; } - /** - * Loads the WOPI server url from configuration. - * - * @throws \Drupal\collabora_online\Exception\CoolRequestException - * The WOPI server url is misconfigured, or the protocol does not match - * that of the current Drupal request. - */ - protected function getWopiClientServerBaseUrl(): string { - $cool_settings = $this->configFactory->get('collabora_online.settings')->get('cool'); - if (!$cool_settings) { - throw new CoolRequestException( - 'The Collabora Online connection is not configured.', - // Use the same code as was previously used in this case. - 201, - ); - } - $wopi_client_server = $cool_settings['server'] ?? NULL; - if (!$wopi_client_server) { - throw new CoolRequestException( - 'The configured Collabora Online server address is empty.', - 201, - ); - } - $wopi_client_server = trim($wopi_client_server); - - if (!preg_match('@^https?://@', $wopi_client_server)) { - throw new CoolRequestException( - sprintf( - "The configured Collabora Online server address must begin with 'http://' or 'https://'. Found '%s'.", - $wopi_client_server, - ), - 204, - ); - } - - return $wopi_client_server; - } - - /** - * Gets the contents of discovery.xml from the Collabora server. - * - * @return string - * The full contents of discovery.xml. - * - * @throws \Drupal\collabora_online\Exception\CoolRequestException - * The client url cannot be retrieved. - */ - protected function getDiscoveryXml(): string { - $discovery_url = $this->getWopiClientServerBaseUrl() . '/hosting/discovery'; - - $cool_settings = $this->configFactory->get('collabora_online.settings')->get('cool'); - if (!$cool_settings) { - throw new CoolRequestException( - 'The Collabora Online connection is not configured.', - // Use the same code as was previously used in this case. - 203, - ); - } - $disable_checks = !empty($cool_settings['disable_cert_check']); - - try { - $response = $this->client->get($discovery_url, [ - RequestOptions::VERIFY => !$disable_checks, - ]); - $xml = $response->getBody()->getContents(); - } - catch (ClientExceptionInterface $e) { - // The backtrace of a client exception is typically not very - // interesting. Just log the message. - $this->logger->error("Failed to fetch from '@url': @message.", [ - '@url' => $discovery_url, - '@message' => $e->getMessage(), - ]); - throw new CoolRequestException( - 'Not able to retrieve the discovery.xml file from the Collabora Online server.', - 203, - $e, - ); - } - return $xml; - } - /** * Extracts a WOPI url from the parsed discovery.xml. *