give
/
vendor
/
vendor-prefixed
/
stellarwp
/
licensing-api-client
/
src
/
Resources
/
TokensResource.php
give
/
vendor
/
vendor-prefixed
/
stellarwp
/
licensing-api-client
/
src
/
Resources
Last commit date
Concerns
3 months ago
Contracts
3 months ago
Credit
3 months ago
EntitlementsResource.php
3 months ago
LicensesResource.php
3 months ago
ProductsResource.php
3 months ago
TokensResource.php
3 months ago
TokensResource.php
170 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Give\Vendors\LiquidWeb\LicensingApiClient\Resources; |
| 4 | |
| 5 | use JsonException; |
| 6 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\Contracts\ApiErrorExceptionInterface; |
| 7 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\MissingAuthenticationException; |
| 8 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\UnexpectedResponseException; |
| 9 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\AuthState; |
| 10 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\Factories\ApiUriFactory; |
| 11 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\RequestExecutor; |
| 12 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\RequestHeaderCollection; |
| 13 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\Token\Create as CreateRequest; |
| 14 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\Token\Revoke as RevokeRequest; |
| 15 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Concerns\RebindsAuthState; |
| 16 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Concerns\RebindsRequestHeaderCollection; |
| 17 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Contracts\TokensResourceInterface; |
| 18 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\Token\Auth; |
| 19 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\Token\TokenList; |
| 20 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\Token\ValueObjects\TokenItem; |
| 21 | use Give\Vendors\Psr\Http\Client\ClientExceptionInterface; |
| 22 | |
| 23 | /** |
| 24 | * Provides operations for the tokens API resource. |
| 25 | * |
| 26 | * @phpstan-import-type CreateTokenPayload from CreateRequest |
| 27 | * @phpstan-import-type RevokeTokenPayload from RevokeRequest |
| 28 | * @phpstan-import-type TokenItemPayload from TokenItem |
| 29 | * @phpstan-type CreateResponsePayload TokenItemPayload |
| 30 | * @phpstan-type RevokeResponsePayload TokenItemPayload |
| 31 | * @phpstan-type AuthResponsePayload array{authorized: bool} |
| 32 | * @phpstan-type TokenListPayload array{ |
| 33 | * tokens: list<array{ |
| 34 | * id: int, |
| 35 | * token: string, |
| 36 | * license_id: int, |
| 37 | * domain: string, |
| 38 | * is_revoked: bool, |
| 39 | * created_at: string, |
| 40 | * updated_at: string |
| 41 | * }> |
| 42 | * } |
| 43 | */ |
| 44 | final class TokensResource implements TokensResourceInterface |
| 45 | { |
| 46 | use RebindsAuthState; |
| 47 | use RebindsRequestHeaderCollection; |
| 48 | |
| 49 | private RequestExecutor $requestExecutor; |
| 50 | |
| 51 | private ApiUriFactory $apiUriFactory; |
| 52 | |
| 53 | private AuthState $authState; |
| 54 | |
| 55 | private RequestHeaderCollection $requestHeaderCollection; |
| 56 | |
| 57 | public function __construct( |
| 58 | RequestExecutor $requestExecutor, |
| 59 | ApiUriFactory $apiUriFactory, |
| 60 | AuthState $authState, |
| 61 | RequestHeaderCollection $requestHeaderCollection |
| 62 | ) { |
| 63 | $this->requestExecutor = $requestExecutor; |
| 64 | $this->apiUriFactory = $apiUriFactory; |
| 65 | $this->authState = $authState; |
| 66 | $this->requestHeaderCollection = $requestHeaderCollection; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @throws ApiErrorExceptionInterface |
| 71 | * @throws MissingAuthenticationException |
| 72 | * @throws UnexpectedResponseException |
| 73 | * @throws ClientExceptionInterface |
| 74 | * @throws JsonException |
| 75 | */ |
| 76 | public function list(string $licenseKey): TokenList { |
| 77 | $result = $this->requestExecutor->executeJson( |
| 78 | 'GET', |
| 79 | $this->apiUriFactory->make('/tokens'), |
| 80 | ['license_key' => $licenseKey], |
| 81 | null, |
| 82 | $this->authState->requiredToken(), |
| 83 | $this->requestHeaderCollection->all() |
| 84 | ); |
| 85 | |
| 86 | /** @var TokenListPayload $result */ |
| 87 | return TokenList::from($result); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @throws ApiErrorExceptionInterface |
| 92 | * @throws MissingAuthenticationException |
| 93 | * @throws UnexpectedResponseException |
| 94 | * @throws ClientExceptionInterface |
| 95 | * @throws JsonException |
| 96 | */ |
| 97 | public function create(CreateRequest $request): TokenItem { |
| 98 | /** @var CreateTokenPayload $body */ |
| 99 | $body = $request->toArray(); |
| 100 | |
| 101 | $result = $this->requestExecutor->executeJson( |
| 102 | 'POST', |
| 103 | $this->apiUriFactory->make('/tokens/create'), |
| 104 | [], |
| 105 | $body, |
| 106 | $this->authState->requiredToken(), |
| 107 | $this->requestHeaderCollection->all() |
| 108 | ); |
| 109 | |
| 110 | /** @var CreateResponsePayload $result */ |
| 111 | return TokenItem::from($result); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @throws ApiErrorExceptionInterface |
| 116 | * @throws MissingAuthenticationException |
| 117 | * @throws UnexpectedResponseException |
| 118 | * @throws ClientExceptionInterface |
| 119 | * @throws JsonException |
| 120 | */ |
| 121 | public function revoke(RevokeRequest $request): TokenItem { |
| 122 | /** @var RevokeTokenPayload $body */ |
| 123 | $body = $request->toArray(); |
| 124 | |
| 125 | $result = $this->requestExecutor->executeJson( |
| 126 | 'PUT', |
| 127 | $this->apiUriFactory->make('/tokens/revoke'), |
| 128 | [], |
| 129 | $body, |
| 130 | $this->authState->requiredToken(), |
| 131 | $this->requestHeaderCollection->all() |
| 132 | ); |
| 133 | |
| 134 | /** @var RevokeResponsePayload $result */ |
| 135 | return TokenItem::from($result); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @throws ApiErrorExceptionInterface |
| 140 | * @throws UnexpectedResponseException |
| 141 | * @throws ClientExceptionInterface |
| 142 | * @throws JsonException |
| 143 | */ |
| 144 | public function auth(string $licenseKey, string $token, string $domain): Auth { |
| 145 | $result = $this->requestExecutor->executeJson( |
| 146 | 'GET', |
| 147 | $this->apiUriFactory->make('/tokens/auth'), |
| 148 | [ |
| 149 | 'license_key' => $licenseKey, |
| 150 | 'token' => $token, |
| 151 | 'domain' => $domain, |
| 152 | ], |
| 153 | null, |
| 154 | $this->authState->optionalToken(), |
| 155 | $this->requestHeaderCollection->all() |
| 156 | ); |
| 157 | |
| 158 | /** @var AuthResponsePayload $result */ |
| 159 | return Auth::from($result); |
| 160 | } |
| 161 | |
| 162 | protected function rebindWithAuthState(AuthState $authState): self { |
| 163 | return new self($this->requestExecutor, $this->apiUriFactory, $authState, $this->requestHeaderCollection); |
| 164 | } |
| 165 | |
| 166 | protected function rebindWithRequestHeaderCollection(RequestHeaderCollection $requestHeaderCollection): self { |
| 167 | return new self($this->requestExecutor, $this->apiUriFactory, $this->authState, $requestHeaderCollection); |
| 168 | } |
| 169 | } |
| 170 |