give
/
vendor
/
vendor-prefixed
/
stellarwp
/
licensing-api-client
/
src
/
Resources
/
LicensesResource.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
LicensesResource.php
472 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Give\Vendors\LiquidWeb\LicensingApiClient\Resources; |
| 4 | |
| 5 | use Generator; |
| 6 | use JsonException; |
| 7 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\Contracts\ApiErrorExceptionInterface; |
| 8 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\MissingAuthenticationException; |
| 9 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\UnexpectedResponseException; |
| 10 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\AuthState; |
| 11 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\Factories\ApiUriFactory; |
| 12 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\RequestExecutor; |
| 13 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\RequestHeaderCollection; |
| 14 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\License\Activate as ActivateRequest; |
| 15 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\License\Alias\ImportAliases as ImportAliasesRequest; |
| 16 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\License\Alias\RemoveAliases as RemoveAliasesRequest; |
| 17 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\License\Deactivate as DeactivateRequest; |
| 18 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\License\DeleteActivation as DeleteActivationRequest; |
| 19 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\License\LicenseReference; |
| 20 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\License\Listing\ListRequest; |
| 21 | use Give\Vendors\LiquidWeb\LicensingApiClient\Requests\License\RegenerateKey as RegenerateKeyRequest; |
| 22 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Concerns\RebindsAuthState; |
| 23 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Concerns\RebindsRequestHeaderCollection; |
| 24 | use Give\Vendors\LiquidWeb\LicensingApiClient\Resources\Contracts\LicensesResourceInterface; |
| 25 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\Activate; |
| 26 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\Alias\ImportAliases; |
| 27 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\Alias\RemoveAliases; |
| 28 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\Deactivate; |
| 29 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\DeleteActivation; |
| 30 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\Listing\Listing; |
| 31 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\RegenerateKey; |
| 32 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\StatusChange; |
| 33 | use Give\Vendors\LiquidWeb\LicensingApiClient\Responses\License\Validate; |
| 34 | use Give\Vendors\Psr\Http\Client\ClientExceptionInterface; |
| 35 | |
| 36 | /** |
| 37 | * Provides operations for the licenses API resource. |
| 38 | * |
| 39 | * @phpstan-type ActivationDomainPayload array{ |
| 40 | * activated_at: string, |
| 41 | * deactivated_at: string|null, |
| 42 | * is_active: bool, |
| 43 | * is_production: bool |
| 44 | * } |
| 45 | * @phpstan-type ActivationDomainsPayload array<string, ActivationDomainPayload> |
| 46 | * @phpstan-type ValidatePayload array{ |
| 47 | * license: array{license_key: string, status: string}|null, |
| 48 | * domain: string, |
| 49 | * is_production: bool, |
| 50 | * products: list<array{ |
| 51 | * product_slug: string, |
| 52 | * status: string, |
| 53 | * is_valid: bool, |
| 54 | * entitlement: array{ |
| 55 | * tier: string, |
| 56 | * site_limit: int, |
| 57 | * active_count: int, |
| 58 | * available: int, |
| 59 | * over_limit: bool, |
| 60 | * excess_activations: int, |
| 61 | * expiration_date: string, |
| 62 | * status: string, |
| 63 | * capabilities: list<string> |
| 64 | * }|null, |
| 65 | * activation: array{ |
| 66 | * domain: string, |
| 67 | * activated_at: string |
| 68 | * }|null, |
| 69 | * available_entitlements?: list<array{ |
| 70 | * tier: string, |
| 71 | * site_limit: int, |
| 72 | * active_count: int, |
| 73 | * available: int, |
| 74 | * over_limit: bool, |
| 75 | * excess_activations: int, |
| 76 | * capabilities: list<string>, |
| 77 | * status: string, |
| 78 | * expires: string |
| 79 | * }> |
| 80 | * }> |
| 81 | * } |
| 82 | * @phpstan-type ListingPayload array{ |
| 83 | * licenses: list<array{ |
| 84 | * license_key: string, |
| 85 | * identity_id: string, |
| 86 | * status: string, |
| 87 | * created_at: string, |
| 88 | * updated_at: string, |
| 89 | * products: list<array{ |
| 90 | * product_slug: string, |
| 91 | * tier: string, |
| 92 | * status: string, |
| 93 | * expires: string, |
| 94 | * capabilities: list<string>, |
| 95 | * activations: array{ |
| 96 | * site_limit: int, |
| 97 | * active_count: int, |
| 98 | * over_limit: bool, |
| 99 | * excess_activations: int, |
| 100 | * domains: ActivationDomainsPayload |
| 101 | * } |
| 102 | * }>, |
| 103 | * aliases: list<array{alias_key: string, product_slug: string|null}> |
| 104 | * }>, |
| 105 | * links: array{ |
| 106 | * first: string, |
| 107 | * last: string|null, |
| 108 | * prev: string|null, |
| 109 | * next: string|null |
| 110 | * }, |
| 111 | * meta: array{ |
| 112 | * page: array{ |
| 113 | * total: int, |
| 114 | * limit: int, |
| 115 | * max_size: int |
| 116 | * } |
| 117 | * } |
| 118 | * } |
| 119 | * @phpstan-import-type ActivatePayload from ActivateRequest |
| 120 | * @phpstan-import-type DeactivatePayload from DeactivateRequest |
| 121 | * @phpstan-import-type DeleteActivationPayload from DeleteActivationRequest |
| 122 | * @phpstan-import-type LicenseReferencePayload from LicenseReference |
| 123 | * @phpstan-import-type RegenerateKeyPayload from RegenerateKeyRequest |
| 124 | * @phpstan-import-type ImportAliasesPayload from ImportAliasesRequest |
| 125 | * @phpstan-import-type RemoveAliasesPayload from RemoveAliasesRequest |
| 126 | * @phpstan-type ActivateResponsePayload array{ |
| 127 | * status: string, |
| 128 | * is_valid: bool, |
| 129 | * is_production: bool, |
| 130 | * license: array{license_key: string, status: string}|null, |
| 131 | * entitlement: array{ |
| 132 | * product_slug: string, |
| 133 | * tier: string, |
| 134 | * site_limit: int, |
| 135 | * active_count: int, |
| 136 | * available: int, |
| 137 | * over_limit: bool, |
| 138 | * excess_activations: int, |
| 139 | * expiration_date: string, |
| 140 | * status: string, |
| 141 | * capabilities: list<string> |
| 142 | * }|null, |
| 143 | * activation: array{ |
| 144 | * domain: string, |
| 145 | * activated_at: string |
| 146 | * }|null |
| 147 | * } |
| 148 | * @phpstan-type DeactivateResponsePayload array{deactivated: bool} |
| 149 | * @phpstan-type DeleteActivationResponsePayload array{deleted: bool} |
| 150 | * @phpstan-type StatusChangePayload array{license_key: string, status: string} |
| 151 | * @phpstan-type RegenerateKeyResponsePayload array{license_key: string} |
| 152 | * @phpstan-type ImportAliasesResponsePayload array{ |
| 153 | * imported: list<array{alias_key: string, product_slug?: string|null}> |
| 154 | * } |
| 155 | * @phpstan-type RemoveAliasesResponsePayload array{removed: int} |
| 156 | */ |
| 157 | final class LicensesResource implements LicensesResourceInterface |
| 158 | { |
| 159 | use RebindsAuthState; |
| 160 | use RebindsRequestHeaderCollection; |
| 161 | |
| 162 | private RequestExecutor $requestExecutor; |
| 163 | |
| 164 | private ApiUriFactory $apiUriFactory; |
| 165 | |
| 166 | private AuthState $authState; |
| 167 | |
| 168 | private RequestHeaderCollection $requestHeaderCollection; |
| 169 | |
| 170 | public function __construct( |
| 171 | RequestExecutor $requestExecutor, |
| 172 | ApiUriFactory $apiUriFactory, |
| 173 | AuthState $authState, |
| 174 | RequestHeaderCollection $requestHeaderCollection |
| 175 | ) { |
| 176 | $this->requestExecutor = $requestExecutor; |
| 177 | $this->apiUriFactory = $apiUriFactory; |
| 178 | $this->authState = $authState; |
| 179 | $this->requestHeaderCollection = $requestHeaderCollection; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @throws ApiErrorExceptionInterface |
| 184 | * @throws MissingAuthenticationException |
| 185 | * @throws UnexpectedResponseException |
| 186 | * @throws ClientExceptionInterface |
| 187 | * @throws JsonException |
| 188 | */ |
| 189 | public function activate(ActivateRequest $request): Activate { |
| 190 | /** @var ActivatePayload $body */ |
| 191 | $body = $request->toArray(); |
| 192 | |
| 193 | $result = $this->requestExecutor->executeJson( |
| 194 | 'POST', |
| 195 | $this->apiUriFactory->make('/licenses/activate'), |
| 196 | [], |
| 197 | $body, |
| 198 | $this->authState->requiredToken(), |
| 199 | $this->requestHeaderCollection->all() |
| 200 | ); |
| 201 | |
| 202 | /** @var ActivateResponsePayload $result */ |
| 203 | return Activate::from($result); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @throws ApiErrorExceptionInterface |
| 208 | * @throws MissingAuthenticationException |
| 209 | * @throws UnexpectedResponseException |
| 210 | * @throws ClientExceptionInterface |
| 211 | * @throws JsonException |
| 212 | */ |
| 213 | public function deactivate(DeactivateRequest $request): Deactivate { |
| 214 | /** @var DeactivatePayload $body */ |
| 215 | $body = $request->toArray(); |
| 216 | |
| 217 | $result = $this->requestExecutor->executeJson( |
| 218 | 'POST', |
| 219 | $this->apiUriFactory->make('/licenses/deactivate'), |
| 220 | [], |
| 221 | $body, |
| 222 | $this->authState->requiredToken(), |
| 223 | $this->requestHeaderCollection->all() |
| 224 | ); |
| 225 | |
| 226 | /** @var DeactivateResponsePayload $result */ |
| 227 | return Deactivate::from($result); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @throws ApiErrorExceptionInterface |
| 232 | * @throws MissingAuthenticationException |
| 233 | * @throws UnexpectedResponseException |
| 234 | * @throws ClientExceptionInterface |
| 235 | * @throws JsonException |
| 236 | */ |
| 237 | public function deleteActivation(DeleteActivationRequest $request): DeleteActivation { |
| 238 | /** @var DeleteActivationPayload $body */ |
| 239 | $body = $request->toArray(); |
| 240 | |
| 241 | $result = $this->requestExecutor->executeJson( |
| 242 | 'DELETE', |
| 243 | $this->apiUriFactory->make('/licenses/activation'), |
| 244 | [], |
| 245 | $body, |
| 246 | $this->authState->requiredToken(), |
| 247 | $this->requestHeaderCollection->all() |
| 248 | ); |
| 249 | |
| 250 | /** @var DeleteActivationResponsePayload $result */ |
| 251 | return DeleteActivation::from($result); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @throws ApiErrorExceptionInterface |
| 256 | * @throws MissingAuthenticationException |
| 257 | * @throws UnexpectedResponseException |
| 258 | * @throws ClientExceptionInterface |
| 259 | * @throws JsonException |
| 260 | */ |
| 261 | public function list(ListRequest $request): Listing { |
| 262 | $result = $this->requestExecutor->executeJson( |
| 263 | 'GET', |
| 264 | $this->apiUriFactory->make('/licenses'), |
| 265 | $request->toQuery(), |
| 266 | null, |
| 267 | $this->authState->requiredToken(), |
| 268 | $this->requestHeaderCollection->all() |
| 269 | ); |
| 270 | |
| 271 | /** @var ListingPayload $result */ |
| 272 | return Listing::from($result); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @throws ApiErrorExceptionInterface |
| 277 | * @throws MissingAuthenticationException |
| 278 | * @throws UnexpectedResponseException |
| 279 | * @throws ClientExceptionInterface |
| 280 | * @throws JsonException |
| 281 | * |
| 282 | * @return Generator<int, Listing, mixed, void> |
| 283 | */ |
| 284 | public function pages(ListRequest $request): Generator { |
| 285 | $page = $this->list($request); |
| 286 | |
| 287 | while (true) { |
| 288 | yield $page; |
| 289 | |
| 290 | if ($page->links->next === null) { |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | $result = $this->requestExecutor->executeJson( |
| 295 | 'GET', |
| 296 | $this->apiUriFactory->fromPaginationLink($page->links->next), |
| 297 | [], |
| 298 | null, |
| 299 | $this->authState->requiredToken(), |
| 300 | $this->requestHeaderCollection->all() |
| 301 | ); |
| 302 | |
| 303 | /** @var ListingPayload $result */ |
| 304 | $page = Listing::from($result); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @param list<string> $productSlugs |
| 310 | * |
| 311 | * @throws ApiErrorExceptionInterface |
| 312 | * @throws MissingAuthenticationException |
| 313 | * @throws UnexpectedResponseException |
| 314 | * @throws ClientExceptionInterface |
| 315 | * @throws JsonException |
| 316 | */ |
| 317 | public function validate(string $licenseKey, array $productSlugs, string $domain): Validate { |
| 318 | $result = $this->requestExecutor->executeJson( |
| 319 | 'GET', |
| 320 | $this->apiUriFactory->make('/licenses/validate'), |
| 321 | [ |
| 322 | 'license_key' => $licenseKey, |
| 323 | 'product_slugs' => $productSlugs, |
| 324 | 'domain' => $domain, |
| 325 | ], |
| 326 | null, |
| 327 | $this->authState->optionalToken(), |
| 328 | $this->requestHeaderCollection->all() |
| 329 | ); |
| 330 | |
| 331 | /** @var ValidatePayload $result */ |
| 332 | return Validate::from($result); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @throws ApiErrorExceptionInterface |
| 337 | * @throws MissingAuthenticationException |
| 338 | * @throws UnexpectedResponseException |
| 339 | * @throws ClientExceptionInterface |
| 340 | * @throws JsonException |
| 341 | */ |
| 342 | public function suspend(LicenseReference $request): StatusChange { |
| 343 | return $this->changeLicenseStatus('/licenses/suspend', $request); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * @throws ApiErrorExceptionInterface |
| 348 | * @throws MissingAuthenticationException |
| 349 | * @throws UnexpectedResponseException |
| 350 | * @throws ClientExceptionInterface |
| 351 | * @throws JsonException |
| 352 | */ |
| 353 | public function reinstate(LicenseReference $request): StatusChange { |
| 354 | return $this->changeLicenseStatus('/licenses/reinstate', $request); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * @throws ApiErrorExceptionInterface |
| 359 | * @throws MissingAuthenticationException |
| 360 | * @throws UnexpectedResponseException |
| 361 | * @throws ClientExceptionInterface |
| 362 | * @throws JsonException |
| 363 | */ |
| 364 | public function ban(LicenseReference $request): StatusChange { |
| 365 | return $this->changeLicenseStatus('/licenses/ban', $request); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * @throws ApiErrorExceptionInterface |
| 370 | * @throws MissingAuthenticationException |
| 371 | * @throws UnexpectedResponseException |
| 372 | * @throws ClientExceptionInterface |
| 373 | * @throws JsonException |
| 374 | */ |
| 375 | public function regenerateKey(RegenerateKeyRequest $request): RegenerateKey { |
| 376 | /** @var RegenerateKeyPayload $body */ |
| 377 | $body = $request->toArray(); |
| 378 | |
| 379 | $result = $this->requestExecutor->executeJson( |
| 380 | 'POST', |
| 381 | $this->apiUriFactory->make('/licenses/regenerate-key'), |
| 382 | [], |
| 383 | $body, |
| 384 | $this->authState->requiredToken(), |
| 385 | $this->requestHeaderCollection->all() |
| 386 | ); |
| 387 | |
| 388 | /** @var RegenerateKeyResponsePayload $result */ |
| 389 | return RegenerateKey::from($result); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * @throws ApiErrorExceptionInterface |
| 394 | * @throws MissingAuthenticationException |
| 395 | * @throws UnexpectedResponseException |
| 396 | * @throws ClientExceptionInterface |
| 397 | * @throws JsonException |
| 398 | */ |
| 399 | public function importAliases(ImportAliasesRequest $request): ImportAliases { |
| 400 | /** @var ImportAliasesPayload $body */ |
| 401 | $body = $request->toArray(); |
| 402 | |
| 403 | $result = $this->requestExecutor->executeJson( |
| 404 | 'POST', |
| 405 | $this->apiUriFactory->make('/licenses/aliases'), |
| 406 | [], |
| 407 | $body, |
| 408 | $this->authState->requiredToken(), |
| 409 | $this->requestHeaderCollection->all() |
| 410 | ); |
| 411 | |
| 412 | /** @var ImportAliasesResponsePayload $result */ |
| 413 | return ImportAliases::from($result); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * @throws ApiErrorExceptionInterface |
| 418 | * @throws MissingAuthenticationException |
| 419 | * @throws UnexpectedResponseException |
| 420 | * @throws ClientExceptionInterface |
| 421 | * @throws JsonException |
| 422 | */ |
| 423 | public function removeAliases(RemoveAliasesRequest $request): RemoveAliases { |
| 424 | /** @var RemoveAliasesPayload $body */ |
| 425 | $body = $request->toArray(); |
| 426 | |
| 427 | $result = $this->requestExecutor->executeJson( |
| 428 | 'DELETE', |
| 429 | $this->apiUriFactory->make('/licenses/aliases'), |
| 430 | [], |
| 431 | $body, |
| 432 | $this->authState->requiredToken(), |
| 433 | $this->requestHeaderCollection->all() |
| 434 | ); |
| 435 | |
| 436 | /** @var RemoveAliasesResponsePayload $result */ |
| 437 | return RemoveAliases::from($result); |
| 438 | } |
| 439 | |
| 440 | protected function rebindWithAuthState(AuthState $authState): self { |
| 441 | return new self($this->requestExecutor, $this->apiUriFactory, $authState, $this->requestHeaderCollection); |
| 442 | } |
| 443 | |
| 444 | protected function rebindWithRequestHeaderCollection(RequestHeaderCollection $requestHeaderCollection): self { |
| 445 | return new self($this->requestExecutor, $this->apiUriFactory, $this->authState, $requestHeaderCollection); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * @throws ApiErrorExceptionInterface |
| 450 | * @throws MissingAuthenticationException |
| 451 | * @throws UnexpectedResponseException |
| 452 | * @throws ClientExceptionInterface |
| 453 | * @throws JsonException |
| 454 | */ |
| 455 | private function changeLicenseStatus(string $path, LicenseReference $request): StatusChange { |
| 456 | /** @var LicenseReferencePayload $body */ |
| 457 | $body = $request->toArray(); |
| 458 | |
| 459 | $result = $this->requestExecutor->executeJson( |
| 460 | 'POST', |
| 461 | $this->apiUriFactory->make($path), |
| 462 | [], |
| 463 | $body, |
| 464 | $this->authState->requiredToken(), |
| 465 | $this->requestHeaderCollection->all() |
| 466 | ); |
| 467 | |
| 468 | /** @var StatusChangePayload $result */ |
| 469 | return StatusChange::from($result); |
| 470 | } |
| 471 | } |
| 472 |