Maj Library

This commit is contained in:
EoleDev 2016-03-23 15:30:47 +01:00
parent 0dc17aa9ef
commit 54ec6723de
72 changed files with 525 additions and 455 deletions

View file

@ -1,15 +1,15 @@
<?php
<?php declare(strict_types = 1);
namespace OpenCloud\Common\Service;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Middleware as GuzzleMiddleware;
use OpenCloud\Common\Auth\IdentityService;
use OpenCloud\Common\Auth\Token;
use OpenCloud\Common\Transport\HandlerStack;
use OpenCloud\Common\Transport\Middleware;
use OpenCloud\Common\Transport\Utils;
use OpenCloud\Identity\v3\Service;
/**
* A Builder for easily creating OpenCloud services.
@ -54,7 +54,7 @@ class Builder
*
* @return array
*/
private function getClasses($serviceName, $serviceVersion)
private function getClasses(string $serviceName, int $serviceVersion)
{
$rootNamespace = sprintf("%s\\%s\\v%d", $this->rootNamespace, $serviceName, $serviceVersion);
@ -70,19 +70,18 @@ class Builder
* directly - this setup includes the configuration of the HTTP client's base URL, and the
* attachment of an authentication handler.
*
* @param $serviceName The name of the service as it appears in the OpenCloud\* namespace
* @param $serviceVersion The major version of the service
* @param array $serviceOptions The service-specific options to use
* @param string $serviceName The name of the service as it appears in the OpenCloud\* namespace
* @param int $serviceVersion The major version of the service
* @param array $serviceOptions The service-specific options to use
*
* @return \OpenCloud\Common\Service\ServiceInterface
*
* @throws \Exception
*/
public function createService($serviceName, $serviceVersion, array $serviceOptions = [])
public function createService(string $serviceName, int $serviceVersion, array $serviceOptions = []): ServiceInterface
{
$options = $this->mergeOptions($serviceOptions);
$this->stockIdentityService($options);
$this->stockAuthHandler($options);
$this->stockHttpClient($options, $serviceName);
@ -91,7 +90,7 @@ class Builder
return new $serviceClass($options['httpClient'], new $apiClass());
}
private function stockHttpClient(array &$options, $serviceName)
private function stockHttpClient(array &$options, string $serviceName)
{
if (!isset($options['httpClient']) || !($options['httpClient'] instanceof ClientInterface)) {
if (strcasecmp($serviceName, 'identity') === 0) {
@ -121,16 +120,9 @@ class Builder
}
}
private function stockIdentityService(array &$options)
{
if (!isset($options['identityService'])) {
$httpClient = $this->httpClient($options['authUrl'], HandlerStack::create());
$options['identityService'] = Service::factory($httpClient);
}
}
/**
* @param array $options
*
* @codeCoverageIgnore
*/
private function stockAuthHandler(array &$options)
@ -142,14 +134,14 @@ class Builder
}
}
private function getStack(callable $authHandler, Token $token = null)
private function getStack(callable $authHandler, Token $token = null): HandlerStack
{
$stack = HandlerStack::create();
$stack->push(Middleware::authHandler($authHandler, $token));
return $stack;
}
private function httpClient($baseUrl, HandlerStack $stack)
private function httpClient(string $baseUrl, HandlerStack $stack): ClientInterface
{
return new Client([
'base_uri' => Utils::normalizeUrl($baseUrl),
@ -157,7 +149,7 @@ class Builder
]);
}
private function mergeOptions(array $serviceOptions)
private function mergeOptions(array $serviceOptions): array
{
$options = array_merge($this->defaults, $this->globalOptions, $serviceOptions);
@ -165,6 +157,12 @@ class Builder
throw new \InvalidArgumentException('"authUrl" is a required option');
}
if (!isset($options['identityService']) || !($options['identityService'] instanceof IdentityService)) {
throw new \InvalidArgumentException(sprintf(
'"identityService" must be specified and implement %s', IdentityService::class
));
}
return $options;
}
}
}