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 | */ |
||
49 | 27 | public function __construct(string $token, array $options = array()) |
|
63 | |||
64 | /** |
||
65 | * Getter for the HTTP client. |
||
66 | * |
||
67 | * @return Client |
||
68 | */ |
||
69 | 23 | protected function getHttpClient() : Client |
|
88 | |||
89 | /** |
||
90 | * Setter for the Guzzle HandlerStack |
||
91 | */ |
||
92 | 26 | public function setHandlerStack(HandlerStack $handlerStack) |
|
96 | |||
97 | 27 | public function setLogger(LoggerInterface $logger) |
|
101 | |||
102 | 26 | public function setLogLevel(string $logLevel) |
|
118 | |||
119 | /** |
||
120 | * Setter for the Guzzle MessageFormatter |
||
121 | */ |
||
122 | 27 | public function setMessageFormatter(MessageFormatter $messageFormatter) |
|
126 | |||
127 | /** |
||
128 | * Get Your Information |
||
129 | * |
||
130 | * Returns your information. |
||
131 | * |
||
132 | * @return Response |
||
133 | */ |
||
134 | 2 | public function me() : Response |
|
138 | |||
139 | /** |
||
140 | * Update Your Information |
||
141 | * |
||
142 | * Updates and returns your information with the updates applied. |
||
143 | * |
||
144 | * @param string $first_name Your first name. (Optional) |
||
145 | * @param string $last_name Your last name. (Optional) |
||
146 | * |
||
147 | * @return Response |
||
148 | */ |
||
149 | 1 | public function updateMe(string $first_name = null, string $last_name = null) : Response |
|
162 | |||
163 | /** |
||
164 | * API Client List |
||
165 | * |
||
166 | * Returns an array of your API clients. |
||
167 | * |
||
168 | * @return Response |
||
169 | */ |
||
170 | 1 | public function listClients() : Response |
|
174 | |||
175 | /** |
||
176 | * Creating an API Client |
||
177 | * |
||
178 | * Creates an API client, which can be used to authenticate against |
||
179 | * the Gaug.es API. |
||
180 | * |
||
181 | * @param string $description Short description for the key (Optional) |
||
182 | * |
||
183 | * @return Response |
||
184 | */ |
||
185 | 1 | public function createClient(string $description = null) : Response |
|
186 | { |
||
187 | 1 | $params = array(); |
|
188 | 1 | if (isset($description)) { |
|
189 | 1 | $params['description'] = $description; |
|
190 | } |
||
191 | |||
192 | 1 | return $this->makeApiCall('POST', 'clients', $params); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Delete an API Client |
||
197 | * |
||
198 | * Permanently deletes an API client key. |
||
199 | * |
||
200 | * @param string $id |
||
201 | * |
||
202 | * @return Response |
||
203 | */ |
||
204 | 1 | public function deleteClient(string $id) : Response |
|
208 | |||
209 | /** |
||
210 | * Gauges List |
||
211 | * |
||
212 | * Returns an array of your gauges, with recent traffic included. |
||
213 | * |
||
214 | * @param int $page Page number (Optional) |
||
215 | * |
||
216 | * @return Response |
||
217 | */ |
||
218 | 1 | public function listGauges(int $page = null) : Response |
|
222 | |||
223 | /** |
||
224 | * Create a New Gauge |
||
225 | * |
||
226 | * Creates a gauge. |
||
227 | * |
||
228 | * @param string $title |
||
229 | * @param string|\DateTimeZone $tz |
||
230 | * @param string $allowedHosts (Optional) |
||
231 | * |
||
232 | * @return Response |
||
233 | */ |
||
234 | 2 | public function createGauge(string $title, $tz, string $allowedHosts = null) : Response |
|
238 | |||
239 | /** |
||
240 | * Gauge Detail |
||
241 | * |
||
242 | * Gets details for a gauge. |
||
243 | * |
||
244 | * @param string $id |
||
245 | * |
||
246 | * @return Response |
||
247 | */ |
||
248 | 1 | public function gaugeDetail(string $id) : Response |
|
252 | |||
253 | /** |
||
254 | * Update a Gauge |
||
255 | * |
||
256 | * Updates and returns a gauge with the updates applied. |
||
257 | * |
||
258 | * @param string $id |
||
259 | * @param string $title |
||
260 | * @param string|\DateTimeZone $tz |
||
261 | * @param string $allowedHosts (Optional) |
||
262 | * |
||
263 | * @return Response |
||
264 | */ |
||
265 | 1 | public function updateGauge(string $id, string $title, $tz, string $allowedHosts = null) : Response |
|
269 | |||
270 | /** |
||
271 | * Delete a Gauge |
||
272 | * |
||
273 | * Permanently deletes a gauge. |
||
274 | * |
||
275 | * @param string $id |
||
276 | * |
||
277 | * @return Response |
||
278 | */ |
||
279 | 1 | public function deleteGauge(string $id) : Response |
|
283 | |||
284 | /** |
||
285 | * List Shares |
||
286 | * |
||
287 | * Lists the people that have access to a Gauge. |
||
288 | * |
||
289 | * @param string $id |
||
290 | * |
||
291 | * @return Response |
||
292 | */ |
||
293 | 1 | public function listShares(string $id) : Response |
|
297 | |||
298 | /** |
||
299 | * Share a Gauge |
||
300 | * |
||
301 | * Shares gauge with a person by their email. Any valid email will work |
||
302 | * and will receive an invite even if there is no existing Gauges user |
||
303 | * with that email. |
||
304 | * |
||
305 | * @param string $id |
||
306 | * @param string $email |
||
307 | * |
||
308 | * @return Response |
||
309 | */ |
||
310 | 1 | public function shareGauge(string $id, string $email) : Response |
|
318 | |||
319 | /** |
||
320 | * Top Content |
||
321 | * |
||
322 | * Gets top content for a gauge, paginated. |
||
323 | * |
||
324 | * @param string $id |
||
325 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
326 | * @param string $group (Optional) Either "day" or "month". Default is "day". |
||
327 | * @param int $page (Optional) |
||
328 | * |
||
329 | * @return Response |
||
330 | */ |
||
331 | 2 | public function topContent(string $id, $date = null, string $group = null, int $page = null) : Response |
|
347 | |||
348 | /** |
||
349 | * Un-share Gauge |
||
350 | * |
||
351 | * @param string $id |
||
352 | * @param string $user_id |
||
353 | * |
||
354 | * @return Response |
||
355 | */ |
||
356 | 1 | public function unshareGauge(string $id, string $user_id) : Response |
|
360 | |||
361 | /** |
||
362 | * Top Referrers |
||
363 | * |
||
364 | * Gets top referrers for a gauge, paginated. |
||
365 | * |
||
366 | * @param string $id |
||
367 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
368 | * @param int $page (Optional) |
||
369 | * |
||
370 | * @return Response |
||
371 | */ |
||
372 | 1 | public function topReferrers(string $id, $date = null, int $page = null) : Response |
|
376 | |||
377 | /** |
||
378 | * Traffic |
||
379 | * |
||
380 | * Gets traffic for a gauge. |
||
381 | * |
||
382 | * @param string $id |
||
383 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
384 | * |
||
385 | * @return Response |
||
386 | */ |
||
387 | 1 | public function traffic(string $id, $date = null) : Response |
|
391 | |||
392 | /** |
||
393 | * Browser Resolutions |
||
394 | * |
||
395 | * Gets browsers heights, browser widths, and screen widths for a gauge. |
||
396 | * |
||
397 | * @param string $id |
||
398 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
399 | * |
||
400 | * @return Response |
||
401 | */ |
||
402 | 1 | public function browserResolutions(string $id, $date = null) : Response |
|
406 | |||
407 | /** |
||
408 | * Technology |
||
409 | * |
||
410 | * Gets browsers and platforms for a gauge. |
||
411 | * |
||
412 | * @param string $id |
||
413 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
414 | * |
||
415 | * @return Response |
||
416 | */ |
||
417 | 1 | public function technology(string $id, $date = null) : Response |
|
421 | |||
422 | /** |
||
423 | * Search Terms |
||
424 | * |
||
425 | * Gets search terms for a gauge, paginated. |
||
426 | * |
||
427 | * @param string $id |
||
428 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
429 | * @param int $page (Optional) |
||
430 | * |
||
431 | * @return Response |
||
432 | */ |
||
433 | 1 | public function searchTerms(string $id, $date = null, int $page = null) : Response |
|
437 | |||
438 | /** |
||
439 | * Search Engines |
||
440 | * |
||
441 | * Gets search engines for a gauge. |
||
442 | * |
||
443 | * @param string $id |
||
444 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
445 | * |
||
446 | * @return Response |
||
447 | */ |
||
448 | 1 | public function searchEngines(string $id, $date = null) : Response |
|
452 | |||
453 | /** |
||
454 | * Locations |
||
455 | * |
||
456 | * Gets locations for a gauge. |
||
457 | * |
||
458 | * @param string $id |
||
459 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
460 | * |
||
461 | * @return Response |
||
462 | */ |
||
463 | 1 | public function locations(string $id, $date = null) : Response |
|
467 | |||
468 | /** |
||
469 | * Browser stats |
||
470 | * |
||
471 | * Get the browser statistics in a format used with the browserlist module. |
||
472 | * (See https://github.com/ai/browserslist) |
||
473 | * |
||
474 | * @param string $id |
||
475 | * @param string|\DateTime $date (Optional) Date in format YYYY-MM-DD |
||
476 | * |
||
477 | * @return Response |
||
478 | */ |
||
479 | 1 | public function browserStats(string $id, $date = null) : Response |
|
483 | |||
484 | /** |
||
485 | * Make the actual gauges API call. |
||
486 | * |
||
487 | * @param string $method [GET|POST|PUT|DELETE] |
||
488 | * @param string $path |
||
489 | * @param array $params |
||
490 | * |
||
491 | * @return Response |
||
492 | */ |
||
493 | 23 | protected function makeApiCall(string $method, string $path, array $params = array()) : Response |
|
508 | |||
509 | 11 | private function formatCommonParameters($date = null, int $page = null) : array |
|
526 | |||
527 | 3 | private function formatGaugeParameters(string $title, $tz, string $allowedHosts = null) : array |
|
547 | } |
||
548 |