1 | <?php |
||||
2 | namespace Mezon\CustomClient; |
||||
3 | |||||
4 | /** |
||||
5 | * Class CustomClient |
||||
6 | * |
||||
7 | * @package Mezon |
||||
8 | * @subpackage CustomClient |
||||
9 | * @author Dodonov A.A. |
||||
10 | * @version v.1.0 (2019/08/07) |
||||
11 | * @copyright Copyright (c) 2019, aeon.org |
||||
12 | */ |
||||
13 | |||||
14 | /** |
||||
15 | * Custom API client class |
||||
16 | */ |
||||
17 | class CustomClient |
||||
18 | { |
||||
19 | |||||
20 | /** |
||||
21 | * Server host |
||||
22 | * |
||||
23 | * @var string |
||||
24 | */ |
||||
25 | protected $url = false; |
||||
26 | |||||
27 | /** |
||||
28 | * Headers |
||||
29 | * |
||||
30 | * @var array |
||||
31 | */ |
||||
32 | protected $headers = []; |
||||
33 | |||||
34 | /** |
||||
35 | * Idempotence key |
||||
36 | * |
||||
37 | * @var string |
||||
38 | */ |
||||
39 | protected $idempotencyKey = ''; |
||||
40 | |||||
41 | /** |
||||
42 | * Constructor |
||||
43 | * |
||||
44 | * @param string $url |
||||
45 | * Service URL |
||||
46 | * @param array $headers |
||||
47 | * HTTP headers |
||||
48 | */ |
||||
49 | public function __construct(string $url, array $headers = []) |
||||
50 | { |
||||
51 | if ($url === false || $url === '') { |
||||
52 | throw (new \Exception( |
||||
53 | 'Service URL must be set in class ' . __CLASS__ . ' extended in ' . get_called_class() . |
||||
54 | ' and called from ' . ($_SERVER['SERVER_NAME'] ?? 'console') . ($_SERVER['REQUEST_URI'] ?? ''), |
||||
55 | - 23)); |
||||
56 | } |
||||
57 | |||||
58 | $this->url = rtrim($url, '/'); |
||||
59 | |||||
60 | $this->headers = $headers; |
||||
61 | } |
||||
62 | |||||
63 | /** |
||||
64 | * Method send request to the URL |
||||
65 | * |
||||
66 | * @param string $url |
||||
67 | * URL |
||||
68 | * @param array $headers |
||||
69 | * Headers |
||||
70 | * @param string $method |
||||
71 | * Request HTTP Method |
||||
72 | * @param array $data |
||||
73 | * Request data |
||||
74 | * @return array Response body and HTTP code |
||||
75 | * @codeCoverageIgnore |
||||
76 | */ |
||||
77 | protected function sendRequest(string $url, array $headers, string $method, array $data = []): array |
||||
78 | { |
||||
79 | return \Mezon\CustomClient\CurlWrapper::sendRequest($url, $headers, $method, $data); |
||||
80 | } |
||||
81 | |||||
82 | /** |
||||
83 | * Method gets result and validates it. |
||||
84 | * |
||||
85 | * @param string $url |
||||
86 | * request URL |
||||
87 | * @param int $code |
||||
88 | * response HTTP code |
||||
89 | * @param string $body |
||||
90 | * response body |
||||
91 | * @return mixed Request result |
||||
92 | */ |
||||
93 | protected function dispatchResult(string $url, int $code, string $body) |
||||
94 | { |
||||
95 | if ($code == 0) { |
||||
96 | throw (new \Exception("No response from URL : " . $url)); |
||||
97 | } elseif ($code == 404) { |
||||
98 | throw (new \Exception("URL: $url not found")); |
||||
99 | } elseif ($code == 400) { |
||||
100 | throw (new \Exception("Bad request on URL $url")); |
||||
101 | } elseif ($code == 403) { |
||||
102 | throw (new \Exception("Auth error")); |
||||
103 | } |
||||
104 | |||||
105 | return $body; |
||||
106 | } |
||||
107 | |||||
108 | /** |
||||
109 | * Method returns common headers |
||||
110 | * |
||||
111 | * @return array Headers |
||||
112 | */ |
||||
113 | protected function getCommonHeaders(): array |
||||
114 | { |
||||
115 | $result = $this->headers; |
||||
116 | |||||
117 | if ($this->idempotencyKey !== '') { |
||||
118 | $result[] = 'Idempotency-Key: ' . $this->idempotencyKey; |
||||
119 | } |
||||
120 | |||||
121 | $result[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'; |
||||
122 | |||||
123 | return $result; |
||||
124 | } |
||||
125 | |||||
126 | /** |
||||
127 | * Method compiles post headers |
||||
128 | * |
||||
129 | * @return array Header |
||||
130 | */ |
||||
131 | protected function getFormHeaders(): array |
||||
132 | { |
||||
133 | $fullHeaders = $this->getCommonHeaders(); |
||||
134 | |||||
135 | $fullHeaders[] = 'Content-type: application/x-www-form-urlencoded'; |
||||
136 | |||||
137 | return $fullHeaders; |
||||
138 | } |
||||
139 | |||||
140 | /** |
||||
141 | * Method sends request on server |
||||
142 | * |
||||
143 | * @param string $method |
||||
144 | * HTTP method (POST|PUT|DELETE) |
||||
145 | * @param string $endpoint |
||||
146 | * Calling endpoint |
||||
147 | * @param mixed $data |
||||
148 | * Request data |
||||
149 | */ |
||||
150 | protected function sendFormRequest(string $method, string $endpoint, array $data = []) |
||||
151 | { |
||||
152 | $fullUrl = $this->url . '/' . ltrim($endpoint, '/'); |
||||
153 | |||||
154 | list ($body, $code) = $this->sendRequest($fullUrl, $this->getFormHeaders(), $method, $data); |
||||
155 | |||||
156 | return $this->dispatchResult($fullUrl, $code, $body); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
157 | } |
||||
158 | |||||
159 | /** |
||||
160 | * Method sends POST request to server |
||||
161 | * |
||||
162 | * @param string $endpoint |
||||
163 | * Calling endpoint |
||||
164 | * @param array $data |
||||
165 | * Request data |
||||
166 | * @return mixed Result of the request |
||||
167 | */ |
||||
168 | public function sendPostRequest(string $endpoint, array $data = []) |
||||
169 | { |
||||
170 | return $this->sendFormRequest('POST', $endpoint, $data); |
||||
171 | } |
||||
172 | |||||
173 | /** |
||||
174 | * Method sends PUT request to server |
||||
175 | * |
||||
176 | * @param string $endpoint |
||||
177 | * Calling endpoint |
||||
178 | * @param array $data |
||||
179 | * Request data |
||||
180 | * @return mixed Result of the request |
||||
181 | */ |
||||
182 | public function sendPutRequest(string $endpoint, array $data = []) |
||||
183 | { |
||||
184 | return $this->sendFormRequest('PUT', $endpoint, $data); |
||||
185 | } |
||||
186 | |||||
187 | /** |
||||
188 | * Method sends DELETE request to server |
||||
189 | * |
||||
190 | * @param string $endpoint |
||||
191 | * Calling endpoint |
||||
192 | * @param array $data |
||||
193 | * Request data |
||||
194 | * @return mixed Result of the request |
||||
195 | */ |
||||
196 | public function sendDeleteRequest(string $endpoint, array $data = []) |
||||
197 | { |
||||
198 | return $this->sendFormRequest('DELETE', $endpoint, $data); |
||||
199 | } |
||||
200 | |||||
201 | /** |
||||
202 | * Method sends GET request to server. |
||||
203 | * |
||||
204 | * @param string $endpoint |
||||
205 | * Calling endpoint. |
||||
206 | * @return mixed Result of the remote call. |
||||
207 | */ |
||||
208 | public function sendGetRequest(string $endpoint) |
||||
209 | { |
||||
210 | $fullUrl = $this->url . '/' . ltrim($endpoint, '/'); |
||||
211 | |||||
212 | $fullUrl = str_replace(' ', '%20', $fullUrl); |
||||
213 | |||||
214 | list ($body, $code) = $this->sendRequest($fullUrl, $this->getCommonHeaders(), 'GET'); |
||||
215 | |||||
216 | return $this->dispatchResult($fullUrl, $code, $body); |
||||
0 ignored issues
–
show
It seems like
$body can also be of type boolean ; however, parameter $body of Mezon\CustomClient\CustomClient::dispatchResult() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
217 | } |
||||
218 | |||||
219 | /** |
||||
220 | * Method sets idempotence key. |
||||
221 | * To remove the key just call this method the second time with the '' parameter |
||||
222 | * |
||||
223 | * @param string $key |
||||
224 | * Idempotence key |
||||
225 | */ |
||||
226 | public function setIdempotencyKey(string $key): void |
||||
227 | { |
||||
228 | $this->idempotencyKey = $key; |
||||
229 | } |
||||
230 | |||||
231 | /** |
||||
232 | * Method returns idempotency key |
||||
233 | * |
||||
234 | * @return string Idempotency key |
||||
235 | */ |
||||
236 | public function getIdempotencyKey(): string |
||||
237 | { |
||||
238 | return $this->idempotencyKey; |
||||
239 | } |
||||
240 | |||||
241 | /** |
||||
242 | * Method returns URL |
||||
243 | * |
||||
244 | * @return string URL |
||||
245 | */ |
||||
246 | public function getUrl(): string |
||||
247 | { |
||||
248 | return $this->url; |
||||
249 | } |
||||
250 | |||||
251 | /** |
||||
252 | * Method returns headers |
||||
253 | * |
||||
254 | * @return array Headers |
||||
255 | */ |
||||
256 | public function getHeaders(): array |
||||
257 | { |
||||
258 | return $this->headers; |
||||
259 | } |
||||
260 | } |
||||
261 |