Strict DiscoveryService

* Made strict
* Type hints
* Return types

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2018-01-16 20:01:38 +01:00
parent 13a787e2f5
commit cdfffe7bda
No known key found for this signature in database
GPG key ID: F941078878347C0C
2 changed files with 12 additions and 10 deletions

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
*
@ -60,7 +61,7 @@ class DiscoveryService implements 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 {
// Check the cache first
$cacheData = $this->cache->get($remote . '#' . $service);
if($cacheData) {
@ -77,8 +78,10 @@ class DiscoveryService implements IDiscoveryService {
]);
if($response->getStatusCode() === Http::STATUS_OK) {
$decodedServices = json_decode($response->getBody(), true);
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
}
@ -91,17 +94,15 @@ class DiscoveryService implements IDiscoveryService {
/**
* 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;
@ -119,7 +120,7 @@ class DiscoveryService implements IDiscoveryService {
* @param string $url
* @return bool
*/
protected function isSafeUrl($url) {
protected function isSafeUrl(string $url): bool {
return (bool)preg_match('/^[\/\.\-A-Za-z0-9]+$/', $url);
}

View file

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
*
@ -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;
}