Complex classes like CacheHandler 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 CacheHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class CacheHandler |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var \Concat\Cache\CacheInterface Cache provider. |
||
25 | */ |
||
26 | protected $cache; |
||
27 | |||
28 | /** |
||
29 | * @var callable Default handler used to send response. |
||
30 | */ |
||
31 | protected $handler; |
||
32 | |||
33 | /** |
||
34 | * @var \Psr\Log\LoggerInterface PSR-3 compliant logger. |
||
35 | */ |
||
36 | protected $logger; |
||
37 | |||
38 | /** |
||
39 | * @var string|callable Constant or callable that accepts a Response. |
||
40 | */ |
||
41 | protected $logLevel; |
||
42 | |||
43 | /** |
||
44 | * @var string Log template. |
||
45 | */ |
||
46 | protected $logTemplate; |
||
47 | |||
48 | /** |
||
49 | * @var array Configuration options. |
||
50 | */ |
||
51 | protected $options; |
||
52 | |||
53 | /** |
||
54 | * @var bool Whether the most recent request was fetched from the cache. |
||
55 | */ |
||
56 | private $fetchedFromCache = false; |
||
57 | |||
58 | /** |
||
59 | * Constructs a new cache handler. |
||
60 | * |
||
61 | * @param object $cache Cache provider. |
||
62 | * @param callable $handler Default handler used to send response. |
||
63 | * @param array $options Configuration options. |
||
64 | */ |
||
65 | public function __construct($cache, $handler = null, array $options = []) |
||
73 | |||
74 | /** |
||
75 | * Sets the fallback handler to use when the cache is invalid. |
||
76 | * |
||
77 | * @param callable $handler |
||
78 | * |
||
79 | * @codeCoverageIgnore |
||
80 | */ |
||
81 | public function setHandler(callable $handler) |
||
85 | |||
86 | /** |
||
87 | * Sets the cache provider. |
||
88 | * |
||
89 | * @param object $cache |
||
90 | */ |
||
91 | public function setCacheProvider($cache) |
||
95 | |||
96 | /** |
||
97 | * Resets the options, merged with default values. |
||
98 | * |
||
99 | * @param array $options |
||
100 | */ |
||
101 | public function setOptions(array $options) |
||
105 | |||
106 | /** |
||
107 | * Sets the logger. |
||
108 | * |
||
109 | * @param LoggerInterface $logger |
||
110 | */ |
||
111 | public function setLogger(LoggerInterface $logger) |
||
115 | |||
116 | /** |
||
117 | * Sets the template to use when logging cache events. |
||
118 | * |
||
119 | * @param string $logTemplate |
||
120 | */ |
||
121 | public function setLogTemplate($logTemplate) |
||
125 | |||
126 | /** |
||
127 | * Returns a defined log template, or a default template otherwise. |
||
128 | * |
||
129 | * @return string Log template. |
||
130 | */ |
||
131 | protected function getLogTemplate() |
||
139 | |||
140 | /** |
||
141 | * Returns the default handler, used if a handler is not set. |
||
142 | * |
||
143 | * @return callable |
||
144 | * @codeCoverageIgnore |
||
145 | */ |
||
146 | protected function getDefaultHandler() |
||
150 | |||
151 | /** |
||
152 | * Returns the default confiration options. |
||
153 | * |
||
154 | * @return array The default configuration options. |
||
155 | */ |
||
156 | protected function getDefaultOptions() |
||
170 | |||
171 | /** |
||
172 | * Called when a request is made on the client. |
||
173 | * |
||
174 | * @return PromiseInterface |
||
175 | */ |
||
176 | public function __invoke(Request $request, array $options) |
||
184 | |||
185 | /** |
||
186 | * Attempts to fetch, otherwise promises to cache a response when the |
||
187 | * default handler fulfills its promise. |
||
188 | * |
||
189 | * @param Request $request The request to cache. |
||
190 | * @param array $options Configuration options. |
||
191 | * |
||
192 | * @return PromiseInterface |
||
193 | */ |
||
194 | protected function cache(Request $request, array $options) |
||
210 | |||
211 | /** |
||
212 | * Makes the request and stores it in the cache if required to. |
||
213 | * |
||
214 | * @param Request $request |
||
215 | * @param array $options |
||
216 | * @param string $key |
||
217 | * |
||
218 | * @return PromiseInterface |
||
219 | */ |
||
220 | protected function request(Request $request, array $options, $key) |
||
246 | |||
247 | /** |
||
248 | * Returns true if the most recent request was fetched from the cache, or |
||
249 | * false if the request was made (regardless of whether it was then cached). |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function lastRequestWasFetchedFromCache() |
||
257 | |||
258 | /** |
||
259 | * Attempts to fetch a response bundle from the cache for the given key. |
||
260 | * |
||
261 | * @param Request $request |
||
262 | * @param string $key |
||
263 | * |
||
264 | * @return Response|null A response null if invalid. |
||
265 | */ |
||
266 | protected function fetch(Request $request, $key) |
||
275 | |||
276 | /** |
||
277 | * Fetches a response bundle from the cache for a given key. |
||
278 | * |
||
279 | * @param string $key The key to fetch. |
||
280 | * |
||
281 | * @return array|null Bundle from cache or null if expired. |
||
282 | */ |
||
283 | protected function fetchBundle($key) |
||
299 | |||
300 | /** |
||
301 | * Builds and stores a cache bundle. |
||
302 | * |
||
303 | * @param Request $request |
||
304 | * @param Response $response |
||
305 | * @param string $key |
||
306 | * |
||
307 | * @throws RuntimeException if it fails to store the response in the cache. |
||
308 | */ |
||
309 | protected function store(Request $request, Response $response, $key) |
||
323 | |||
324 | /** |
||
325 | * Builds a cache bundle using a given response. |
||
326 | * |
||
327 | * @param Response $response |
||
328 | * |
||
329 | * @return array The response bundle to cache. |
||
330 | */ |
||
331 | protected function buildCacheBundle(Response $response) |
||
338 | |||
339 | /** |
||
340 | * Filters the request using a configured filter to determine if it should |
||
341 | * be cached. |
||
342 | * |
||
343 | * @param Request $request The request to filter. |
||
344 | * |
||
345 | * @return bool true if should be cached, false otherwise. |
||
346 | */ |
||
347 | protected function filter(Request $request) |
||
352 | |||
353 | /** |
||
354 | * Checks the method of the request to determine if it should be cached. |
||
355 | * |
||
356 | * @param Request $request The request to check. |
||
357 | * |
||
358 | * @return bool true if should be cached, false otherwise. |
||
359 | */ |
||
360 | protected function checkMethod(Request $request) |
||
365 | |||
366 | /** |
||
367 | * Returns true if the given request should be cached. |
||
368 | * |
||
369 | * @param Request $request The request to check. |
||
370 | * |
||
371 | * @return bool true if the request should be cached, false otherwise. |
||
372 | */ |
||
373 | private function shouldCacheRequest(Request $request) |
||
377 | |||
378 | /** |
||
379 | * Determines if a response should be cached. |
||
380 | * |
||
381 | * @param Response $response |
||
382 | */ |
||
383 | protected function shouldCacheResponse(Response $response) |
||
387 | |||
388 | /** |
||
389 | * Generates the cache key for the given request and request options. The |
||
390 | * namespace should be set on the cache provider. |
||
391 | * |
||
392 | * @param Request $request The request to generate a key for. |
||
393 | * @param array $options Configuration options. |
||
394 | * |
||
395 | * @return string The cache key |
||
396 | */ |
||
397 | protected function getKey(Request $request, array $options) |
||
405 | |||
406 | /** |
||
407 | * Invokes the default handler to produce a promise. |
||
408 | * |
||
409 | * @param Request $request |
||
410 | * @param array $options |
||
411 | * |
||
412 | * @return PromiseInterface |
||
413 | */ |
||
414 | protected function invokeDefault(Request $request, array $options) |
||
418 | |||
419 | /** |
||
420 | * Returns the default log level to use when logging response bundles. |
||
421 | * |
||
422 | * @return string LogLevel |
||
423 | */ |
||
424 | protected function getDefaultLogLevel() |
||
428 | |||
429 | /** |
||
430 | * Sets the log level to use, which can be either a string or a callable |
||
431 | * that accepts a response (which could be null). A log level could also |
||
432 | * be null, which indicates that the default log level should be used. |
||
433 | * |
||
434 | * @param string|callable|null |
||
435 | */ |
||
436 | public function setLogLevel($logLevel) |
||
440 | |||
441 | /** |
||
442 | * Returns a log level for a given response. |
||
443 | * |
||
444 | * @param Response $response The response being logged. |
||
445 | * |
||
446 | * @return string LogLevel |
||
447 | */ |
||
448 | protected function getLogLevel(Response $response) |
||
460 | |||
461 | /** |
||
462 | * Convenient internal logger entry point. |
||
463 | * |
||
464 | * @param string $message |
||
465 | * @param array $bundle |
||
466 | */ |
||
467 | private function log($message, array $bundle) |
||
474 | |||
475 | /** |
||
476 | * Logs that a bundle has been stored in the cache. |
||
477 | * |
||
478 | * @param Request $request The request. |
||
479 | * @param array $bundle The stored response bundle. |
||
480 | */ |
||
481 | protected function logStoredBundle(Request $request, array $bundle) |
||
486 | |||
487 | /** |
||
488 | * Logs that a bundle has been fetched from the cache. |
||
489 | * |
||
490 | * @param Request $request The request that produced the response. |
||
491 | * @param array $bundle The fetched response bundle. |
||
492 | */ |
||
493 | protected function logFetchedBundle(Request $request, array $bundle) |
||
498 | |||
499 | /** |
||
500 | * Prepares a log template with optional extra fields. |
||
501 | * |
||
502 | * @param array $extras |
||
503 | * |
||
504 | * @return string $template |
||
505 | */ |
||
506 | protected function prepareTemplate(array $extras) |
||
516 | |||
517 | /** |
||
518 | * Formats a request and response as a log message. |
||
519 | * |
||
520 | * @param Request $request |
||
521 | * @param array $bundle |
||
522 | * @param string $event |
||
523 | * |
||
524 | * @return string The formatted message. |
||
525 | */ |
||
526 | protected function getLogMessage(Request $request, array $bundle, $event) |
||
538 | |||
539 | /** |
||
540 | * Returns the log message for when a bundle is stored in the cache. |
||
541 | * |
||
542 | * @param Request $request The request that produced the response. |
||
543 | * @param array $bundle The stored response bundle. |
||
544 | * |
||
545 | * @return string The log message. |
||
546 | */ |
||
547 | protected function getStoredLogMessage(Request $request, array $bundle) |
||
551 | |||
552 | /** |
||
553 | * Returns the log message for when a bundle is fetched from the cache. |
||
554 | * |
||
555 | * @param Request $request The request that produced the response. |
||
556 | * @param array $bundle The stored response bundle. |
||
557 | * |
||
558 | * @return string The log message. |
||
559 | */ |
||
560 | protected function getFetchedLogMessage(Request $request, array $bundle) |
||
564 | } |
||
565 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.