give
/
vendor
/
vendor-prefixed
/
stellarwp
/
licensing-api-client
/
src
/
Http
/
RequestExecutor.php
RequestExecutor.php
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Give\Vendors\LiquidWeb\LicensingApiClient\Http; |
| 4 | |
| 5 | use JsonException; |
| 6 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\ApiResponseException; |
| 7 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\DecodingException; |
| 8 | use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\UnexpectedResponseException; |
| 9 | use Give\Vendors\LiquidWeb\LicensingApiClient\Http\Factories\ResponseExceptionFactory; |
| 10 | use Give\Vendors\LiquidWeb\LicensingApiClient\Value\AuthToken; |
| 11 | use Give\Vendors\Psr\Http\Client\ClientExceptionInterface; |
| 12 | use Give\Vendors\Psr\Http\Client\ClientInterface as HttpClient; |
| 13 | use Give\Vendors\Psr\Http\Message\ResponseInterface; |
| 14 | |
| 15 | /** |
| 16 | * Executes API requests and delegates response error mapping. |
| 17 | * |
| 18 | * @phpstan-import-type QueryValue from RequestBuilder |
| 19 | * @phpstan-import-type HeaderValue from RequestBuilder |
| 20 | * @phpstan-import-type JsonObject from RequestBuilder |
| 21 | */ |
| 22 | final class RequestExecutor |
| 23 | { |
| 24 | private HttpClient $httpClient; |
| 25 | |
| 26 | private RequestBuilder $requestBuilder; |
| 27 | |
| 28 | private JsonDecoder $jsonDecoder; |
| 29 | |
| 30 | private ResponseExceptionFactory $responseExceptionFactory; |
| 31 | |
| 32 | public function __construct( |
| 33 | HttpClient $httpClient, |
| 34 | RequestBuilder $requestBuilder, |
| 35 | JsonDecoder $jsonDecoder, |
| 36 | ResponseExceptionFactory $responseExceptionFactory |
| 37 | ) { |
| 38 | $this->httpClient = $httpClient; |
| 39 | $this->requestBuilder = $requestBuilder; |
| 40 | $this->jsonDecoder = $jsonDecoder; |
| 41 | $this->responseExceptionFactory = $responseExceptionFactory; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param array<string, QueryValue> $query |
| 46 | * @param JsonObject|null $body |
| 47 | * @param array<string, HeaderValue> $headers |
| 48 | * |
| 49 | * @throws ApiResponseException |
| 50 | * @throws UnexpectedResponseException |
| 51 | * @throws ClientExceptionInterface |
| 52 | * @throws JsonException |
| 53 | */ |
| 54 | public function execute( |
| 55 | string $method, |
| 56 | ApiUri $uri, |
| 57 | array $query = [], |
| 58 | ?array $body = null, |
| 59 | ?AuthToken $token = null, |
| 60 | array $headers = [] |
| 61 | ): ResponseInterface { |
| 62 | $request = $this->requestBuilder->build($method, $uri, $query, $body, $token, $headers); |
| 63 | $response = $this->httpClient->sendRequest($request); |
| 64 | $statusCode = $response->getStatusCode(); |
| 65 | |
| 66 | if ($statusCode >= 200 && $statusCode < 400) { |
| 67 | return $response; |
| 68 | } |
| 69 | |
| 70 | if ($statusCode >= 400) { |
| 71 | throw $this->responseExceptionFactory->make($response); |
| 72 | } |
| 73 | |
| 74 | throw new UnexpectedResponseException('Unexpected response status code.', $statusCode); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param array<string, QueryValue> $query |
| 79 | * @param JsonObject|null $body |
| 80 | * @param array<string, HeaderValue> $headers |
| 81 | * |
| 82 | * @throws ApiResponseException |
| 83 | * @throws UnexpectedResponseException |
| 84 | * @throws ClientExceptionInterface |
| 85 | * @throws JsonException |
| 86 | * |
| 87 | * @return array<array-key, mixed> |
| 88 | */ |
| 89 | public function executeJson( |
| 90 | string $method, |
| 91 | ApiUri $uri, |
| 92 | array $query = [], |
| 93 | ?array $body = null, |
| 94 | ?AuthToken $token = null, |
| 95 | array $headers = [] |
| 96 | ): array { |
| 97 | $response = $this->execute($method, $uri, $query, $body, $token, $headers); |
| 98 | |
| 99 | $body = (string) $response->getBody(); |
| 100 | |
| 101 | try { |
| 102 | return $this->jsonDecoder->decode($body); |
| 103 | } catch (DecodingException $exception) { |
| 104 | throw new UnexpectedResponseException('Unable to decode JSON response.', 0, $exception); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 |