Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Request implements LoggerAwareInterface |
||
19 | { |
||
20 | const URI = 'https://secure.gaug.es/'; |
||
21 | |||
22 | /** @var null|ClientInterface */ |
||
23 | private $client; |
||
24 | |||
25 | /** @var HandlerStack */ |
||
26 | private $handlerStack; |
||
27 | |||
28 | /** @var null|LoggerInterface */ |
||
29 | private $logger; |
||
30 | |||
31 | /** @var string */ |
||
32 | private $logLevel; |
||
33 | |||
34 | /** @var MessageFormatter */ |
||
35 | private $messageFormatter; |
||
36 | |||
37 | /** @var array */ |
||
38 | private $options; |
||
39 | |||
40 | /** @var string */ |
||
41 | protected $token; |
||
42 | |||
43 | /** |
||
44 | * Constructor |
||
45 | * |
||
46 | * @param string $token Your API token |
||
47 | * @param array $options See Guzzle documentation (proxy, etc.) |
||
48 | * @param string $logLevel PSR3 LogLevel. Default = LogLevel::INFO |
||
49 | */ |
||
50 | 26 | public function __construct(string $token, array $options = array(), string $logLevel = LogLevel::INFO) |
|
64 | |||
65 | /** |
||
66 | * Getter for the HTTP client. |
||
67 | * |
||
68 | * @return Client |
||
69 | */ |
||
70 | 23 | protected function getHttpClient() : Client |
|
91 | |||
92 | /** |
||
93 | * Setter for the Guzzle HandlerStack |
||
94 | * |
||
95 | * @param HandlerStack $handlerStack |
||
96 | */ |
||
97 | 25 | public function setHandlerStack(HandlerStack $handlerStack) |
|
101 | |||
102 | 26 | public function setLogger(LoggerInterface $logger) |
|
106 | |||
107 | /** |
||
108 | * Setter for the Guzzle MessageFormatter |
||
109 | * |
||
110 | * @param MessageFormatter $messageFormatter |
||
111 | */ |
||
112 | 26 | public function setMessageFormatter(MessageFormatter $messageFormatter) |
|
116 | |||
117 | /** |
||
118 | * Get Your Information |
||
119 | * |
||
120 | * Returns your information. |
||
121 | * |
||
122 | * @return Response |
||
123 | */ |
||
124 | 2 | public function me() : Response |
|
128 | |||
129 | /** |
||
130 | * Update Your Information |
||
131 | * |
||
132 | * Updates and returns your information with the updates applied. |
||
133 | * |
||
134 | * @param string $firstName Your first name. (Optional) |
||
135 | * @param string $lastName Your last name. (Optional) |
||
136 | * |
||
137 | * @return Response |
||
138 | */ |
||
139 | 1 | public function updateMe(string $firstName = null, string $lastName = null) : Response |
|
152 | |||
153 | /** |
||
154 | * API Client List |
||
155 | * |
||
156 | * Returns an array of your API clients. |
||
157 | * |
||
158 | * @return Response |
||
159 | */ |
||
160 | 1 | public function listClients() : Response |
|
164 | |||
165 | /** |
||
166 | * Creating an API Client |
||
167 | * |
||
168 | * Creates an API client, which can be used to authenticate against |
||
169 | * the Gaug.es API. |
||
170 | * |
||
171 | * @param string $description Short description for the key (Optional) |
||
172 | * |
||
173 | * @return Response |
||
174 | */ |
||
175 | 1 | public function createClient(string $description = null) : Response |
|
184 | |||
185 | /** |
||
186 | * Delete an API Client |
||
187 | * |
||
188 | * Permanently deletes an API client key. |
||
189 | * |
||
190 | * @param string $id |
||
191 | * |
||
192 | * @return Response |
||
193 | */ |
||
194 | 1 | public function deleteClient(string $id) : Response |
|
198 | |||
199 | /** |
||
200 | * Gauges List |
||
201 | * |
||
202 | * Returns an array of your gauges, with recent traffic included. |
||
203 | * |
||
204 | * @param int $page Page number (Optional) |
||
205 | * |
||
206 | * @return Response |
||
207 | */ |
||
208 | 1 | public function listGauges(int $page = null) : Response |
|
212 | |||
213 | /** |
||
214 | * Create a New Gauge |
||
215 | * |
||
216 | * Creates a gauge. |
||
217 | * |
||
218 | * @param string $title |
||
219 | * @param string|\DateTimeZone $tz |
||
220 | * @param string $allowedHosts (Optional) |
||
221 | * |
||
222 | * @return Response |
||
223 | */ |
||
224 | 2 | public function createGauge(string $title, $tz, string $allowedHosts = null) : Response |
|
228 | |||
229 | /** |
||
230 | * Gauge Detail |
||
231 | * |
||
232 | * Gets details for a gauge. |
||
233 | * |
||
234 | * @param string $id |
||
235 | * |
||
236 | * @return Response |
||
237 | */ |
||
238 | 1 | public function gaugeDetail(string $id) : Response |
|
242 | |||
243 | /** |
||
244 | * Update a Gauge |
||
245 | * |
||
246 | * Updates and returns a gauge with the updates applied. |
||
247 | * |
||
248 | * @param string $id |
||
249 | * @param string $title |
||
250 | * @param string|\DateTimeZone $tz |
||
251 | * @param string $allowedHosts (Optional) |
||
252 | * |
||
253 | * @return Response |
||
254 | */ |
||
255 | 1 | public function updateGauge(string $id, string $title, $tz, string $allowedHosts = null) : Response |
|
259 | |||
260 | /** |
||
261 | * Delete a Gauge |
||
262 | * |
||
263 | * Permanently deletes a gauge. |
||
264 | * |
||
265 | * @param string $id |
||
266 | * |
||
267 | * @return Response |
||
268 | */ |
||
269 | 1 | public function deleteGauge(string $id) : Response |
|
273 | |||
274 | /** |
||
275 | * List Shares |
||
276 | * |
||
277 | * Lists the people that have access to a Gauge. |
||
278 | * |
||
279 | * @param string $id |
||
280 | * |
||
281 | * @return Response |
||
282 | */ |
||
283 | 1 | public function listShares(string $id) : Response |
|
287 | |||
288 | /** |
||
289 | * Share a Gauge |
||
290 | * |
||
291 | * Shares gauge with a person by their email. Any valid email will work |
||
292 | * and will receive an invite even if there is no existing Gauges user |
||
293 | * with that email. |
||
294 | * |
||
295 | * @param string $id |
||
296 | * @param string $email |
||
297 | * |
||
298 | * @return Response |
||
299 | */ |
||
300 | 1 | public function shareGauge(string $id, string $email) : Response |
|
308 | |||
309 | /** |
||
310 | * Top Content |
||
311 | * |
||
312 | * Gets top content for a gauge, paginated. |
||
313 | * |
||
314 | * @param string $id |
||
315 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
316 | * @param string $group (Optional) Either "day" or "month". Default is "day". |
||
317 | * @param int $page (Optional) |
||
318 | * |
||
319 | * @return Response |
||
320 | */ |
||
321 | 2 | public function topContent(string $id, $date = null, string $group = null, int $page = null) : Response |
|
337 | |||
338 | /** |
||
339 | * Un-share Gauge |
||
340 | * |
||
341 | * @param string $id |
||
342 | * @param string $user_id |
||
343 | * |
||
344 | * @return Response |
||
345 | */ |
||
346 | 1 | public function unshareGauge(string $id, string $user_id) : Response |
|
350 | |||
351 | /** |
||
352 | * Top Referrers |
||
353 | * |
||
354 | * Gets top referrers for a gauge, paginated. |
||
355 | * |
||
356 | * @param string $id |
||
357 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
358 | * @param int $page (Optional) |
||
359 | * |
||
360 | * @return Response |
||
361 | */ |
||
362 | 1 | public function topReferrers(string $id, $date = null, int $page = null) : Response |
|
366 | |||
367 | /** |
||
368 | * Traffic |
||
369 | * |
||
370 | * Gets traffic for a gauge. |
||
371 | * |
||
372 | * @param string $id |
||
373 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
374 | * |
||
375 | * @return Response |
||
376 | */ |
||
377 | 1 | public function traffic(string $id, $date = null) : Response |
|
381 | |||
382 | /** |
||
383 | * Browser Resolutions |
||
384 | * |
||
385 | * Gets browsers heights, browser widths, and screen widths for a gauge. |
||
386 | * |
||
387 | * @param string $id |
||
388 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
389 | * |
||
390 | * @return Response |
||
391 | */ |
||
392 | 1 | public function browserResolutions(string $id, $date = null) : Response |
|
396 | |||
397 | /** |
||
398 | * Technology |
||
399 | * |
||
400 | * Gets browsers and platforms for a gauge. |
||
401 | * |
||
402 | * @param string $id |
||
403 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
404 | * |
||
405 | * @return Response |
||
406 | */ |
||
407 | 1 | public function technology(string $id, $date = null) : Response |
|
411 | |||
412 | /** |
||
413 | * Search Terms |
||
414 | * |
||
415 | * Gets search terms for a gauge, paginated. |
||
416 | * |
||
417 | * @param string $id |
||
418 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
419 | * @param int $page (Optional) |
||
420 | * |
||
421 | * @return Response |
||
422 | */ |
||
423 | 1 | public function searchTerms(string $id, $date = null, int $page = null) : Response |
|
427 | |||
428 | /** |
||
429 | * Search Engines |
||
430 | * |
||
431 | * Gets search engines for a gauge. |
||
432 | * |
||
433 | * @param string $id |
||
434 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
435 | * |
||
436 | * @return Response |
||
437 | */ |
||
438 | 1 | public function searchEngines(string $id, $date = null) : Response |
|
442 | |||
443 | /** |
||
444 | * Locations |
||
445 | * |
||
446 | * Gets locations for a gauge. |
||
447 | * |
||
448 | * @param string $id |
||
449 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
450 | * |
||
451 | * @return Response |
||
452 | */ |
||
453 | 1 | public function locations(string $id, $date = null) : Response |
|
457 | |||
458 | /** |
||
459 | * Browser stats |
||
460 | * |
||
461 | * Get the browser statistics in a format used with the browserlist module. |
||
462 | * (See https://github.com/ai/browserslist) |
||
463 | * |
||
464 | * @param string $id |
||
465 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
466 | * |
||
467 | * @return Response |
||
468 | */ |
||
469 | 1 | public function browserStats(string $id, $date = null) : Response |
|
473 | |||
474 | /** |
||
475 | * Make the actual gauges API call. |
||
476 | * |
||
477 | * @param string $method [GET|POST|PUT|DELETE] |
||
478 | * @param string $path |
||
479 | * @param array $params |
||
480 | * |
||
481 | * @return Response |
||
482 | */ |
||
483 | 23 | protected function makeApiCall(string $method, string $path, array $params = array()) : Response |
|
498 | |||
499 | 11 | private function formatCommonParameters($date = null, int $page = null) : array |
|
516 | |||
517 | 3 | private function formatGaugeParameters(string $title, $tz, string $allowedHosts = null) : array |
|
537 | } |
||
538 |