Concerns
3 months ago
Contracts
3 months ago
Exceptions
3 months ago
Http
3 months ago
Requests
3 months ago
Resources
3 months ago
Responses
3 months ago
Tracing
3 months ago
Value
3 months ago
Api.php
3 months ago
ApiBuilder.php
3 months ago
Config.php
3 months ago
Config.php
88 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Give\Vendors\LiquidWeb\LicensingApiClient; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\RetryPolicy; |
| 7 | use Give\Vendors\LiquidWeb\LicensingApiClient\Value\AuthToken; |
| 8 | |
| 9 | /** |
| 10 | * Stores stable client configuration shared across API requests. |
| 11 | */ |
| 12 | final class Config |
| 13 | { |
| 14 | public string $baseUri; |
| 15 | |
| 16 | public ?AuthToken $configuredToken; |
| 17 | |
| 18 | public string $userAgent; |
| 19 | |
| 20 | public RetryPolicy $retryPolicy; |
| 21 | |
| 22 | public string $restNamespace; |
| 23 | |
| 24 | public string $apiRoot; |
| 25 | |
| 26 | /** |
| 27 | * @throws InvalidArgumentException |
| 28 | */ |
| 29 | public function __construct( |
| 30 | string $baseUri, |
| 31 | ?string $configuredToken = null, |
| 32 | ?string $userAgent = null, |
| 33 | ?RetryPolicy $retryPolicy = null, |
| 34 | ?string $restNamespace = null, |
| 35 | ?string $apiRoot = null |
| 36 | ) { |
| 37 | $baseUri = rtrim($baseUri, '/'); |
| 38 | $apiRoot = trim(($apiRoot ?? 'api'), '/'); |
| 39 | $restNamespace = trim(($restNamespace ?? 'liquidweb'), '/'); |
| 40 | |
| 41 | if ($baseUri === '') { |
| 42 | throw new InvalidArgumentException('Base URI cannot be empty.'); |
| 43 | } |
| 44 | |
| 45 | if ($apiRoot === '') { |
| 46 | throw new InvalidArgumentException('API root cannot be empty.'); |
| 47 | } |
| 48 | |
| 49 | if ($restNamespace === '') { |
| 50 | throw new InvalidArgumentException('REST namespace cannot be empty.'); |
| 51 | } |
| 52 | |
| 53 | $this->baseUri = $baseUri; |
| 54 | $this->configuredToken = $configuredToken !== null ? new AuthToken($configuredToken) : null; |
| 55 | $this->userAgent = $userAgent !== null && $userAgent !== '' ? $userAgent : 'stellarwp/licensing-api-client'; |
| 56 | $this->apiRoot = $apiRoot; |
| 57 | $this->restNamespace = $restNamespace; |
| 58 | $this->retryPolicy = $retryPolicy ?: RetryPolicy::default(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param array{ |
| 63 | * base_uri?: non-empty-string, |
| 64 | * configured_token?: non-empty-string|null, |
| 65 | * user_agent?: non-empty-string|null, |
| 66 | * retry_policy?: RetryPolicy|null, |
| 67 | * rest_namespace?: non-empty-string|null, |
| 68 | * api_root?: non-empty-string|null |
| 69 | * } $config |
| 70 | * |
| 71 | * @throws InvalidArgumentException |
| 72 | */ |
| 73 | public static function fromArray(array $config): self { |
| 74 | return new self( |
| 75 | $config['base_uri'] ?? '', |
| 76 | $config['configured_token'] ?? null, |
| 77 | $config['user_agent'] ?? null, |
| 78 | $config['retry_policy'] ?? null, |
| 79 | $config['rest_namespace'] ?? null, |
| 80 | $config['api_root'] ?? null |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | public function apiRootPath(): string { |
| 85 | return '/' . $this->apiRoot; |
| 86 | } |
| 87 | } |
| 88 |