Total Complexity | 44 |
Total Lines | 342 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like HttpClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HttpClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class HttpClient extends Client |
||
21 | { |
||
22 | public const OPTION_HANDLER_RESPONSE = 'handler_response'; |
||
23 | public const OPTION_CACHE_TTL = 'cache_ttl'; |
||
24 | public const OPTION_NO_CACHE = 'no_cache'; |
||
25 | public const OPTION_CACHE_KEY = 'cache_key'; |
||
26 | |||
27 | /** |
||
28 | * @internal |
||
29 | */ |
||
30 | private const CACHE_KEY = 'gplay.v1.%s.%s'; |
||
31 | |||
32 | /** |
||
33 | * Number of attempts with HTTP error (except 404) |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | private $retryLimit; |
||
38 | /** |
||
39 | * @var CacheInterface|null |
||
40 | */ |
||
41 | private $cache; |
||
42 | |||
43 | /** |
||
44 | * HttpClient constructor. |
||
45 | * |
||
46 | * @param array $config |
||
47 | * @param int $retryLimit |
||
48 | * @param CacheInterface|null $cache |
||
49 | */ |
||
50 | public function __construct(array $config = [], int $retryLimit = 4, ?CacheInterface $cache = null) |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param CacheInterface|null $cache |
||
187 | * @return self |
||
188 | */ |
||
189 | public function setCache(?CacheInterface $cache): self |
||
190 | { |
||
191 | $this->cache = $cache; |
||
192 | return $this; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param string|null $proxy |
||
197 | * @return self |
||
198 | */ |
||
199 | public function setProxy(?string $proxy): self |
||
200 | { |
||
201 | $config = $this->getConfig(); |
||
202 | $config[RequestOptions::PROXY] = $proxy; |
||
203 | $this->setConfig($config); |
||
204 | return $this; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param int $retryLimit |
||
209 | * @return self |
||
210 | */ |
||
211 | public function setRetryLimit(int $retryLimit): self |
||
212 | { |
||
213 | $this->retryLimit = max(0, $retryLimit); |
||
214 | return $this; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param string $key |
||
219 | * @param string $value |
||
220 | * @return HttpClient |
||
221 | */ |
||
222 | public function setHttpHeader(string $key, ?string $value): self |
||
223 | { |
||
224 | $config = $this->getConfig(); |
||
225 | if ($value === null) { |
||
226 | if (isset($config[RequestOptions::HEADERS][$key])) { |
||
227 | unset($config[RequestOptions::HEADERS][$key]); |
||
228 | $this->setConfig($config); |
||
229 | } |
||
230 | } else { |
||
231 | $config[RequestOptions::HEADERS][$key] = $value; |
||
232 | $this->setConfig($config); |
||
233 | } |
||
234 | return $this; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @param \DateInterval|int|null $ttl |
||
239 | * @return HttpClient |
||
240 | */ |
||
241 | public function setCacheTtl($ttl): self |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param float $connectTimeout |
||
254 | * @return HttpClient |
||
255 | */ |
||
256 | public function setConnectTimeout(float $connectTimeout): self |
||
257 | { |
||
258 | if ($connectTimeout < 0) { |
||
259 | throw new \InvalidArgumentException('negative connect timeout'); |
||
260 | } |
||
261 | $config = $this->getConfig(); |
||
262 | $config[RequestOptions::CONNECT_TIMEOUT] = $connectTimeout; |
||
263 | $this->setConfig($config); |
||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param float $timeout |
||
269 | * @return HttpClient |
||
270 | */ |
||
271 | public function setTimeout(float $timeout): self |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @param array $config |
||
284 | */ |
||
285 | protected function mergeConfig(array $config): void |
||
286 | { |
||
287 | if (!empty($config)) { |
||
288 | $this->setConfig( |
||
289 | array_replace_recursive( |
||
290 | $this->getConfig(), |
||
291 | $config |
||
292 | ) |
||
293 | ); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @param array $config |
||
299 | */ |
||
300 | protected function setConfig(array $config): void |
||
311 | } |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param RequestInterface $request |
||
316 | * @return string |
||
317 | */ |
||
318 | private static function getRequestHash(RequestInterface $request): string |
||
319 | { |
||
320 | $data = [ |
||
321 | $request->getMethod(), |
||
322 | (string)$request->getUri(), |
||
323 | $request->getBody()->getContents(), |
||
324 | ]; |
||
325 | foreach ($request->getHeaders() as $name => $header) { |
||
326 | $data[] = $name . ': ' . implode(', ', $header); |
||
327 | } |
||
328 | $data[] = $request->getBody()->getContents(); |
||
329 | return hash('crc32b', implode("\n", $data)); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param string $method |
||
334 | * @param iterable $urls |
||
335 | * @param array $options |
||
336 | * @param int $concurrency |
||
337 | * @return array |
||
338 | * @throws GuzzleException |
||
339 | */ |
||
340 | public function requestAsyncPool(string $method, iterable $urls, array $options = [], int $concurrency = 4): array |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param string $method |
||
354 | * @param iterable $urls |
||
355 | * @param array $options |
||
356 | * @return \Generator |
||
357 | */ |
||
358 | private function requestGenerator(string $method, iterable $urls, array $options): \Generator |
||
362 | } |
||
363 | } |
||
365 |