Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php namespace BinPacking3d; |
||
17 | abstract class Query |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Regions |
||
22 | */ |
||
23 | const REGION_US = 'US'; // US AWS |
||
24 | const REGION_EU = 'EU'; // EU AWS |
||
25 | const REGION_GLOBAL = 'GLOBAL'; // Global API used Latency based routing to find US or EU API |
||
26 | |||
27 | /** |
||
28 | * @var |
||
29 | */ |
||
30 | protected $endpoint; |
||
31 | |||
32 | /** |
||
33 | * @var Client |
||
34 | */ |
||
35 | private $client; |
||
36 | |||
37 | /** |
||
38 | * @var Request |
||
39 | */ |
||
40 | private $request; |
||
41 | |||
42 | /** |
||
43 | * @var LoggerInterface |
||
44 | */ |
||
45 | private $logger; |
||
46 | |||
47 | /** |
||
48 | * @var |
||
49 | */ |
||
50 | private $params; |
||
51 | |||
52 | /** |
||
53 | * @var |
||
54 | */ |
||
55 | private $cache; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $region; |
||
61 | |||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | private $cacheTtl = 3600; |
||
66 | |||
67 | /** |
||
68 | * @var |
||
69 | */ |
||
70 | private $timeout; |
||
71 | |||
72 | /** |
||
73 | * @param string $region |
||
74 | */ |
||
75 | public function __construct($region = self::REGION_GLOBAL) |
||
86 | |||
87 | /** |
||
88 | * @param mixed $timeout |
||
89 | * @return Query |
||
90 | */ |
||
91 | public function setTimeout($timeout) |
||
96 | |||
97 | /** |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function getTimeout() |
||
104 | |||
105 | /** |
||
106 | * Supplying a region will use the closest by URL |
||
107 | * Supplying false will disable certificate verification and will start using the global URL always |
||
108 | * |
||
109 | * @param string|bool $region |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getUrl($region) |
||
124 | |||
125 | /** |
||
126 | * Supplying false disables CA verification, which is not advised. |
||
127 | * Supplying a region will check against the proper certificate. |
||
128 | * |
||
129 | * @param string|bool $region |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getPem($region) |
||
133 | { |
||
134 | switch ($region) { |
||
135 | case self::REGION_US: |
||
136 | return __DIR__ . '/../cert/us-east.api.3dbinpacking.com.pem'; |
||
137 | case self::REGION_EU: |
||
138 | return __DIR__ . '/../cert/eu.api.3dbinpacking.com.pem'; |
||
139 | case false: |
||
140 | return false; |
||
141 | case self::REGION_GLOBAL: |
||
142 | default: |
||
143 | return __DIR__ . '/../cert/global-api.3dbinpacking.com.pem'; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getRegion() |
||
154 | |||
155 | /** |
||
156 | * @param string $region |
||
157 | * @return Query |
||
158 | */ |
||
159 | public function setRegion($region) |
||
165 | |||
166 | /** |
||
167 | * @return Client |
||
168 | */ |
||
169 | public function getClient() |
||
173 | |||
174 | /** |
||
175 | * @param Client $client |
||
176 | * @return Query |
||
177 | */ |
||
178 | public function setClient($client) |
||
184 | |||
185 | /** |
||
186 | * @return int |
||
187 | */ |
||
188 | public function getCacheTtl() |
||
192 | |||
193 | /** |
||
194 | * @param int $cacheTtl |
||
195 | * @return Query |
||
196 | */ |
||
197 | public function setCacheTtl($cacheTtl) |
||
203 | |||
204 | /** |
||
205 | * @return LoggerInterface |
||
206 | */ |
||
207 | public function getLogger() |
||
211 | |||
212 | /** |
||
213 | * @param LoggerInterface $logger |
||
214 | * @return Query |
||
215 | */ |
||
216 | public function setLogger(LoggerInterface $logger) |
||
222 | |||
223 | /** |
||
224 | * @return Cache |
||
225 | */ |
||
226 | public function getCache() |
||
230 | |||
231 | /** |
||
232 | * @param Cache $cache |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function setCache(Cache $cache) |
||
241 | |||
242 | /** |
||
243 | * @return Packed |
||
244 | * @throws \Exception |
||
245 | */ |
||
246 | public function run() |
||
291 | |||
292 | /** |
||
293 | * @return string |
||
294 | */ |
||
295 | public function getEndpoint() |
||
299 | |||
300 | /** |
||
301 | * @param string $endpoint |
||
302 | * @return Query |
||
303 | */ |
||
304 | protected function setEndpoint($endpoint) |
||
310 | |||
311 | /** |
||
312 | * @return array |
||
313 | */ |
||
314 | public function renderRequest() |
||
318 | |||
319 | /** |
||
320 | * @return string |
||
321 | */ |
||
322 | public function renderRequestJson() |
||
326 | |||
327 | /** |
||
328 | * @param string $level |
||
329 | * @param $message |
||
330 | * @return bool |
||
331 | */ |
||
332 | private function log($level, $message) |
||
336 | |||
337 | /** |
||
338 | * @param string $cacheKey |
||
339 | * @return mixed |
||
340 | */ |
||
341 | private function getFromCache($cacheKey) |
||
349 | |||
350 | /** |
||
351 | * @return mixed |
||
352 | */ |
||
353 | public function getParams() |
||
357 | |||
358 | /** |
||
359 | * @param mixed $params |
||
360 | * @return Query |
||
361 | */ |
||
362 | public function setParams($params) |
||
368 | |||
369 | /** |
||
370 | * Handle response and return |
||
371 | * |
||
372 | * @param \Psr\Http\Message\ResponseInterface $response |
||
373 | * @param string $cacheKey |
||
374 | * @return Packed |
||
375 | * @throws CriticalException |
||
376 | */ |
||
377 | public function handleResponse(\Psr\Http\Message\ResponseInterface $response, $cacheKey = null) |
||
395 | |||
396 | /** |
||
397 | * @param $cacheKey |
||
398 | * @param string $contents |
||
399 | */ |
||
400 | private function saveToCache($cacheKey, $contents) |
||
408 | |||
409 | /** |
||
410 | * @param Request $request |
||
411 | * @return $this |
||
412 | */ |
||
413 | protected function setRequest(Request $request) |
||
419 | |||
420 | } |
||
421 |