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 |
||
11 | class HttpClient |
||
12 | { |
||
13 | /** |
||
14 | * The Guzzle client. |
||
15 | * |
||
16 | * @var \GuzzleHttp\Client |
||
17 | */ |
||
18 | protected $client; |
||
19 | |||
20 | /** |
||
21 | * The Guzzle response. |
||
22 | * |
||
23 | * @var \GuzzleHttp\Psr7\Response |
||
24 | */ |
||
25 | protected $response; |
||
26 | |||
27 | /** |
||
28 | * The request options. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $options = [ |
||
33 | 'connect_timeout' => 5, |
||
34 | 'timeout' => 25, |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * Indicates whether throws Guzzle exceptions. |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $withExceptions = false; |
||
43 | |||
44 | /** |
||
45 | * Create a http client instance. |
||
46 | * |
||
47 | * @param array|string $config base_uri or any request options |
||
48 | */ |
||
49 | 16 | public function __construct($config = null) |
|
59 | |||
60 | /** |
||
61 | * Get the Guzzle client instance. |
||
62 | * |
||
63 | * @return \GuzzleHttp\Client |
||
64 | */ |
||
65 | 2 | public function getClient() |
|
69 | |||
70 | /** |
||
71 | * Trun on/off Guzzle exceptions. |
||
72 | * |
||
73 | * @param bool $throws |
||
74 | * @return $this |
||
75 | */ |
||
76 | 1 | public function withExceptions($throws) |
|
82 | |||
83 | /** |
||
84 | * Get the request options. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 9 | public function getOptions() |
|
92 | |||
93 | /** |
||
94 | * Merge request options. |
||
95 | * |
||
96 | * @param array $options |
||
97 | * @return $this |
||
98 | */ |
||
99 | 4 | public function options(array ...$options) |
|
105 | |||
106 | /** |
||
107 | * Remove one or many options using "dot" notation. |
||
108 | * |
||
109 | * @param string|array|null $key |
||
110 | * @return $this |
||
111 | */ |
||
112 | 9 | public function removeOptions($key = null) |
|
122 | |||
123 | /** |
||
124 | * Set a request option using "dot" notation. |
||
125 | * |
||
126 | * @param string $key |
||
127 | * @param mixed $value |
||
128 | * @return $this |
||
129 | */ |
||
130 | 7 | public function option($key, $value) |
|
138 | |||
139 | /** |
||
140 | * Get a request option using "dot" notation. |
||
141 | * |
||
142 | * @param string $key |
||
143 | * @return mixed |
||
144 | */ |
||
145 | 3 | public function getOption($key) |
|
149 | |||
150 | /** |
||
151 | * Set the request header. |
||
152 | * |
||
153 | * @param string $name |
||
154 | * @param mixed $value |
||
155 | * @return $this |
||
156 | */ |
||
157 | 4 | public function header($name, $value) |
|
161 | |||
162 | /** |
||
163 | * Set the request content type. |
||
164 | * |
||
165 | * @param string $type |
||
166 | * @return $this |
||
167 | */ |
||
168 | 1 | public function contentType($type) |
|
172 | |||
173 | /** |
||
174 | * Set the request accept type. |
||
175 | * |
||
176 | * @param string $type |
||
177 | * @return $this |
||
178 | */ |
||
179 | 2 | public function accept($type) |
|
183 | |||
184 | /** |
||
185 | * Set the request accept type to JSON. |
||
186 | * |
||
187 | * @return $this |
||
188 | */ |
||
189 | 1 | public function acceptJson() |
|
193 | |||
194 | /** |
||
195 | * Specify where the body of a response will be saved. |
||
196 | * Set the "sink" option. |
||
197 | * |
||
198 | * @param mixed $value |
||
199 | * @return $this |
||
200 | */ |
||
201 | 1 | public function saveTo($value) |
|
205 | |||
206 | /** |
||
207 | * Get the Guzzle response instance. |
||
208 | * |
||
209 | * @return \GuzzleHttp\Psr7\Response|null |
||
210 | */ |
||
211 | 1 | public function getResponse() |
|
215 | |||
216 | /** |
||
217 | * Get the status code of response. |
||
218 | * |
||
219 | * @return int |
||
220 | */ |
||
221 | public function getStatusCode() |
||
227 | |||
228 | /** |
||
229 | * Get the response header value. |
||
230 | * |
||
231 | * @param string $name |
||
232 | * @return mixed |
||
233 | */ |
||
234 | public function getHeader($name) |
||
240 | |||
241 | /** |
||
242 | * Get all response headers values. |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | public function getHeaders() |
||
250 | |||
251 | /** |
||
252 | * Get response body. |
||
253 | * |
||
254 | * @return \GuzzleHttp\Psr7\Stream|null |
||
255 | */ |
||
256 | public function getBody() |
||
262 | |||
263 | /** |
||
264 | * Get response content. |
||
265 | * |
||
266 | * @return string|null |
||
267 | */ |
||
268 | public function getContent() |
||
274 | |||
275 | /** |
||
276 | * Get JSON decoded response content. |
||
277 | * |
||
278 | * @param bool $assoc |
||
279 | * @return mixed |
||
280 | */ |
||
281 | public function getJson($assoc = true) |
||
287 | |||
288 | /** |
||
289 | * Make request to an URL. |
||
290 | * |
||
291 | * @param string $url |
||
292 | * @param string $method |
||
293 | * @param array $options |
||
294 | * @return $this |
||
295 | */ |
||
296 | 2 | public function request($url, $method = 'GET', $options = []) |
|
310 | |||
311 | /** |
||
312 | * Make request to an URL, expecting JSON content. |
||
313 | * |
||
314 | * @param string $url |
||
315 | * @param string $method |
||
316 | * @param array $options |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function requestJson($url, $method = 'GET', $options = []) |
||
325 | |||
326 | /** |
||
327 | * Request the URL and return the response content. |
||
328 | * |
||
329 | * @param string $url |
||
330 | * @param string $method |
||
331 | * @param array $options |
||
332 | * @return string|null |
||
333 | */ |
||
334 | public function fetchContent($url, $method = 'GET', $options = []) |
||
338 | |||
339 | /** |
||
340 | * Request the URL and return the JSON decoded response content. |
||
341 | * |
||
342 | * @param string $url |
||
343 | * @param string $method |
||
344 | * @param array $options |
||
345 | * @param bool $assoc |
||
346 | * @return mixed |
||
347 | */ |
||
348 | public function fetchJson($url, $method = 'GET', $options = [], $assoc = true) |
||
352 | |||
353 | 1 | public function __call($method, $args) |
|
381 | } |
||
382 |