Factories
3 months ago
ApiUri.php
3 months ago
ApiVersion.php
3 months ago
AuthContext.php
3 months ago
AuthState.php
3 months ago
JsonDecoder.php
3 months ago
RequestBuilder.php
3 months ago
RequestExecutor.php
3 months ago
RequestHeaderCollection.php
3 months ago
RetryPolicy.php
3 months ago
AuthContext.php
56 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Give\Vendors\LiquidWeb\LicensingApiClient\Http; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | |
| 7 | /** |
| 8 | * Represents the authentication mode the API client should use for a request. |
| 9 | */ |
| 10 | final class AuthContext |
| 11 | { |
| 12 | public const MODE_AUTO = 'auto'; |
| 13 | public const MODE_NONE = 'none'; |
| 14 | public const MODE_CONFIGURED = 'configured'; |
| 15 | public const MODE_EXPLICIT = 'explicit'; |
| 16 | |
| 17 | private string $mode; |
| 18 | |
| 19 | /** |
| 20 | * @throws InvalidArgumentException |
| 21 | */ |
| 22 | public function __construct(string $mode = self::MODE_AUTO) { |
| 23 | $this->assertValidMode($mode); |
| 24 | |
| 25 | $this->mode = $mode; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @throws InvalidArgumentException |
| 30 | */ |
| 31 | private function assertValidMode(string $mode): void { |
| 32 | $validModes = [ |
| 33 | self::MODE_AUTO, |
| 34 | self::MODE_NONE, |
| 35 | self::MODE_CONFIGURED, |
| 36 | self::MODE_EXPLICIT, |
| 37 | ]; |
| 38 | |
| 39 | if ( ! in_array($mode, $validModes, true)) { |
| 40 | throw new InvalidArgumentException('Unsupported auth mode [' . $mode . '].'); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public function requiresToken(): bool { |
| 45 | return $this->mode === self::MODE_CONFIGURED || $this->mode === self::MODE_EXPLICIT; |
| 46 | } |
| 47 | |
| 48 | public function equals(self $authContext): bool { |
| 49 | return $this->mode === $authContext->mode(); |
| 50 | } |
| 51 | |
| 52 | public function mode(): string { |
| 53 | return $this->mode; |
| 54 | } |
| 55 | } |
| 56 |