Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
84 / 84 |
|
100.00% |
50 / 50 |
CRAP | |
100.00% |
1 / 1 |
AbstractClient | |
100.00% |
84 / 84 |
|
100.00% |
50 / 50 |
60 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
setUrl | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
appendToUrl | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setMethod | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getMethod | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
resource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRequest | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasRequest | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRequest | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
request | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setResponse | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasResponse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResponse | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getParsedResponse | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
response | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setField | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setFields | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
removeField | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
removeFields | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addRequestHeaders | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addRequestHeader | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasRequestHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasRequestHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRequestHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRequestHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRequestBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createAsJson | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isJson | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createAsXml | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isXml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createUrlEncodedForm | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isUrlEncodedForm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createMultipartForm | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isMultipartForm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addResponseHeaders | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addResponseHeader | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasResponseHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasResponseHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResponseHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResponseHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResponseBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResponseCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
throwError | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
open | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
send | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
reset | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Http\Client; |
15 | |
16 | use Pop\Http\Parser; |
17 | |
18 | /** |
19 | * Abstract HTTP client class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Http |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 4.1.0 |
27 | */ |
28 | abstract class AbstractClient implements ClientInterface |
29 | { |
30 | |
31 | /** |
32 | * URL |
33 | * @var string |
34 | */ |
35 | protected $url = null; |
36 | |
37 | /** |
38 | * Method |
39 | * @var string |
40 | */ |
41 | protected $method = null; |
42 | |
43 | /** |
44 | * Client resource object |
45 | * @var resource |
46 | */ |
47 | protected $resource = null; |
48 | |
49 | /** |
50 | * Client request object |
51 | * @var Request |
52 | */ |
53 | protected $request = null; |
54 | |
55 | /** |
56 | * Client response object |
57 | * @var Response |
58 | */ |
59 | protected $response = null; |
60 | |
61 | /** |
62 | * Constructor |
63 | * |
64 | * Instantiate the client object |
65 | * |
66 | * @param string $url |
67 | * @param string $method |
68 | */ |
69 | public function __construct($url = null, $method = 'GET') |
70 | { |
71 | if (!empty($url)) { |
72 | $this->setUrl($url); |
73 | } |
74 | if (!empty($method)) { |
75 | $this->setMethod($method); |
76 | } |
77 | } |
78 | |
79 | /** |
80 | * Set the URL |
81 | * |
82 | * @param string $url |
83 | * @return AbstractClient |
84 | */ |
85 | public function setUrl($url) |
86 | { |
87 | $this->url = $url; |
88 | return $this; |
89 | } |
90 | |
91 | /** |
92 | * Append to the URL |
93 | * |
94 | * @param string $url |
95 | * @return AbstractClient |
96 | */ |
97 | public function appendToUrl($url) |
98 | { |
99 | $this->url .= $url; |
100 | return $this; |
101 | } |
102 | |
103 | /** |
104 | * Get the URL |
105 | * |
106 | * @return string |
107 | */ |
108 | public function getUrl() |
109 | { |
110 | return $this->url; |
111 | } |
112 | |
113 | /** |
114 | * Set the method |
115 | * |
116 | * @param string $method |
117 | * @param boolean $strict |
118 | * @throws Exception |
119 | * @return AbstractClient |
120 | */ |
121 | public function setMethod($method, $strict = true) |
122 | { |
123 | $method = strtoupper($method); |
124 | |
125 | if ($strict) { |
126 | if (!in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE'])) { |
127 | throw new Exception('Error: That request method is not valid.'); |
128 | } |
129 | } |
130 | |
131 | $this->method = $method; |
132 | return $this; |
133 | } |
134 | |
135 | /** |
136 | * Get the method |
137 | * |
138 | * @return string |
139 | */ |
140 | public function getMethod() |
141 | { |
142 | return $this->method; |
143 | } |
144 | |
145 | /** |
146 | * Determine whether or not resource is available |
147 | * |
148 | * @return boolean |
149 | */ |
150 | public function hasResource() |
151 | { |
152 | return (null !== $this->resource); |
153 | } |
154 | |
155 | /** |
156 | * Get the resource |
157 | * |
158 | * @return resource |
159 | */ |
160 | public function getResource() |
161 | { |
162 | return $this->resource; |
163 | } |
164 | |
165 | /** |
166 | * Get the resource (alias method) |
167 | * |
168 | * @return resource |
169 | */ |
170 | public function resource() |
171 | { |
172 | return $this->resource; |
173 | } |
174 | |
175 | /** |
176 | * Set the request object |
177 | * |
178 | * @param Request $request |
179 | * @return AbstractClient |
180 | */ |
181 | public function setRequest(Request $request) |
182 | { |
183 | $this->request = $request; |
184 | return $this; |
185 | } |
186 | |
187 | /** |
188 | * Has request object |
189 | * |
190 | * @return boolean |
191 | */ |
192 | public function hasRequest() |
193 | { |
194 | return (null !== $this->request); |
195 | } |
196 | |
197 | /** |
198 | * Get the request object |
199 | * |
200 | * @return Request |
201 | */ |
202 | public function getRequest() |
203 | { |
204 | if (null === $this->request) { |
205 | $this->request = new Request(); |
206 | } |
207 | return $this->request; |
208 | } |
209 | |
210 | /** |
211 | * Get the request object (alias method) |
212 | * |
213 | * @return Request |
214 | */ |
215 | public function request() |
216 | { |
217 | return $this->getRequest(); |
218 | } |
219 | |
220 | /** |
221 | * Set the response object |
222 | * |
223 | * @param Response $response |
224 | * @return AbstractClient |
225 | */ |
226 | public function setResponse(Response $response) |
227 | { |
228 | $this->response = $response; |
229 | return $this; |
230 | } |
231 | |
232 | /** |
233 | * Has response object |
234 | * |
235 | * @return boolean |
236 | */ |
237 | public function hasResponse() |
238 | { |
239 | return (null !== $this->response); |
240 | } |
241 | |
242 | /** |
243 | * Get the response object |
244 | * |
245 | * @return Response |
246 | */ |
247 | public function getResponse() |
248 | { |
249 | if (null === $this->response) { |
250 | $this->response = new Response(); |
251 | } |
252 | return $this->response; |
253 | } |
254 | |
255 | /** |
256 | * Get the parsed response |
257 | * |
258 | * @return mixed |
259 | */ |
260 | public function getParsedResponse() |
261 | { |
262 | $parsedResponse = null; |
263 | |
264 | if (($this->hasResponse()) && ($this->getResponse()->hasBody()) && ($this->getResponse()->hasHeader('Content-Type'))) { |
265 | $rawResponse = $this->getResponse()->getBody()->getContent(); |
266 | $contentType = $this->getResponse()->getHeader('Content-Type')->getValue(); |
267 | $contentEncoding = ($this->getResponse()->hasHeader('Content-Encoding')) ? $this->getResponse()->getHeader('Content-Encoding')->getValue() : null; |
268 | $parsedResponse = Parser::parseDataByContentType($rawResponse, $contentType, $contentEncoding); |
269 | } |
270 | |
271 | return $parsedResponse; |
272 | } |
273 | |
274 | /** |
275 | * Get the response object (alias method) |
276 | * |
277 | * @return Response |
278 | */ |
279 | public function response() |
280 | { |
281 | return $this->getResponse(); |
282 | } |
283 | |
284 | /** |
285 | * Set a field |
286 | * |
287 | * @param string $name |
288 | * @param mixed $value |
289 | * @return AbstractClient |
290 | */ |
291 | public function setField($name, $value) |
292 | { |
293 | $this->getRequest()->setField($name, $value); |
294 | return $this; |
295 | } |
296 | |
297 | /** |
298 | * Set all fields |
299 | * |
300 | * @param array $fields |
301 | * @return AbstractClient |
302 | */ |
303 | public function setFields(array $fields) |
304 | { |
305 | $this->getRequest()->setFields($fields); |
306 | return $this; |
307 | } |
308 | |
309 | /** |
310 | * Get a field |
311 | * |
312 | * @param string $name |
313 | * @return mixed |
314 | */ |
315 | public function getField($name) |
316 | { |
317 | return $this->getRequest()->getField($name); |
318 | } |
319 | |
320 | /** |
321 | * Get all fields |
322 | * |
323 | * @return array |
324 | */ |
325 | public function getFields() |
326 | { |
327 | return $this->getRequest()->getFields(); |
328 | } |
329 | |
330 | /** |
331 | * Has fields |
332 | * |
333 | * @return boolean |
334 | */ |
335 | public function hasFields() |
336 | { |
337 | return $this->getRequest()->hasFields(); |
338 | } |
339 | |
340 | /** |
341 | * Has field |
342 | * |
343 | * @param string $name |
344 | * @return boolean |
345 | */ |
346 | public function hasField($name) |
347 | { |
348 | return $this->getRequest()->hasField($name); |
349 | } |
350 | |
351 | /** |
352 | * Remove a field |
353 | * |
354 | * @param string $name |
355 | * @return AbstractClient |
356 | */ |
357 | public function removeField($name) |
358 | { |
359 | $this->getRequest()->removeField($name); |
360 | return $this; |
361 | } |
362 | |
363 | /** |
364 | * Remove all fields |
365 | * |
366 | * @return AbstractClient |
367 | */ |
368 | public function removeFields() |
369 | { |
370 | $this->getRequest()->removeFields(); |
371 | return $this; |
372 | } |
373 | |
374 | /** |
375 | * Add request headers |
376 | * |
377 | * @param array $headers |
378 | * @return AbstractClient |
379 | */ |
380 | public function addRequestHeaders(array $headers) |
381 | { |
382 | $this->getRequest()->addHeaders($headers); |
383 | return $this; |
384 | } |
385 | |
386 | /** |
387 | * Add request header |
388 | * |
389 | * @param string $name |
390 | * @param string $value |
391 | * @return AbstractClient |
392 | */ |
393 | public function addRequestHeader($name, $value) |
394 | { |
395 | $this->getRequest()->addHeader($name, $value); |
396 | return $this; |
397 | } |
398 | |
399 | /** |
400 | * Has request headers |
401 | * |
402 | * @return boolean |
403 | */ |
404 | public function hasRequestHeaders() |
405 | { |
406 | return $this->getRequest()->hasHeaders(); |
407 | } |
408 | |
409 | /** |
410 | * Has request header |
411 | * |
412 | * @param string $name |
413 | * @return boolean |
414 | */ |
415 | public function hasRequestHeader($name) |
416 | { |
417 | return $this->getRequest()->hasHeader($name); |
418 | } |
419 | |
420 | /** |
421 | * Get the request headers |
422 | * |
423 | * @return array |
424 | */ |
425 | public function getRequestHeaders() |
426 | { |
427 | return $this->getRequest()->getHeaders(); |
428 | } |
429 | |
430 | /** |
431 | * Get the request header |
432 | * |
433 | * @param string $name |
434 | * @return mixed |
435 | */ |
436 | public function getRequestHeader($name) |
437 | { |
438 | return $this->getRequest()->getHeader($name); |
439 | } |
440 | |
441 | /** |
442 | * Get the request body |
443 | * |
444 | * @return string |
445 | */ |
446 | public function getRequestBody() |
447 | { |
448 | return $this->getRequest()->getBody(); |
449 | } |
450 | |
451 | /** |
452 | * Create request as JSON |
453 | * |
454 | * @return AbstractClient |
455 | */ |
456 | public function createAsJson() |
457 | { |
458 | $this->getRequest()->createAsJson(); |
459 | return $this; |
460 | } |
461 | |
462 | /** |
463 | * Check if request is JSON |
464 | * |
465 | * @return boolean |
466 | */ |
467 | public function isJson() |
468 | { |
469 | return $this->getRequest()->isJson(); |
470 | } |
471 | |
472 | /** |
473 | * Create request as XML |
474 | * |
475 | * @return AbstractClient |
476 | */ |
477 | public function createAsXml() |
478 | { |
479 | $this->getRequest()->createAsXml(); |
480 | return $this; |
481 | } |
482 | |
483 | /** |
484 | * Check if request is XML |
485 | * |
486 | * @return boolean |
487 | */ |
488 | public function isXml() |
489 | { |
490 | return $this->getRequest()->isXml(); |
491 | } |
492 | |
493 | /** |
494 | * Create request as a URL-encoded form |
495 | * |
496 | * @return AbstractClient |
497 | */ |
498 | public function createUrlEncodedForm() |
499 | { |
500 | $this->getRequest()->createUrlEncodedForm(); |
501 | return $this; |
502 | } |
503 | |
504 | /** |
505 | * Check if request is a URL-encoded form |
506 | * |
507 | * @return boolean |
508 | */ |
509 | public function isUrlEncodedForm() |
510 | { |
511 | return $this->getRequest()->isUrlEncodedForm(); |
512 | } |
513 | |
514 | /** |
515 | * Create request as a multipart form |
516 | * |
517 | * @return AbstractClient |
518 | */ |
519 | public function createMultipartForm() |
520 | { |
521 | $this->getRequest()->createMultipartForm(); |
522 | return $this; |
523 | } |
524 | |
525 | /** |
526 | * Check if request is a multipart form |
527 | * |
528 | * @return boolean |
529 | */ |
530 | public function isMultipartForm() |
531 | { |
532 | return $this->getRequest()->isMultipartForm(); |
533 | } |
534 | |
535 | /** |
536 | * Add response headers |
537 | * |
538 | * @param array $headers |
539 | * @return AbstractClient |
540 | */ |
541 | public function addResponseHeaders(array $headers) |
542 | { |
543 | $this->getResponse()->addHeaders($headers); |
544 | return $this; |
545 | } |
546 | |
547 | /** |
548 | * Add response header |
549 | * |
550 | * @param string $name |
551 | * @param string $value |
552 | * @return AbstractClient |
553 | */ |
554 | public function addResponseHeader($name, $value) |
555 | { |
556 | $this->getResponse()->addHeader($name, $value); |
557 | return $this; |
558 | } |
559 | |
560 | /** |
561 | * Has response headers |
562 | * |
563 | * @return boolean |
564 | */ |
565 | public function hasResponseHeaders() |
566 | { |
567 | return $this->getResponse()->hasHeaders(); |
568 | } |
569 | |
570 | /** |
571 | * Has response header |
572 | * |
573 | * @param string $name |
574 | * @return boolean |
575 | */ |
576 | public function hasResponseHeader($name) |
577 | { |
578 | return $this->getResponse()->hasHeader($name); |
579 | } |
580 | |
581 | /** |
582 | * Get the response headers |
583 | * |
584 | * @return array |
585 | */ |
586 | public function getResponseHeaders() |
587 | { |
588 | return $this->getResponse()->getHeaders(); |
589 | } |
590 | |
591 | /** |
592 | * Get the response header |
593 | * |
594 | * @param string $name |
595 | * @return mixed |
596 | */ |
597 | public function getResponseHeader($name) |
598 | { |
599 | return $this->getResponse()->getHeader($name); |
600 | } |
601 | |
602 | /** |
603 | * Get the response body |
604 | * |
605 | * @return string |
606 | */ |
607 | public function getResponseBody() |
608 | { |
609 | return $this->getResponse()->getBody(); |
610 | } |
611 | |
612 | /** |
613 | * Get the response code |
614 | * |
615 | * @return string |
616 | */ |
617 | public function getResponseCode() |
618 | { |
619 | return $this->getResponse()->getCode(); |
620 | } |
621 | |
622 | /** |
623 | * Throw an exception upon an error. |
624 | * |
625 | * @param string $error |
626 | * @throws Exception |
627 | * @return void |
628 | */ |
629 | public function throwError($error) |
630 | { |
631 | throw new Exception($error); |
632 | } |
633 | |
634 | /** |
635 | * Create and open the client resource |
636 | * |
637 | * @return AbstractClient |
638 | */ |
639 | abstract public function open(); |
640 | |
641 | /** |
642 | * Method to send the request and get the response |
643 | * |
644 | * @return void |
645 | */ |
646 | abstract public function send(); |
647 | |
648 | /** |
649 | * Method to reset the client object |
650 | * |
651 | * @return AbstractClient |
652 | */ |
653 | abstract public function reset(); |
654 | |
655 | } |