give
/
vendor
/
vendor-prefixed
/
stellarwp
/
licensing-api-client
/
src
/
Resources
/
Credit
/
CreditsPoolsResource.php
give
/
vendor
/
vendor-prefixed
/
stellarwp
/
licensing-api-client
/
src
/
Resources
/
Credit
Last commit date
CreditsLedgerResource.php
3 months ago
CreditsPoolsResource.php
3 months ago
CreditsQuotasResource.php
3 months ago
CreditsResource.php
3 months ago
CreditsPoolsResource.php
179 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Credit; |
| 4 | |
| 5 | use JsonException; |
| 6 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\ApiResponseException; |
| 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\Credit\CreatePool; |
| 14 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\Credit\DeletePool as DeletePoolRequest; |
| 15 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\Credit\UpdatePool; |
| 16 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Concerns\RebindsAuthState; |
| 17 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Concerns\RebindsRequestHeaderCollection; |
| 18 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Contracts\CreditsPoolsResourceInterface; |
| 19 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\Credit\DeletePool; |
| 20 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\Credit\PoolCollection; |
| 21 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\Credit\ValueObjects\CreditPool; |
| 22 | use Give\Vendors\Psr\Http\Client\ClientExceptionInterface; |
| 23 | |
| 24 | /** |
| 25 | * Provides operations for the credits pools API resource. |
| 26 | * |
| 27 | * @phpstan-import-type CreatePoolPayload from CreatePool |
| 28 | * @phpstan-import-type UpdatePoolPayload from UpdatePool |
| 29 | * @phpstan-import-type DeletePoolPayload from DeletePoolRequest |
| 30 | * @phpstan-type PoolPayload array{ |
| 31 | * pool_id: int, |
| 32 | * credit_type: string, |
| 33 | * credits_total: int, |
| 34 | * credits_used: int, |
| 35 | * overage_limit: ?int, |
| 36 | * priority: int, |
| 37 | * period: string, |
| 38 | * first_period_start: ?string, |
| 39 | * expires_at: ?string, |
| 40 | * is_expired: bool |
| 41 | * } |
| 42 | * @phpstan-type PoolCollectionPayload array{ |
| 43 | * pools: array<int|string, PoolPayload> |
| 44 | * } |
| 45 | * @phpstan-type DeletePoolResponsePayload array{ |
| 46 | * deleted: bool, |
| 47 | * pool_id: int |
| 48 | * } |
| 49 | */ |
| 50 | final class CreditsPoolsResource implements CreditsPoolsResourceInterface |
| 51 | { |
| 52 | use RebindsAuthState; |
| 53 | use RebindsRequestHeaderCollection; |
| 54 | |
| 55 | private RequestExecutor $requestExecutor; |
| 56 | |
| 57 | private ApiUriFactory $apiUriFactory; |
| 58 | |
| 59 | private AuthState $authState; |
| 60 | |
| 61 | private RequestHeaderCollection $requestHeaderCollection; |
| 62 | |
| 63 | public function __construct( |
| 64 | RequestExecutor $requestExecutor, |
| 65 | ApiUriFactory $apiUriFactory, |
| 66 | AuthState $authState, |
| 67 | RequestHeaderCollection $requestHeaderCollection |
| 68 | ) { |
| 69 | $this->requestExecutor = $requestExecutor; |
| 70 | $this->apiUriFactory = $apiUriFactory; |
| 71 | $this->authState = $authState; |
| 72 | $this->requestHeaderCollection = $requestHeaderCollection; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @throws ApiResponseException |
| 77 | * @throws MissingAuthenticationException |
| 78 | * @throws UnexpectedResponseException |
| 79 | * @throws ClientExceptionInterface |
| 80 | * @throws JsonException |
| 81 | */ |
| 82 | public function list(string $licenseKey, bool $active = false): PoolCollection { |
| 83 | $result = $this->requestExecutor->executeJson( |
| 84 | 'GET', |
| 85 | $this->apiUriFactory->make('/credits/pools'), |
| 86 | [ |
| 87 | 'license_key' => $licenseKey, |
| 88 | 'active' => $active, |
| 89 | ], |
| 90 | null, |
| 91 | $this->authState->requiredToken(), |
| 92 | $this->requestHeaderCollection->all() |
| 93 | ); |
| 94 | |
| 95 | /** @var PoolCollectionPayload $result */ |
| 96 | return PoolCollection::from($result); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @throws ApiResponseException |
| 101 | * @throws MissingAuthenticationException |
| 102 | * @throws UnexpectedResponseException |
| 103 | * @throws ClientExceptionInterface |
| 104 | * @throws JsonException |
| 105 | */ |
| 106 | public function create(CreatePool $request): CreditPool { |
| 107 | /** @var CreatePoolPayload $body */ |
| 108 | $body = $request->toArray(); |
| 109 | |
| 110 | $result = $this->requestExecutor->executeJson( |
| 111 | 'POST', |
| 112 | $this->apiUriFactory->make('/credits/pools'), |
| 113 | [], |
| 114 | $body, |
| 115 | $this->authState->requiredToken(), |
| 116 | $this->requestHeaderCollection->all() |
| 117 | ); |
| 118 | |
| 119 | /** @var PoolPayload $result */ |
| 120 | return CreditPool::from($result); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @throws ApiResponseException |
| 125 | * @throws MissingAuthenticationException |
| 126 | * @throws UnexpectedResponseException |
| 127 | * @throws ClientExceptionInterface |
| 128 | * @throws JsonException |
| 129 | */ |
| 130 | public function update(UpdatePool $request): CreditPool { |
| 131 | /** @var UpdatePoolPayload $body */ |
| 132 | $body = $request->toArray(); |
| 133 | |
| 134 | $result = $this->requestExecutor->executeJson( |
| 135 | 'PATCH', |
| 136 | $this->apiUriFactory->make('/credits/pools'), |
| 137 | [], |
| 138 | $body, |
| 139 | $this->authState->requiredToken(), |
| 140 | $this->requestHeaderCollection->all() |
| 141 | ); |
| 142 | |
| 143 | /** @var PoolPayload $result */ |
| 144 | return CreditPool::from($result); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @throws ApiResponseException |
| 149 | * @throws MissingAuthenticationException |
| 150 | * @throws UnexpectedResponseException |
| 151 | * @throws ClientExceptionInterface |
| 152 | * @throws JsonException |
| 153 | */ |
| 154 | public function delete(DeletePoolRequest $request): DeletePool { |
| 155 | /** @var DeletePoolPayload $body */ |
| 156 | $body = $request->toArray(); |
| 157 | |
| 158 | $result = $this->requestExecutor->executeJson( |
| 159 | 'DELETE', |
| 160 | $this->apiUriFactory->make('/credits/pools'), |
| 161 | [], |
| 162 | $body, |
| 163 | $this->authState->requiredToken(), |
| 164 | $this->requestHeaderCollection->all() |
| 165 | ); |
| 166 | |
| 167 | /** @var DeletePoolResponsePayload $result */ |
| 168 | return DeletePool::from($result); |
| 169 | } |
| 170 | |
| 171 | protected function rebindWithAuthState(AuthState $authState): self { |
| 172 | return new self($this->requestExecutor, $this->apiUriFactory, $authState, $this->requestHeaderCollection); |
| 173 | } |
| 174 | |
| 175 | protected function rebindWithRequestHeaderCollection(RequestHeaderCollection $requestHeaderCollection): self { |
| 176 | return new self($this->requestExecutor, $this->apiUriFactory, $this->authState, $requestHeaderCollection); |
| 177 | } |
| 178 | } |
| 179 |