PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / apimatic / core / src / Response / Context.php
ameliabooking / vendor / apimatic / core / src / Response Last commit date
Types 1 year ago Context.php 1 year ago ResponseError.php 1 year ago ResponseHandler.php 1 year ago
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