Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/private/OCS/DiscoveryService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
*
Expand Down Expand Up @@ -60,11 +61,14 @@ public function __construct(ICacheFactory $cacheFactory,
* @param string $service the service you want to discover
* @return array
*/
public function discover($remote, $service) {
public function discover(string $remote, string $service): array {
// Check the cache first
$cacheData = $this->cache->get($remote . '#' . $service);
if($cacheData) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the next line, maybe we should add a check here since json_decode might be null which will then cause a type error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds sane I'll add it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return json_decode($cacheData, true);
$data = json_decode($cacheData, true);
if (\is_array($data)) {
return $data;
}
}

$discoveredServices = [];
Expand All @@ -77,7 +81,9 @@ public function discover($remote, $service) {
]);
if($response->getStatusCode() === Http::STATUS_OK) {
$decodedServices = json_decode($response->getBody(), true);
$discoveredServices = $this->getEndpoints($decodedServices, $service);
if (\is_array($decodedServices)) {
$discoveredServices = $this->getEndpoints($decodedServices, $service);
}
}
} catch (\Exception $e) {
// if we couldn't discover the service or any end-points we return a empty array
Expand All @@ -91,17 +97,15 @@ public function discover($remote, $service) {
/**
* get requested end-points from the requested service
*
* @param $decodedServices
* @param $service
* @param array $decodedServices
* @param string $service
* @return array
*/
protected function getEndpoints($decodedServices, $service) {
protected function getEndpoints(array $decodedServices, string $service): array {

$discoveredServices = [];

if(is_array($decodedServices) &&
isset($decodedServices['services'][$service]['endpoints'])
) {
if(isset($decodedServices['services'][$service]['endpoints'])) {
foreach ($decodedServices['services'][$service]['endpoints'] as $endpoint => $url) {
if($this->isSafeUrl($url)) {
$discoveredServices[$endpoint] = $url;
Expand All @@ -119,7 +123,7 @@ protected function getEndpoints($decodedServices, $service) {
* @param string $url
* @return bool
*/
protected function isSafeUrl($url) {
protected function isSafeUrl(string $url): bool {
return (bool)preg_match('/^[\/\.\-A-Za-z0-9]+$/', $url);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/public/OCS/IDiscoveryService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
*
Expand Down Expand Up @@ -45,6 +46,6 @@ interface IDiscoveryService {
* @param string $service the service you want to discover
* @return array
*/
public function discover($remote, $service);
public function discover(string $remote, string $service): array;

}