| Total Complexity | 40 |
| Total Lines | 312 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 242 | { |
||
| 243 | if ($ttl !== null && !is_int($ttl) && !$ttl instanceof \DateInterval) { |
||
|
|
|||
| 244 | throw new \InvalidArgumentException('Invalid cache ttl value. Supported \DateInterval, int and null.'); |
||
| 245 | } |
||
| 246 | $config = $this->getConfig(); |
||
| 247 | $config[self::OPTION_CACHE_TTL] = $ttl; |
||
| 248 | $this->setConfig($config); |
||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param array $config |
||
| 254 | */ |
||
| 255 | protected function mergeConfig(array $config): void |
||
| 256 | { |
||
| 257 | if (!empty($config)) { |
||
| 258 | $this->setConfig( |
||
| 259 | array_replace_recursive( |
||
| 260 | $this->getConfig(), |
||
| 261 | $config |
||
| 262 | ) |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param array $config |
||
| 269 | */ |
||
| 270 | protected function setConfig(array $config): void |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param RequestInterface $request |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | private static function getRequestHash(RequestInterface $request): string |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $method |
||
| 304 | * @param iterable $urls |
||
| 305 | * @param array $options |
||
| 306 | * @param int $concurrency |
||
| 307 | * @return array |
||
| 308 | * @throws GuzzleException |
||
| 309 | */ |
||
| 310 | public function requestAsyncPool(string $method, iterable $urls, array $options = [], int $concurrency = 4): array |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $method |
||
| 324 | * @param iterable $urls |
||
| 325 | * @param array $options |
||
| 326 | * @return \Generator |
||
| 327 | */ |
||
| 328 | private function requestGenerator(string $method, iterable $urls, array $options): \Generator |
||
| 332 | } |
||
| 333 | } |
||
| 334 | } |
||
| 335 |