Skip to content

Commit

Permalink
Issue #56: Introduce interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Dec 3, 2024
1 parent d9ce6af commit e663b8d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 32 deletions.
6 changes: 4 additions & 2 deletions collabora_online.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ services:
logger.channel.collabora_online:
parent: logger.channel_base
arguments: ['cool']
Drupal\collabora_online\Cool\CollaboraDiscoveryFetcher: { }
Drupal\collabora_online\Cool\CollaboraDiscovery: { }
Drupal\collabora_online\Cool\CollaboraDiscoveryFetcherInterface:
class: Drupal\collabora_online\Cool\CollaboraDiscoveryFetcher
Drupal\collabora_online\Cool\CollaboraDiscoveryInterface:
class: Drupal\collabora_online\Cool\CollaboraDiscovery
6 changes: 3 additions & 3 deletions src/Controller/ViewerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Drupal\collabora_online\Controller;

use Drupal\collabora_online\Cool\CollaboraDiscovery;
use Drupal\collabora_online\Cool\CollaboraDiscoveryInterface;
use Drupal\collabora_online\Cool\CoolUtils;
use Drupal\collabora_online\Exception\CollaboraNotAvailableException;
use Drupal\Core\Controller\ControllerBase;
Expand All @@ -32,13 +32,13 @@ class ViewerController extends ControllerBase {
/**
* The controller constructor.
*
* @param \Drupal\collabora_online\Cool\CollaboraDiscovery $discovery
* @param \Drupal\collabora_online\Cool\CollaboraDiscoveryInterface $discovery
* Service to fetch a WOPI client URL.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(
protected readonly CollaboraDiscovery $discovery,
protected readonly CollaboraDiscoveryInterface $discovery,
protected readonly RendererInterface $renderer,
) {}

Expand Down
18 changes: 4 additions & 14 deletions src/Cool/CollaboraDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,20 @@
/**
* Service to get a WOPI client url for a given MIME type.
*/
class CollaboraDiscovery {
class CollaboraDiscovery implements CollaboraDiscoveryInterface {

/**
* Constructor.
*
* @param \Drupal\collabora_online\Cool\CollaboraDiscoveryFetcher $discoveryFetcher
* @param \Drupal\collabora_online\Cool\CollaboraDiscoveryFetcherInterface $discoveryFetcher
* Service to load the discovery.xml from the Collabora server.
*/
public function __construct(
protected readonly CollaboraDiscoveryFetcher $discoveryFetcher,
protected readonly CollaboraDiscoveryFetcherInterface $discoveryFetcher,
) {}

/**
* Gets the URL for the WOPI client.
*
* @param string $mimetype
* Mime type for which to get the WOPI client url.
* This refers to config entries in the discovery.xml file.
*
* @return string
* The WOPI client url.
*
* @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException
* The client url cannot be retrieved.
* {@inheritdoc}
*/
public function getWopiClientURL(string $mimetype = 'text/plain'): string {
$xml = $this->discoveryFetcher->getDiscoveryXml();
Expand Down
10 changes: 2 additions & 8 deletions src/Cool/CollaboraDiscoveryFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Service to load the discovery.xml from the Collabora server.
*/
class CollaboraDiscoveryFetcher {
class CollaboraDiscoveryFetcher implements CollaboraDiscoveryFetcherInterface {

/**
* Constructor.
Expand All @@ -45,13 +45,7 @@ public function __construct(
) {}

/**
* Gets the contents of discovery.xml from the Collabora server.
*
* @return string
* The full contents of discovery.xml.
*
* @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException
* The client url cannot be retrieved.
* {@inheritdoc}
*/
public function getDiscoveryXml(): string {
$discovery_url = $this->getWopiClientServerBaseUrl() . '/hosting/discovery';
Expand Down
33 changes: 33 additions & 0 deletions src/Cool/CollaboraDiscoveryFetcherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

declare(strict_types=1);

namespace Drupal\collabora_online\Cool;

/**
* Service to load the discovery.xml from the Collabora server.
*/
interface CollaboraDiscoveryFetcherInterface {

/**
* Gets the contents of discovery.xml from the Collabora server.
*
* @return string
* The full contents of discovery.xml.
*
* @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException
* The client url cannot be retrieved.
*/
public function getDiscoveryXml(): string;

}
37 changes: 37 additions & 0 deletions src/Cool/CollaboraDiscoveryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

declare(strict_types=1);

namespace Drupal\collabora_online\Cool;

/**
* Service to get a WOPI client url for a given MIME type.
*/
interface CollaboraDiscoveryInterface {

/**
* Gets the URL for the WOPI client.
*
* @param string $mimetype
* Mime type for which to get the WOPI client url.
* This refers to config entries in the discovery.xml file.
*
* @return string
* The WOPI client url.
*
* @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException
* The client url cannot be retrieved.
*/
public function getWopiClientURL(string $mimetype = 'text/plain'): string;

}
10 changes: 5 additions & 5 deletions tests/src/ExistingSite/FetchClientUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Drupal\Tests\collabora_online\ExistingSite;

use Drupal\collabora_online\Cool\CollaboraDiscovery;
use Drupal\collabora_online\Cool\CollaboraDiscoveryInterface;
use Drupal\collabora_online\Exception\CollaboraNotAvailableException;
use weitzman\DrupalTestTraits\ExistingSiteBase;

Expand All @@ -27,8 +27,8 @@ class FetchClientUrlTest extends ExistingSiteBase {
* Tests fetching the client url.
*/
public function testFetchClientUrl(): void {
/** @var \Drupal\collabora_online\Cool\CollaboraDiscovery $discovery */
$discovery = \Drupal::service(CollaboraDiscovery::class);
/** @var \Drupal\collabora_online\Cool\CollaboraDiscoveryInterface $discovery */
$discovery = \Drupal::service(CollaboraDiscoveryInterface::class);
$client_url = $discovery->getWopiClientURL();
// The protocol, domain and port are known when this test runs in the
// docker-compose setup.
Expand All @@ -46,8 +46,8 @@ public function testFetchClientUrlWithMisconfiguration(): void {
'server' => 'httx://example.com',
],
]);
/** @var \Drupal\collabora_online\Cool\CollaboraDiscovery $discovery */
$discovery = \Drupal::service(CollaboraDiscovery::class);
/** @var \Drupal\collabora_online\Cool\CollaboraDiscoveryInterface $discovery */
$discovery = \Drupal::service(CollaboraDiscoveryInterface::class);

$this->expectException(CollaboraNotAvailableException::class);
$this->expectExceptionMessage("The configured Collabora Online server address must begin with 'http://' or 'https://'. Found 'httx://example.com'.");
Expand Down

0 comments on commit e663b8d

Please sign in to comment.