Skip to content

Commit

Permalink
Issue CollaboraOnline#56: Extract getWopiClientServerBaseUrl().
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Nov 12, 2024
1 parent a4d7a2c commit db065fe
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions src/Cool/CoolRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,7 @@ public function __construct(
* The client url cannot be retrieved.
*/
public function getWopiClientURL(): string {
$default_config = $this->configFactory->get('collabora_online.settings');
$wopi_client_server = $default_config->get('cool')['server'];
if (!$wopi_client_server) {
throw new CoolRequestException(
'Collabora Online server address is not valid.',
201,
);
}
$wopi_client_server = trim($wopi_client_server);

if (!str_starts_with($wopi_client_server, 'http')) {
throw new CoolRequestException(
'Warning! You have to specify the scheme protocol too (http|https) for the server address.',
204,
);
}

if (!str_starts_with($wopi_client_server, $this->request->getScheme() . '://')) {
throw new CoolRequestException(
'Collabora Online server address scheme does not match the current page url scheme.',
202,
);
}

$wopi_client_server = $this->getWopiClientServerBaseUrl();
$discovery = $this->getDiscovery($wopi_client_server);
if ($discovery === FALSE) {
throw new CoolRequestException(
Expand Down Expand Up @@ -101,6 +78,44 @@ public function getWopiClientURL(): string {
return $wopi_src;
}

/**
* Loads the WOPI server url from configuration.
*
* @return string
* The base url of the WOPI server.
*
* @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 {
$default_config = $this->configFactory->get('collabora_online.settings');
$wopi_client_server = $default_config->get('cool')['server'];
if (!$wopi_client_server) {
throw new CoolRequestException(
'Collabora Online server address is not valid.',
201,
);
}
$wopi_client_server = trim($wopi_client_server);

if (!str_starts_with($wopi_client_server, 'http')) {
throw new CoolRequestException(
'Warning! You have to specify the scheme protocol too (http|https) for the server address.',
204,
);
}

if (!str_starts_with($wopi_client_server, $this->request->getScheme() . '://')) {
throw new CoolRequestException(
'Collabora Online server address scheme does not match the current page url scheme.',
202,
);
}

return $wopi_client_server;
}

/**
* Gets the contents of discovery.xml from the Collabora server.
*
Expand Down

0 comments on commit db065fe

Please sign in to comment.