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 | 25 | public function setHandlerStack(HandlerStack $handlerStack) |
|
99 | |||
100 | 26 | public function setLogger(LoggerInterface $logger) |
|
104 | |||
105 | /** |
||
106 | * Setter for the Guzzle MessageFormatter |
||
107 | */ |
||
108 | 26 | public function setMessageFormatter(MessageFormatter $messageFormatter) |
|
112 | |||
113 | /** |
||
114 | * Get Your Information |
||
115 | * |
||
116 | * Returns your information. |
||
117 | * |
||
118 | * @return Response |
||
119 | */ |
||
120 | 2 | public function me() : Response |
|
124 | |||
125 | /** |
||
126 | * Update Your Information |
||
127 | * |
||
128 | * Updates and returns your information with the updates applied. |
||
129 | * |
||
130 | * @param string $first_name Your first name. (Optional) |
||
131 | * @param string $last_name Your last name. (Optional) |
||
132 | * |
||
133 | * @return Response |
||
134 | */ |
||
135 | 1 | public function updateMe(string $first_name = null, string $last_name = null) : Response |
|
148 | |||
149 | /** |
||
150 | * API Client List |
||
151 | * |
||
152 | * Returns an array of your API clients. |
||
153 | * |
||
154 | * @return Response |
||
155 | */ |
||
156 | 1 | public function listClients() : Response |
|
160 | |||
161 | /** |
||
162 | * Creating an API Client |
||
163 | * |
||
164 | * Creates an API client, which can be used to authenticate against |
||
165 | * the Gaug.es API. |
||
166 | * |
||
167 | * @param string $description Short description for the key (Optional) |
||
168 | * |
||
169 | * @return Response |
||
170 | */ |
||
171 | 1 | public function createClient(string $description = null) : Response |
|
180 | |||
181 | /** |
||
182 | * Delete an API Client |
||
183 | * |
||
184 | * Permanently deletes an API client key. |
||
185 | * |
||
186 | * @param string $id |
||
187 | * |
||
188 | * @return Response |
||
189 | */ |
||
190 | 1 | public function deleteClient(string $id) : Response |
|
194 | |||
195 | /** |
||
196 | * Gauges List |
||
197 | * |
||
198 | * Returns an array of your gauges, with recent traffic included. |
||
199 | * |
||
200 | * @param int $page Page number (Optional) |
||
201 | * |
||
202 | * @return Response |
||
203 | */ |
||
204 | 1 | public function listGauges(int $page = null) : Response |
|
208 | |||
209 | /** |
||
210 | * Create a New Gauge |
||
211 | * |
||
212 | * Creates a gauge. |
||
213 | * |
||
214 | * @param string $title |
||
215 | * @param string|\DateTimeZone $tz |
||
216 | * @param string $allowedHosts (Optional) |
||
217 | * |
||
218 | * @return Response |
||
219 | */ |
||
220 | 2 | public function createGauge(string $title, $tz, string $allowedHosts = null) : Response |
|
224 | |||
225 | /** |
||
226 | * Gauge Detail |
||
227 | * |
||
228 | * Gets details for a gauge. |
||
229 | * |
||
230 | * @param string $id |
||
231 | * |
||
232 | * @return Response |
||
233 | */ |
||
234 | 1 | public function gaugeDetail(string $id) : Response |
|
238 | |||
239 | /** |
||
240 | * Update a Gauge |
||
241 | * |
||
242 | * Updates and returns a gauge with the updates applied. |
||
243 | * |
||
244 | * @param string $id |
||
245 | * @param string $title |
||
246 | * @param string|\DateTimeZone $tz |
||
247 | * @param string $allowedHosts (Optional) |
||
248 | * |
||
249 | * @return Response |
||
250 | */ |
||
251 | 1 | public function updateGauge(string $id, string $title, $tz, string $allowedHosts = null) : Response |
|
255 | |||
256 | /** |
||
257 | * Delete a Gauge |
||
258 | * |
||
259 | * Permanently deletes a gauge. |
||
260 | * |
||
261 | * @param string $id |
||
262 | * |
||
263 | * @return Response |
||
264 | */ |
||
265 | 1 | public function deleteGauge(string $id) : Response |
|
269 | |||
270 | /** |
||
271 | * List Shares |
||
272 | * |
||
273 | * Lists the people that have access to a Gauge. |
||
274 | * |
||
275 | * @param string $id |
||
276 | * |
||
277 | * @return Response |
||
278 | */ |
||
279 | 1 | public function listShares(string $id) : Response |
|
283 | |||
284 | /** |
||
285 | * Share a Gauge |
||
286 | * |
||
287 | * Shares gauge with a person by their email. Any valid email will work |
||
288 | * and will receive an invite even if there is no existing Gauges user |
||
289 | * with that email. |
||
290 | * |
||
291 | * @param string $id |
||
292 | * @param string $email |
||
293 | * |
||
294 | * @return Response |
||
295 | */ |
||
296 | 1 | public function shareGauge(string $id, string $email) : Response |
|
304 | |||
305 | /** |
||
306 | * Top Content |
||
307 | * |
||
308 | * Gets top content for a gauge, paginated. |
||
309 | * |
||
310 | * @param string $id |
||
311 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
312 | * @param string $group (Optional) Either "day" or "month". Default is "day". |
||
313 | * @param int $page (Optional) |
||
314 | * |
||
315 | * @return Response |
||
316 | */ |
||
317 | 2 | public function topContent(string $id, $date = null, string $group = null, int $page = null) : Response |
|
333 | |||
334 | /** |
||
335 | * Un-share Gauge |
||
336 | * |
||
337 | * @param string $id |
||
338 | * @param string $user_id |
||
339 | * |
||
340 | * @return Response |
||
341 | */ |
||
342 | 1 | public function unshareGauge(string $id, string $user_id) : Response |
|
346 | |||
347 | /** |
||
348 | * Top Referrers |
||
349 | * |
||
350 | * Gets top referrers for a gauge, paginated. |
||
351 | * |
||
352 | * @param string $id |
||
353 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
354 | * @param int $page (Optional) |
||
355 | * |
||
356 | * @return Response |
||
357 | */ |
||
358 | 1 | public function topReferrers(string $id, $date = null, int $page = null) : Response |
|
362 | |||
363 | /** |
||
364 | * Traffic |
||
365 | * |
||
366 | * Gets traffic for a gauge. |
||
367 | * |
||
368 | * @param string $id |
||
369 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
370 | * |
||
371 | * @return Response |
||
372 | */ |
||
373 | 1 | public function traffic(string $id, $date = null) : Response |
|
377 | |||
378 | /** |
||
379 | * Browser Resolutions |
||
380 | * |
||
381 | * Gets browsers heights, browser widths, and screen widths for a gauge. |
||
382 | * |
||
383 | * @param string $id |
||
384 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
385 | * |
||
386 | * @return Response |
||
387 | */ |
||
388 | 1 | public function browserResolutions(string $id, $date = null) : Response |
|
392 | |||
393 | /** |
||
394 | * Technology |
||
395 | * |
||
396 | * Gets browsers and platforms for a gauge. |
||
397 | * |
||
398 | * @param string $id |
||
399 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
400 | * |
||
401 | * @return Response |
||
402 | */ |
||
403 | 1 | public function technology(string $id, $date = null) : Response |
|
407 | |||
408 | /** |
||
409 | * Search Terms |
||
410 | * |
||
411 | * Gets search terms for a gauge, paginated. |
||
412 | * |
||
413 | * @param string $id |
||
414 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
415 | * @param int $page (Optional) |
||
416 | * |
||
417 | * @return Response |
||
418 | */ |
||
419 | 1 | public function searchTerms(string $id, $date = null, int $page = null) : Response |
|
423 | |||
424 | /** |
||
425 | * Search Engines |
||
426 | * |
||
427 | * Gets search engines for a gauge. |
||
428 | * |
||
429 | * @param string $id |
||
430 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
431 | * |
||
432 | * @return Response |
||
433 | */ |
||
434 | 1 | public function searchEngines(string $id, $date = null) : Response |
|
438 | |||
439 | /** |
||
440 | * Locations |
||
441 | * |
||
442 | * Gets locations for a gauge. |
||
443 | * |
||
444 | * @param string $id |
||
445 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
446 | * |
||
447 | * @return Response |
||
448 | */ |
||
449 | 1 | public function locations(string $id, $date = null) : Response |
|
453 | |||
454 | /** |
||
455 | * Browser stats |
||
456 | * |
||
457 | * Get the browser statistics in a format used with the browserlist module. |
||
458 | * (See https://github.com/ai/browserslist) |
||
459 | * |
||
460 | * @param string $id |
||
461 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
462 | * |
||
463 | * @return Response |
||
464 | */ |
||
465 | 1 | public function browserStats(string $id, $date = null) : Response |
|
469 | |||
470 | /** |
||
471 | * Make the actual gauges API call. |
||
472 | * |
||
473 | * @param string $method [GET|POST|PUT|DELETE] |
||
474 | * @param string $path |
||
475 | * @param array $params |
||
476 | * |
||
477 | * @return Response |
||
478 | */ |
||
479 | 23 | protected function makeApiCall(string $method, string $path, array $params = array()) : Response |
|
494 | |||
495 | 11 | private function formatCommonParameters($date = null, int $page = null) : array |
|
512 | |||
513 | 3 | private function formatGaugeParameters(string $title, $tz, string $allowedHosts = null) : array |
|
533 | } |
||
534 |