1 | <?php |
||
16 | abstract class HttpApi |
||
17 | { |
||
18 | /** |
||
19 | * @var HttpClient |
||
20 | */ |
||
21 | protected $httpClient; |
||
22 | |||
23 | /** |
||
24 | * @var Hydrator |
||
25 | */ |
||
26 | protected $hydrator; |
||
27 | |||
28 | /** |
||
29 | * @var MessageFactory |
||
30 | */ |
||
31 | protected $messageFactory; |
||
32 | |||
33 | /** |
||
34 | * @param HttpClient $httpClient |
||
35 | * @param MessageFactory $messageFactory |
||
36 | * @param Hydrator $hydrator |
||
37 | */ |
||
38 | 467 | public function __construct( |
|
47 | |||
48 | /** |
||
49 | * Send a GET request with query parameters. |
||
50 | * |
||
51 | * @param string $path Request path |
||
52 | * @param array $params GET parameters |
||
53 | * @param array $requestHeaders Request Headers |
||
54 | * |
||
55 | * @return ResponseInterface |
||
56 | */ |
||
57 | 162 | protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
67 | |||
68 | /** |
||
69 | * Send a POST request with JSON-encoded parameters. |
||
70 | * |
||
71 | * @param string $path Request path |
||
72 | * @param array $params POST parameters to be JSON encoded |
||
73 | * @param array $pathParams Params added to the path, as query parameters |
||
74 | * @param array $requestHeaders Request headers |
||
75 | * |
||
76 | * @return ResponseInterface |
||
77 | */ |
||
78 | 96 | protected function httpPost(string $path, array $params = [], array $pathParams = [], array $requestHeaders = []): ResponseInterface |
|
85 | |||
86 | /** |
||
87 | * Send a POST request with raw data. |
||
88 | * |
||
89 | * @param string $path Request path |
||
90 | * @param array|string $body Request body |
||
91 | * @param array $requestHeaders Request headers |
||
92 | * |
||
93 | * @return ResponseInterface |
||
94 | */ |
||
95 | 96 | protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface |
|
101 | |||
102 | /** |
||
103 | * Send a PUT request with JSON-encoded parameters. |
||
104 | * |
||
105 | * @param string $path Request path |
||
106 | * @param array $params POST parameters to be JSON encoded |
||
107 | * @param array $requestHeaders Request headers |
||
108 | * |
||
109 | * @return ResponseInterface |
||
110 | */ |
||
111 | 88 | protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
117 | |||
118 | /** |
||
119 | * Send a DELETE request with JSON-encoded parameters. |
||
120 | * |
||
121 | * @param string $path Request path |
||
122 | * @param array $params POST parameters to be JSON encoded |
||
123 | * @param array $requestHeaders Request headers |
||
124 | * |
||
125 | * @return ResponseInterface |
||
126 | */ |
||
127 | 49 | protected function httpDelete(string $path, array $params = [], array $pathParams = [], array $requestHeaders = []): ResponseInterface |
|
135 | |||
136 | /** |
||
137 | * Handle responses from the endpoint: handle errors and hydrations. |
||
138 | * |
||
139 | * @param ResponseInterface $response The request response |
||
140 | * @param string $class The Class to hydrate the response |
||
141 | * |
||
142 | * @return mixed Hydration return data |
||
143 | */ |
||
144 | 371 | protected function handleResponse(ResponseInterface $response, $class) |
|
152 | |||
153 | /** |
||
154 | * Handle HTTP errors. |
||
155 | * |
||
156 | * Call is controlled by the specific API methods. |
||
157 | * |
||
158 | * @param ResponseInterface $response |
||
159 | * |
||
160 | * @throws GenericApiException |
||
161 | */ |
||
162 | 357 | protected function handleErrors(ResponseInterface $response) |
|
163 | { |
||
164 | 357 | $error = null; |
|
165 | // We only hydrate the Error response if the hydrator is a Model one |
||
166 | 357 | if ($this->hydrator instanceof ModelHydrator) { |
|
167 | 7 | $error = $this->hydrator->hydrate($response, Error::class); |
|
168 | } |
||
169 | |||
170 | 357 | switch ($response->getStatusCode()) { |
|
171 | 357 | case 400: |
|
172 | 51 | throw new DomainExceptions\ValidationException($response, $error); |
|
173 | 306 | case 401: |
|
174 | 51 | throw new DomainExceptions\MissingAccessTokenException($response, $error); |
|
175 | 255 | case 403: |
|
176 | 51 | throw new DomainExceptions\PermissionDeniedException($response, $error); |
|
177 | 204 | case 404: |
|
178 | 51 | throw new DomainExceptions\NotFoundException($response, $error); |
|
179 | 153 | case 501: |
|
180 | 51 | throw new DomainExceptions\DisabledFeatureException($response, $error); |
|
181 | default: |
||
182 | 102 | throw new GenericApiException($response, $error); |
|
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Create a JSON encoded version of an array of parameters. |
||
188 | * |
||
189 | * @param array $params Request parameters |
||
190 | * |
||
191 | * @return null|string |
||
192 | */ |
||
193 | 233 | private function createJsonBody(array $params) |
|
197 | |||
198 | /** |
||
199 | * Builds the Query string given the parameters, null if no parameters are provided. |
||
200 | * |
||
201 | * @param array $params |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | 145 | private function buildPathParams(array $params) |
|
211 | } |
||
212 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.