1 | <?php |
||||||
2 | |||||||
3 | namespace EasyHttp\GuzzleLayer; |
||||||
4 | |||||||
5 | use EasyHttp\LayerContracts\Contracts\HttpClientAdapter; |
||||||
6 | use EasyHttp\LayerContracts\Contracts\HttpClientRequest; |
||||||
7 | use EasyHttp\LayerContracts\Contracts\HttpClientResponse; |
||||||
8 | use EasyHttp\LayerContracts\Exceptions\HttpClientException; |
||||||
9 | use EasyHttp\LayerContracts\Exceptions\HttpConnectionException; |
||||||
10 | use GuzzleHttp\ClientInterface; |
||||||
11 | use GuzzleHttp\Exception\ConnectException; |
||||||
12 | use GuzzleHttp\Exception\RequestException; |
||||||
13 | use GuzzleHttp\Exception\TransferException; |
||||||
14 | |||||||
15 | class GuzzleAdapter implements HttpClientAdapter |
||||||
16 | { |
||||||
17 | protected ClientInterface $client; |
||||||
18 | |||||||
19 | 23 | public function __construct(ClientInterface $client) |
|||||
20 | { |
||||||
21 | 23 | $this->client = $client; |
|||||
22 | } |
||||||
23 | |||||||
24 | 23 | public function request(HttpClientRequest $request): HttpClientResponse |
|||||
25 | { |
||||||
26 | try { |
||||||
27 | 23 | $response = $this->client->request( |
|||||
28 | 23 | $request->getMethod(), |
|||||
29 | 23 | $request->getUri(), |
|||||
30 | 23 | $this->buildOptions($request) |
|||||
31 | 23 | ); |
|||||
32 | 9 | } catch (ConnectException $exception) { |
|||||
33 | 1 | throw new HttpConnectionException($exception->getMessage(), (int) $exception->getCode(), $exception); |
|||||
34 | 8 | } catch (RequestException $exception) { |
|||||
35 | 7 | if (! $exception->hasResponse()) { |
|||||
36 | 5 | throw new HttpClientException($exception->getMessage(), (int) $exception->getCode(), $exception); |
|||||
37 | } |
||||||
38 | |||||||
39 | 2 | $response = $exception->getResponse(); |
|||||
40 | 1 | } catch (TransferException $exception) { |
|||||
41 | 1 | throw new HttpClientException($exception->getMessage(), (int) $exception->getCode(), $exception); |
|||||
42 | } |
||||||
43 | |||||||
44 | 16 | return new GuzzleResponse($response); |
|||||
45 | } |
||||||
46 | |||||||
47 | 23 | private function buildOptions(HttpClientRequest $request): array |
|||||
48 | { |
||||||
49 | 23 | $options = [ |
|||||
50 | 23 | 'timeout' => $request->getTimeout(), |
|||||
51 | 23 | 'verify' => $request->isSSL() |
|||||
52 | 23 | ]; |
|||||
53 | |||||||
54 | 23 | $this |
|||||
55 | 23 | ->setHeaders($request, $options) |
|||||
56 | 23 | ->setJson($request, $options) |
|||||
57 | 23 | ->setQuery($request, $options) |
|||||
58 | 23 | ->setUrlEncodedData($request, $options) |
|||||
59 | 23 | ->setSecurityContext($request, $options) |
|||||
60 | 23 | ->setBasicAuth($request, $options); |
|||||
61 | |||||||
62 | 23 | return $options; |
|||||
63 | } |
||||||
64 | |||||||
65 | 23 | private function setHeaders(HttpClientRequest $request, &$options): self |
|||||
66 | { |
||||||
67 | 23 | $headers = []; |
|||||
68 | |||||||
69 | 23 | if ($request->hasHeaders()) { |
|||||
70 | 2 | $headers = $request->getHeaders(); |
|||||
71 | } |
||||||
72 | |||||||
73 | 23 | if ($request->hasServerCookies()) { |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
74 | 1 | $headers['Set-Cookie'] = $request->getServerCookies(); |
|||||
0 ignored issues
–
show
The method
getServerCookies() does not exist on EasyHttp\LayerContracts\...racts\HttpClientRequest . It seems like you code against a sub-type of EasyHttp\LayerContracts\...racts\HttpClientRequest such as EasyHttp\GuzzleLayer\GuzzleRequest .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
75 | } |
||||||
76 | |||||||
77 | 23 | if (!empty($headers)) { |
|||||
78 | 3 | $options['headers'] = $headers; |
|||||
79 | } |
||||||
80 | |||||||
81 | 23 | return $this; |
|||||
82 | } |
||||||
83 | |||||||
84 | 23 | private function setJson(HttpClientRequest $request, &$options): self |
|||||
85 | { |
||||||
86 | 23 | if ($request->hasJson()) { |
|||||
87 | 3 | $options['json'] = $request->getJson(); |
|||||
88 | } |
||||||
89 | |||||||
90 | 23 | return $this; |
|||||
91 | } |
||||||
92 | |||||||
93 | 23 | private function setQuery(HttpClientRequest $request, &$options): self |
|||||
94 | { |
||||||
95 | 23 | if ($request->hasQuery()) { |
|||||
96 | 3 | $options['query'] = $request->getQuery(); |
|||||
97 | } |
||||||
98 | |||||||
99 | 23 | return $this; |
|||||
100 | } |
||||||
101 | |||||||
102 | 23 | private function setSecurityContext(HttpClientRequest $request, &$options): self |
|||||
103 | { |
||||||
104 | 23 | if ($request->hasSecurityContext() && $request->getSecurityContext()->hasCertificate()) { |
|||||
105 | 3 | $options['cert'] = $request->getSecurityContext()->getCertificate(); |
|||||
106 | } |
||||||
107 | |||||||
108 | 23 | if ($request->hasSecurityContext() && $request->getSecurityContext()->hasPrivateKey()) { |
|||||
109 | 2 | $options['ssl_key'] = $request->getSecurityContext()->getPrivateKey(); |
|||||
110 | } |
||||||
111 | |||||||
112 | 23 | return $this; |
|||||
113 | } |
||||||
114 | |||||||
115 | 23 | private function setBasicAuth(HttpClientRequest $request, &$options): self |
|||||
116 | { |
||||||
117 | 23 | if (count($request->getBasicAuth())) { |
|||||
118 | 2 | $options['auth'] = $request->getBasicAuth(); |
|||||
119 | } |
||||||
120 | |||||||
121 | 23 | return $this; |
|||||
122 | } |
||||||
123 | |||||||
124 | 23 | private function setUrlEncodedData(HttpClientRequest $request, array &$options): self |
|||||
125 | { |
||||||
126 | 23 | if ($request->hasUrlEncodedData()) { |
|||||
0 ignored issues
–
show
The method
hasUrlEncodedData() does not exist on EasyHttp\LayerContracts\...racts\HttpClientRequest . It seems like you code against a sub-type of EasyHttp\LayerContracts\...racts\HttpClientRequest such as EasyHttp\GuzzleLayer\GuzzleRequest .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
127 | 1 | $options['form_params'] = $request->getUrlEncodedData(); |
|||||
0 ignored issues
–
show
The method
getUrlEncodedData() does not exist on EasyHttp\LayerContracts\...racts\HttpClientRequest . It seems like you code against a sub-type of EasyHttp\LayerContracts\...racts\HttpClientRequest such as EasyHttp\GuzzleLayer\GuzzleRequest .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
128 | } |
||||||
129 | |||||||
130 | 23 | return $this; |
|||||
131 | } |
||||||
132 | } |
||||||
133 |