From 0b5f7fef03f95a405da0d3634486de3f7f6a74ad Mon Sep 17 00:00:00 2001 From: "Martin Studer, fluxlabs ag" Date: Fri, 15 Apr 2022 18:39:06 -0400 Subject: [PATCH] 1-0-0 --- README.md | 19 ++++++++ composer.json | 6 +++ composer.lock | 2 +- examples/query.php | 13 +----- fn/query.php | 10 +++++ src/Adapters/Configs/OpenApiOutbounds.php | 25 ----------- src/Adapters/Outbounds.php | 25 +++++++++++ src/{OpenApiClientApi.php => Api.php} | 15 +++++-- src/{OpenApiClientConfig.php => Config.php} | 31 ++++++++----- .../Handlers/AuthorizationHandler.php | 2 - .../{Configs/OpenApiConfig.php => Config.php} | 4 +- src/Core/Ports/Configs/OpenApiOutbounds.php | 8 ---- src/Core/Ports/OpenApiService.php | 12 ++--- src/Core/Ports/Outbounds.php | 8 ++++ src/Env.php | 45 +++++++++++++++++++ 15 files changed, 156 insertions(+), 69 deletions(-) create mode 100644 fn/query.php delete mode 100644 src/Adapters/Configs/OpenApiOutbounds.php create mode 100644 src/Adapters/Outbounds.php rename src/{OpenApiClientApi.php => Api.php} (55%) rename src/{OpenApiClientConfig.php => Config.php} (63%) rename src/Core/Ports/{Configs/OpenApiConfig.php => Config.php} (75%) delete mode 100644 src/Core/Ports/Configs/OpenApiOutbounds.php create mode 100644 src/Core/Ports/Outbounds.php create mode 100644 src/Env.php diff --git a/README.md b/README.md index 3fd7765..9f4d90d 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,25 @@ This component is supposed to assist in querying openapis. +## Functional Usage + +``` +fluxDotEnv\loadDotEnv(__DIR__); + +$result = fluxOpenApiClient\query('/api/ilias/accounts', [], 'EXAMPLE_'); +print_r($result); exit; +``` + +outputs: +``` +Array +( + [firstname] => Emmett + [lastname] => Brown + [lastChanged] => 2021-01-15T05:30:10+01:00 +) +``` + ## Contributing :purple_heart: Please ... diff --git a/composer.json b/composer.json index 2423d44..ca09271 100644 --- a/composer.json +++ b/composer.json @@ -32,9 +32,15 @@ "swoole/ide-helper": ">=4.6" }, "autoload": { + "files": [ + "fn/query.php" + ], "psr-4": { "FluxEco\\OpenApiClient\\": [ "src/" + ], + "fluxOpenApiClient\\": [ + "fn/" ] } }, diff --git a/composer.lock b/composer.lock index ab05f74..144772b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e9ea5889e3e5fdcf4e6c313a8549b4d3", + "content-hash": "972a00ce9a10c67971d48f25705e9f1d", "packages": [ { "name": "flux-eco/dot-env", diff --git a/examples/query.php b/examples/query.php index d3d3283..06ffe04 100644 --- a/examples/query.php +++ b/examples/query.php @@ -2,16 +2,7 @@ require_once __DIR__ . '/../vendor/autoload.php'; -fluxy\loadDotEnv(__DIR__); +fluxDotEnv\loadDotEnv(__DIR__); -$client = FluxEco\OpenApiClient\OpenApiClientApi::new( - FluxEco\OpenApiClient\OpenApiClientConfig::new( - getenv('EXAMPLE_OPEN_API_CLIENT_ID'), - getenv('EXAMPLE_OPEN_API_SECRET'), - getenv('EXAMPLE_OPEN_API_SCOPE'), - getenv('EXAMPLE_OPEN_API_API_URL'), - getenv('EXAMPLE_OPEN_API_AUTHENTICATION_URL'), - ) -); -$result = $client->query('/api/ilias/accounts'); +$result = fluxOpenApiClient\query('/api/ilias/accounts', [], 'EXAMPLE_'); print_r($result); exit; \ No newline at end of file diff --git a/fn/query.php b/fn/query.php new file mode 100644 index 0000000..6c3a170 --- /dev/null +++ b/fn/query.php @@ -0,0 +1,10 @@ +query($endpoint, $filter); +} \ No newline at end of file diff --git a/src/Adapters/Configs/OpenApiOutbounds.php b/src/Adapters/Configs/OpenApiOutbounds.php deleted file mode 100644 index 048309c..0000000 --- a/src/Adapters/Configs/OpenApiOutbounds.php +++ /dev/null @@ -1,25 +0,0 @@ -openApiConfig = $openApiConfig; - } - - public static function new(Ports\Configs\OpenApiConfig $openApiConfig): self - { - return new self($openApiConfig); - } - - final public function getOpenApiConfig(): Ports\Configs\OpenApiConfig - { - return $this->openApiConfig; - } - -} \ No newline at end of file diff --git a/src/Adapters/Outbounds.php b/src/Adapters/Outbounds.php new file mode 100644 index 0000000..8ea58b8 --- /dev/null +++ b/src/Adapters/Outbounds.php @@ -0,0 +1,25 @@ +config = $config; + } + + public static function new(Ports\Config $config): self + { + return new self($config); + } + + final public function getConfig(): Ports\Config + { + return $this->config; + } + +} \ No newline at end of file diff --git a/src/OpenApiClientApi.php b/src/Api.php similarity index 55% rename from src/OpenApiClientApi.php rename to src/Api.php index 95d2880..76f8b0a 100644 --- a/src/OpenApiClientApi.php +++ b/src/Api.php @@ -2,7 +2,9 @@ namespace FluxEco\OpenApiClient; -class OpenApiClientApi +use FluxEco\OpenApiClient\Adapters\Outbounds; + +class Api { private Core\Ports\OpenApiService $openApiService; @@ -12,9 +14,16 @@ private function __construct( $this->openApiService = $openApiService; } - public static function new(OpenApiClientConfig $openApiConfig) : self + public static function new(Config $config) : self + { + $outbounds = Outbounds::new($config); + $openApiService = Core\Ports\OpenApiService::new($outbounds); + return new self($openApiService); + } + + public static function newFromEnv(string $envPrefix) : self { - $outbounds = Adapters\Configs\OpenApiOutbounds::new($openApiConfig); + $outbounds = Outbounds::new(Config::newFromEnv($envPrefix)); $openApiService = Core\Ports\OpenApiService::new($outbounds); return new self($openApiService); } diff --git a/src/OpenApiClientConfig.php b/src/Config.php similarity index 63% rename from src/OpenApiClientConfig.php rename to src/Config.php index f912fb4..0bdd757 100644 --- a/src/OpenApiClientConfig.php +++ b/src/Config.php @@ -1,11 +1,10 @@ clientId = $clientId; $this->secret = $secret; $this->scope = $scope; @@ -34,8 +32,7 @@ public static function new( string $scope, string $apiUrl, string $authenticationUrl - ): self - { + ) : self { return new self( $clientId, $secret, @@ -45,27 +42,39 @@ public static function new( ); } - final public function getClientId(): string + public static function newFromEnv(string $envPrefix) : self + { + $apiEnv = Env::new($envPrefix); + return new self( + $apiEnv->getOpenApiClientId(), + $apiEnv->getOpenApiSecret(), + $apiEnv->getOpenApiScope(), + $apiEnv->getOpenApiUrl(), + $apiEnv->getOpenApiAuthenticationUrl() + ); + } + + final public function getClientId() : string { return $this->clientId; } - final public function getSecret(): string + final public function getSecret() : string { return $this->secret; } - final public function getScope(): string + final public function getScope() : string { return $this->scope; } - final public function getApiUrl(): string + final public function getApiUrl() : string { return $this->apiUrl; } - final public function getAuthenticationUrl(): string + final public function getAuthenticationUrl() : string { return $this->authenticationUrl; } diff --git a/src/Core/Application/Handlers/AuthorizationHandler.php b/src/Core/Application/Handlers/AuthorizationHandler.php index 33d4138..1f66ee6 100644 --- a/src/Core/Application/Handlers/AuthorizationHandler.php +++ b/src/Core/Application/Handlers/AuthorizationHandler.php @@ -43,8 +43,6 @@ public function handle(AuthorizationCommand $command): Domain\Models\Authorizati $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status !== 200) { - print_r($content); - print_r($status); throw new Exception($content, $status); } diff --git a/src/Core/Ports/Configs/OpenApiConfig.php b/src/Core/Ports/Config.php similarity index 75% rename from src/Core/Ports/Configs/OpenApiConfig.php rename to src/Core/Ports/Config.php index d321a10..bf1e51d 100644 --- a/src/Core/Ports/Configs/OpenApiConfig.php +++ b/src/Core/Ports/Config.php @@ -1,8 +1,8 @@ openApiConfig = $openApiConfig; + $this->config = $config; } public static function new( - Ports\Configs\OpenApiOutbounds $openApiOutbounds + Ports\Outbounds $openApiOutbounds ): self { return new self( - $openApiOutbounds->getOpenApiConfig() + $openApiOutbounds->getConfig() ); } public function query(string $endpoint, array $filter): array { - $openApiConfig = $this->openApiConfig; + $openApiConfig = $this->config; $authorizationCommand = Application\Handlers\AuthorizationCommand::new( $openApiConfig->getClientId(), $openApiConfig->getSecret(), diff --git a/src/Core/Ports/Outbounds.php b/src/Core/Ports/Outbounds.php new file mode 100644 index 0000000..429e153 --- /dev/null +++ b/src/Core/Ports/Outbounds.php @@ -0,0 +1,8 @@ +envPrefix = $envPrefix; + } + + public static function new(string $envPrefix = '') { + return new self($envPrefix); + } + + public function getOpenApiClientId(): string { + return getenv($this->envPrefix.self::OPEN_API_CLIENT_ID); + } + + public function getOpenApiSecret(): string { + return getenv($this->envPrefix.self::OPEN_API_SECRET); + } + + public function getOpenApiScope(): string { + return getenv($this->envPrefix.self::OPEN_API_SCOPE); + } + + public function getOpenApiUrl(): string { + return getenv($this->envPrefix.self::OPEN_API_API_URL); + } + + public function getOpenApiAuthenticationUrl(): string { + return getenv($this->envPrefix.self::OPEN_API_AUTHENTICATION_URL); + } +} \ No newline at end of file