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 |
||
12 | class HttpClient |
||
13 | { |
||
14 | /** |
||
15 | * The default request options. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected static $defaultOptions = [ |
||
20 | 'connect_timeout' => 5, |
||
21 | 'timeout' => 25, |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * The Guzzle client. |
||
26 | * |
||
27 | * @var \GuzzleHttp\Client |
||
28 | */ |
||
29 | protected $client; |
||
30 | |||
31 | /** |
||
32 | * The Guzzle response. |
||
33 | * |
||
34 | * @var \GuzzleHttp\Psr7\Response |
||
35 | */ |
||
36 | protected $response; |
||
37 | |||
38 | /** |
||
39 | * The request options. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $options = []; |
||
44 | |||
45 | /** |
||
46 | * Indicate whether to catch Guzzle exceptions. |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $catchExceptions = true; |
||
51 | |||
52 | /** |
||
53 | * Get the default request options. |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | 24 | public static function defaultOptions() |
|
61 | |||
62 | /** |
||
63 | * Set the default request options. |
||
64 | * |
||
65 | * @param array $options |
||
66 | * @return void |
||
67 | */ |
||
68 | 1 | public static function setDefaultOptions(array $options) |
|
72 | |||
73 | /** |
||
74 | * Create a http client instance. |
||
75 | * |
||
76 | * @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
||
77 | * |
||
78 | * @throws \InvalidArgumentException |
||
79 | */ |
||
80 | 23 | public function __construct($options = []) |
|
81 | { |
||
82 | 23 | if (is_string($options) || $options instanceof UriInterface) { |
|
83 | 1 | $options = ['base_uri' => $options]; |
|
84 | 22 | } elseif (! is_array($options)) { |
|
85 | throw new InvalidArgumentException('config must be a string, UriInterface, or an array'); |
||
86 | } |
||
87 | |||
88 | 23 | $this->client = new Client( |
|
89 | 23 | $this->options = $options + static::defaultOptions() |
|
90 | ); |
||
91 | 23 | } |
|
92 | |||
93 | /** |
||
94 | * Get the Guzzle client instance. |
||
95 | * |
||
96 | * @return \GuzzleHttp\Client |
||
97 | */ |
||
98 | 2 | public function getClient() |
|
102 | |||
103 | /** |
||
104 | * Get whether to catch Guzzle exceptions or not. |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function areExceptionsCaught() |
||
112 | |||
113 | /** |
||
114 | * Set whether to catch Guzzle exceptions or not. |
||
115 | * |
||
116 | * @param bool $catch |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function catchExceptions($catch) |
||
125 | |||
126 | /** |
||
127 | * Get the request options. |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | 9 | public function getOptions() |
|
135 | |||
136 | /** |
||
137 | * Merge the request options. |
||
138 | * |
||
139 | * @param array $options |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function mergeOptions(array $options) |
||
143 | { |
||
144 | $this->options = $options + $this->options; |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Remove options using "dot" notation. |
||
151 | * |
||
152 | * @param string|array|null $key |
||
153 | * @return $this |
||
154 | */ |
||
155 | 16 | public function removeOptions($key = null) |
|
156 | { |
||
157 | 16 | if (is_null($key)) { |
|
158 | 16 | $this->options = []; |
|
159 | } else { |
||
160 | 1 | Arr::forget($this->options, is_array($key) ? $key : func_get_args()); |
|
161 | } |
||
162 | |||
163 | 16 | return $this; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get a request option using "dot" notation. |
||
168 | * |
||
169 | * @param string $key |
||
170 | * @return mixed |
||
171 | */ |
||
172 | 3 | public function getOption($key) |
|
173 | { |
||
174 | 3 | return Arr::get($this->options, $key); |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Set a request option using "dot" notation. |
||
179 | * |
||
180 | * @param string|array $key |
||
181 | * @param mixed $value |
||
182 | * @return $this |
||
183 | */ |
||
184 | 10 | public function option($key, $value = null) |
|
185 | { |
||
186 | 10 | $keys = is_array($key) ? $key : [$key => $value]; |
|
187 | |||
188 | 10 | foreach ($keys as $key => $value) { |
|
189 | 10 | Arr::set($this->options, $key, $value); |
|
190 | } |
||
191 | |||
192 | 10 | return $this; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Set the request header. |
||
197 | * |
||
198 | * @param string $name |
||
199 | * @param mixed $value |
||
200 | * @return $this |
||
201 | */ |
||
202 | 4 | public function header($name, $value) |
|
206 | |||
207 | /** |
||
208 | * Set the request content type. |
||
209 | * |
||
210 | * @param string $type |
||
211 | * @return $this |
||
212 | */ |
||
213 | 1 | public function contentType($type) |
|
217 | |||
218 | /** |
||
219 | * Set the request accept type. |
||
220 | * |
||
221 | * @param string $type |
||
222 | * @return $this |
||
223 | */ |
||
224 | 2 | public function accept($type) |
|
228 | |||
229 | /** |
||
230 | * Set the request accept type to JSON. |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | 1 | public function acceptJson() |
|
238 | |||
239 | /** |
||
240 | * Specify where the body of a response will be saved. |
||
241 | * Set the "sink" option. |
||
242 | * |
||
243 | * @param mixed $dest |
||
244 | * @return $this |
||
245 | */ |
||
246 | 1 | public function saveTo($dest) |
|
250 | |||
251 | /** |
||
252 | * Get the Guzzle response instance. |
||
253 | * |
||
254 | * @return \GuzzleHttp\Psr7\Response|null |
||
255 | */ |
||
256 | 8 | public function getResponse() |
|
260 | |||
261 | /** |
||
262 | * Get the status code of response. |
||
263 | * |
||
264 | * @return int |
||
265 | */ |
||
266 | public function getStatusCode() |
||
272 | |||
273 | /** |
||
274 | * Get the response header value. |
||
275 | * |
||
276 | * @param string $name |
||
277 | * @return mixed |
||
278 | */ |
||
279 | public function getHeader($name) |
||
285 | |||
286 | /** |
||
287 | * Get all response headers values. |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getHeaders() |
||
295 | |||
296 | /** |
||
297 | * Get response body. |
||
298 | * |
||
299 | * @return \GuzzleHttp\Psr7\Stream|null |
||
300 | */ |
||
301 | 2 | public function getBody() |
|
307 | |||
308 | /** |
||
309 | * Get response content. |
||
310 | * |
||
311 | * @return string|null |
||
312 | */ |
||
313 | 2 | public function getContent() |
|
319 | |||
320 | /** |
||
321 | * Get JSON decoded response content. |
||
322 | * |
||
323 | * @param bool $assoc |
||
324 | * @return mixed |
||
325 | */ |
||
326 | 1 | public function getJson($assoc = true) |
|
332 | |||
333 | /** |
||
334 | * Make request to a URI. |
||
335 | * |
||
336 | * @param string $uri |
||
337 | * @param string $method |
||
338 | * @param array $options |
||
339 | * @return $this |
||
340 | */ |
||
341 | 9 | public function request($uri, $method = 'GET', array $options = []) |
|
342 | { |
||
343 | try { |
||
344 | 9 | $this->response = $this->client->request( |
|
345 | 9 | $method, $uri, $options += $this->options |
|
346 | ); |
||
347 | 1 | } catch (Exception $e) { |
|
348 | 1 | if (! $this->catchExceptions) { |
|
349 | throw $e; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | 9 | return $this; |
|
354 | } |
||
355 | |||
356 | /** |
||
357 | * Make request to a URI, expecting JSON content. |
||
358 | * |
||
359 | * @param string $uri |
||
360 | * @param string $method |
||
361 | * @param array $options |
||
362 | * @return $this |
||
363 | */ |
||
364 | 3 | public function requestJson($uri, $method = 'GET', array $options = []) |
|
370 | |||
371 | /** |
||
372 | * Request the URI and return the response content. |
||
373 | * |
||
374 | * @param string $uri |
||
375 | * @param string $method |
||
376 | * @param array $options |
||
377 | * @return string|null |
||
378 | */ |
||
379 | 1 | public function fetchContent($uri, $method = 'GET', array $options = []) |
|
383 | |||
384 | /** |
||
385 | * Request the URI and return the JSON decoded response content. |
||
386 | * |
||
387 | * @param string $uri |
||
388 | * @param string $method |
||
389 | * @param array $options |
||
390 | * @param bool $assoc |
||
391 | * @return mixed |
||
392 | */ |
||
393 | 1 | public function fetchJson($uri, $method = 'GET', array $options = [], $assoc = true) |
|
397 | |||
398 | /** |
||
399 | * Dynamically methods to set request option, send request, or get |
||
400 | * response properties. |
||
401 | * |
||
402 | * @param string $method |
||
403 | * @param array $args |
||
404 | * @return mixed |
||
405 | */ |
||
406 | 6 | public function __call($method, $args) |
|
423 | } |
||
424 |