Context.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Core\Response; |
| 6 | |
| 7 | use Core\Client; |
| 8 | use Core\Utils\JsonHelper; |
| 9 | use CoreInterfaces\Core\ContextInterface; |
| 10 | use CoreInterfaces\Core\Request\RequestInterface; |
| 11 | use CoreInterfaces\Core\Response\ResponseInterface; |
| 12 | |
| 13 | class Context implements ContextInterface |
| 14 | { |
| 15 | private $request; |
| 16 | private $response; |
| 17 | private $converter; |
| 18 | private $jsonHelper; |
| 19 | |
| 20 | /** |
| 21 | * Initializes a new Context with the request, response, jsonHelper and the converter set. |
| 22 | */ |
| 23 | public function __construct(RequestInterface $request, ResponseInterface $response, Client $client) |
| 24 | { |
| 25 | $this->request = $request; |
| 26 | $this->response = $response; |
| 27 | $this->converter = Client::getConverter($client); |
| 28 | $this->jsonHelper = Client::getJsonHelper($client); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Returns Request object. |
| 33 | */ |
| 34 | public function getRequest(): RequestInterface |
| 35 | { |
| 36 | return $this->request; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns Response object. |
| 41 | */ |
| 42 | public function getResponse(): ResponseInterface |
| 43 | { |
| 44 | return $this->response; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns Response body as a scalar or an associative array. |
| 49 | */ |
| 50 | public function getResponseBody() |
| 51 | { |
| 52 | $responseBody = $this->response->getBody(); |
| 53 | if (is_object($responseBody)) { |
| 54 | return (array) $responseBody; |
| 55 | } |
| 56 | return $responseBody; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Is successful response. |
| 61 | */ |
| 62 | public function isFailure(): bool |
| 63 | { |
| 64 | $statusCode = $this->response->getStatusCode(); |
| 65 | return $statusCode !== min(max($statusCode, 200), 208); // [200,208] = HTTP OK |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Is response body missing. |
| 70 | */ |
| 71 | public function isBodyMissing(): bool |
| 72 | { |
| 73 | $rawBody = $this->response->getRawBody(); |
| 74 | return trim($rawBody) === ''; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns JsonHelper object. |
| 79 | */ |
| 80 | public function getJsonHelper(): JsonHelper |
| 81 | { |
| 82 | return $this->jsonHelper; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns an ApiException with errorMessage and childClass set, if not null. |
| 87 | */ |
| 88 | public function toApiException(string $errorMessage, ?string $childClass = null) |
| 89 | { |
| 90 | $responseBody = $this->response->getBody(); |
| 91 | if (is_null($childClass)) { |
| 92 | return $this->converter->createApiException($errorMessage, $this->request, $this->response); |
| 93 | } |
| 94 | if (!is_object($responseBody)) { |
| 95 | return $this->converter->createApiException($errorMessage, $this->request, $this->response); |
| 96 | } |
| 97 | $responseBody->reason = $errorMessage; |
| 98 | $responseBody->request = $this->request->convert(); |
| 99 | $responseBody->response = $this->response->convert($this->converter); |
| 100 | return $this->jsonHelper->mapClass($responseBody, $childClass); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns a MockApiResponse object from the context and the deserializedBody provided. |
| 105 | */ |
| 106 | public function toApiResponse($deserializedBody) |
| 107 | { |
| 108 | return $this->converter->createApiResponse($this, $deserializedBody); |
| 109 | } |
| 110 | } |
| 111 |