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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 68 | class HttpClient |
||
| 69 | { |
||
| 70 | /** |
||
| 71 | * The default request options. |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected static $defaultOptions = [ |
||
| 76 | 'connect_timeout' => 5, |
||
| 77 | 'timeout' => 20, |
||
| 78 | 'http_errors' => false, |
||
| 79 | ]; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The Guzzle client. |
||
| 83 | * |
||
| 84 | * @var \GuzzleHttp\Client |
||
| 85 | */ |
||
| 86 | protected $client; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The request options. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $options = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The Guzzle response. |
||
| 97 | * |
||
| 98 | * @var \GuzzleHttp\Psr7\Response |
||
| 99 | */ |
||
| 100 | protected $response; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Indicate whether to catch Guzzle exceptions. |
||
| 104 | * |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | protected $catchExceptions = true; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the default request options. |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | 28 | public static function defaultOptions() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Set the default request options. |
||
| 121 | * |
||
| 122 | * @param array $options |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | 29 | public static function setDefaultOptions(array $options) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Create a new http client instance. |
||
| 132 | * |
||
| 133 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
| 134 | * @return static |
||
| 135 | */ |
||
| 136 | 1 | public static function new($options = []) |
|
| 137 | { |
||
| 138 | 1 | return new static($options); |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Create a new http client instance. |
||
| 143 | * |
||
| 144 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
| 145 | * |
||
| 146 | * @throws \InvalidArgumentException |
||
| 147 | */ |
||
| 148 | 28 | public function __construct($options = []) |
|
| 149 | { |
||
| 150 | 28 | if (is_string($options) || $options instanceof UriInterface) { |
|
| 151 | 1 | $options = ['base_uri' => $options]; |
|
| 152 | 27 | } elseif (! is_array($options)) { |
|
| 153 | 1 | throw new InvalidArgumentException('Options must be a string, UriInterface, or an array'); |
|
| 154 | } |
||
| 155 | |||
| 156 | 27 | $this->client = new Client( |
|
| 157 | 27 | array_replace_recursive(static::defaultOptions(), $options) |
|
| 158 | ); |
||
| 159 | |||
| 160 | 27 | $this->options = $this->client->getConfig(); |
|
| 161 | 27 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get the Guzzle client instance. |
||
| 165 | * |
||
| 166 | * @return \GuzzleHttp\Client |
||
| 167 | */ |
||
| 168 | 4 | public function getClient() |
|
| 169 | { |
||
| 170 | 4 | return $this->client; |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get whether to catch Guzzle exceptions or not. |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 1 | public function areExceptionsCaught() |
|
| 179 | { |
||
| 180 | 1 | return $this->catchExceptions; |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Set whether to catch Guzzle exceptions or not. |
||
| 185 | * |
||
| 186 | * @param bool $catch |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | 2 | public function catchExceptions($catch) |
|
| 190 | { |
||
| 191 | 2 | $this->catchExceptions = (bool) $catch; |
|
| 192 | |||
| 193 | 2 | return $this; |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the request options using "dot" notation. |
||
| 198 | * |
||
| 199 | * @param string|null $key |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | 12 | public function getOption($key = null) |
|
| 203 | { |
||
| 204 | 12 | return Arr::get($this->options, $key); |
|
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set the request options using "dot" notation. |
||
| 209 | * |
||
| 210 | * @param string|array $key |
||
| 211 | * @param mixed $value |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | 9 | public function option($key, $value = null) |
|
| 215 | { |
||
| 216 | 9 | $keys = is_array($key) ? $key : [$key => $value]; |
|
| 217 | |||
| 218 | 9 | foreach ($keys as $key => $value) { |
|
| 219 | 9 | Arr::set($this->options, $key, $value); |
|
| 220 | } |
||
| 221 | |||
| 222 | 9 | return $this; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Merge the given options to the request options. |
||
| 227 | * |
||
| 228 | * @param array ...$options |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | 1 | public function mergeOptions(array ...$options) |
|
| 232 | { |
||
| 233 | 1 | $this->options = array_replace_recursive($this->options, ...$options); |
|
| 234 | |||
| 235 | 1 | return $this; |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Remove one or many request options using "dot" notation. |
||
| 240 | * |
||
| 241 | * @param array|string $keys |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | 2 | public function removeOption($keys) |
|
| 245 | { |
||
| 246 | 2 | Arr::forget($this->options, is_array($keys) ? $keys : func_get_args()); |
|
| 247 | |||
| 248 | 2 | return $this; |
|
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set a request header. |
||
| 253 | * |
||
| 254 | * @param string $name |
||
| 255 | * @param mixed $value |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | 6 | public function header($name, $value) |
|
| 259 | { |
||
| 260 | 6 | return $this->option('headers.'.$name, $value); |
|
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Set the request accept type. |
||
| 265 | * |
||
| 266 | * @param string $type |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | 3 | public function accept($type) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Set the request accept type to "application/json". |
||
| 276 | * |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | 1 | public function acceptJson() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Set user agent for the request. |
||
| 286 | * |
||
| 287 | * @param string $value |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | 1 | public function userAgent($value) |
|
| 291 | { |
||
| 292 | 1 | return $this->header('User-Agent', $value); |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set the request content type. |
||
| 297 | * |
||
| 298 | * @param string $type |
||
| 299 | * @return $this |
||
| 300 | */ |
||
| 301 | 1 | public function contentType($type) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Specify where the body of the response will be saved. |
||
| 308 | * Set the "sink" option. |
||
| 309 | * |
||
| 310 | * @param string|resource|\Psr\Http\Message\StreamInterface $dest |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | 1 | public function saveTo($dest) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get the Guzzle response instance. |
||
| 320 | * |
||
| 321 | * @return \GuzzleHttp\Psr7\Response|null |
||
| 322 | */ |
||
| 323 | 3 | public function getResponse() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Get data from the response. |
||
| 330 | * |
||
| 331 | * @param string|\Closure $callback |
||
| 332 | * @param mixed $parameters |
||
| 333 | * @param mixed $default |
||
| 334 | * @return mixed |
||
| 335 | */ |
||
| 336 | 6 | public function getResponseData($callback, $parameters = [], $default = null) |
|
| 337 | { |
||
| 338 | 6 | if ($this->response) { |
|
| 339 | 6 | return $callback instanceof Closure |
|
| 340 | 1 | ? $callback($this->response, ...(array) $parameters) |
|
| 341 | 6 | : $this->response->$callback(...(array) $parameters); |
|
| 342 | } |
||
| 343 | |||
| 344 | 1 | return $default; |
|
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get the response content. |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | 4 | public function getContent() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Get the JSON-decoded response content. |
||
| 359 | * |
||
| 360 | * @param bool $assoc |
||
| 361 | * @return mixed |
||
| 362 | */ |
||
| 363 | 2 | public function getJsonContent($assoc = true) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Make request to a URI. |
||
| 370 | * |
||
| 371 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 372 | * @param string $method |
||
| 373 | * @param array $options |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | 11 | public function request($uri = '', $method = 'GET', array $options = []) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Make request to a URI, expecting JSON content. |
||
| 396 | * |
||
| 397 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 398 | * @param string $method |
||
| 399 | * @param array $options |
||
| 400 | * @return $this |
||
| 401 | */ |
||
| 402 | 4 | public function requestJson($uri = '', $method = 'GET', array $options = []) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Add JSON type to the "Accept" header for the request options. |
||
| 413 | * |
||
| 414 | * @param array $options |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | 4 | protected function addAcceptableJsonType(array $options) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Request the URI and return the response content. |
||
| 431 | * |
||
| 432 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 433 | * @param string $method |
||
| 434 | * @param array $options |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 1 | public function fetchContent($uri = '', $method = 'GET', array $options = []) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Request the URI and return the JSON-decoded response content. |
||
| 444 | * |
||
| 445 | * @param string|\Psr\Http\Message\UriInterface $uri |
||
| 446 | * @param string $method |
||
| 447 | * @param array $options |
||
| 448 | * @return mixed |
||
| 449 | */ |
||
| 450 | 1 | public function fetchJson($uri = '', $method = 'GET', array $options = []) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Get all allowed magic request methods. |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | 9 | protected function getMagicRequestMethods() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Determine if the given method is a magic request method. |
||
| 469 | * |
||
| 470 | * @param string $method |
||
| 471 | * @param string &$requestMethod |
||
| 472 | * @param string &$httpMethod |
||
| 473 | * @return bool |
||
| 474 | */ |
||
| 475 | 9 | protected function isMagicRequestMethod($method, &$requestMethod, &$httpMethod) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Get parameters for $this->request() from the magic request methods. |
||
| 496 | * |
||
| 497 | * @param string $httpMethod |
||
| 498 | * @param array $parameters |
||
| 499 | * @return array |
||
| 500 | */ |
||
| 501 | 2 | protected function getRequestParameters($httpMethod, array $parameters) |
|
| 502 | { |
||
| 503 | 2 | if (empty($parameters)) { |
|
| 504 | 1 | $parameters = ['', $httpMethod]; |
|
| 505 | } else { |
||
| 506 | 2 | array_splice($parameters, 1, 0, $httpMethod); |
|
| 507 | } |
||
| 508 | |||
| 509 | 2 | return $parameters; |
|
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get all allowed magic response methods. |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | 7 | protected function getMagicResponseMethods() |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Get all allowed magic option methods. |
||
| 527 | * |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | 2 | protected function getMagicOptionMethods() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Get the option key for the given magic option method. |
||
| 547 | * |
||
| 548 | * @param string $method |
||
| 549 | * @return string|null |
||
| 550 | */ |
||
| 551 | 2 | protected function getOptionKeyForMethod($method) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Handle magic method to send request, get response data, or set |
||
| 560 | * request options. |
||
| 561 | * |
||
| 562 | * @param string $method |
||
| 563 | * @param array $parameters |
||
| 564 | * @return mixed |
||
| 565 | * |
||
| 566 | * @throws \InvalidArgumentException |
||
| 567 | * @throws \BadMethodCallException |
||
| 568 | */ |
||
| 569 | 9 | public function __call($method, $parameters) |
|
| 591 | } |
||
| 592 |