Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
13 / 13 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Client | |
100.00% |
13 / 13 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setConnectionTimeout | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getConnectionTimeout | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
request | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
validateHttpVerb | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace pvc\http; |
4 | |
5 | use GuzzleHttp\Client as GuzzleClient; |
6 | use GuzzleHttp\Psr7\Request; |
7 | use Psr\Http\Message\ResponseInterface; |
8 | use pvc\http\err\ClientGeneralException; |
9 | use pvc\http\err\ClientLogicException; |
10 | use pvc\http\err\ClientRuntimeException; |
11 | use pvc\http\err\InvalidConnectionTimeoutException; |
12 | use pvc\http\err\InvalidHttpVerbException; |
13 | use pvc\interfaces\http\UrlInterface; |
14 | use Throwable; |
15 | |
16 | class Client |
17 | { |
18 | protected int $connectionTimeoutInSeconds = 3; |
19 | |
20 | /** |
21 | * @var array<string> |
22 | */ |
23 | protected array $httpVerbs = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE']; |
24 | |
25 | public function __construct(protected GuzzleClient $guzzleClient) |
26 | { |
27 | } |
28 | |
29 | /** |
30 | * @param int $connectionTimeoutInSeconds |
31 | * @return void |
32 | */ |
33 | public function setConnectionTimeout(int $connectionTimeoutInSeconds): void |
34 | { |
35 | if ($connectionTimeoutInSeconds < 1) { |
36 | throw new InvalidConnectionTimeoutException($connectionTimeoutInSeconds); |
37 | } |
38 | $this->connectionTimeoutInSeconds = $connectionTimeoutInSeconds; |
39 | } |
40 | |
41 | /** |
42 | * @return int |
43 | */ |
44 | public function getConnectionTimeout(): int |
45 | { |
46 | return $this->connectionTimeoutInSeconds; |
47 | } |
48 | |
49 | /** |
50 | * @param string $requestType |
51 | * @param UrlInterface $url |
52 | * @return ResponseInterface |
53 | * @throws ClientRuntimeException |
54 | */ |
55 | public function request(string $requestType, UrlInterface $url): ResponseInterface |
56 | { |
57 | $this->validateHttpVerb($requestType); |
58 | $request = new Request($requestType, $url->render()); |
59 | $options = ['timeout' => $this->connectionTimeoutInSeconds]; |
60 | |
61 | /** |
62 | * the request type is valid and the url is properly formed at this point. Any remaining problems are |
63 | * some variety of runtime exception..... |
64 | */ |
65 | try { |
66 | return $this->guzzleClient->send($request, $options); |
67 | } catch (Throwable $e) { |
68 | throw new ClientRuntimeException($url->render(), $e); |
69 | } |
70 | } |
71 | |
72 | protected function validateHttpVerb(string $httpVerb): void |
73 | { |
74 | if (!in_array($httpVerb, $this->httpVerbs)) { |
75 | throw new InvalidHttpVerbException($httpVerb); |
76 | } |
77 | } |
78 | } |