Complex classes like CacheMiddleware 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 CacheMiddleware, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class CacheMiddleware |
||
19 | { |
||
20 | const HEADER_RE_VALIDATION = 'X-Kevinrob-GuzzleCache-ReValidation'; |
||
21 | const HEADER_CACHE_INFO = 'X-Kevinrob-Cache'; |
||
22 | const HEADER_CACHE_HIT = 'HIT'; |
||
23 | const HEADER_CACHE_MISS = 'MISS'; |
||
24 | const HEADER_CACHE_STALE = 'STALE'; |
||
25 | |||
26 | /** |
||
27 | * @var array of Promise |
||
28 | */ |
||
29 | protected $waitingRevalidate = []; |
||
30 | |||
31 | /** |
||
32 | * @var Client |
||
33 | */ |
||
34 | protected $client; |
||
35 | |||
36 | /** |
||
37 | * @var CacheStrategyInterface |
||
38 | */ |
||
39 | protected $cacheStorage; |
||
40 | |||
41 | /** |
||
42 | * List of allowed HTTP methods to cache |
||
43 | * Key = method name (upscaling) |
||
44 | * Value = true. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $httpMethods = ['GET' => true]; |
||
49 | |||
50 | /** |
||
51 | * @param CacheStrategyInterface|null $cacheStrategy |
||
52 | */ |
||
53 | public function __construct(CacheStrategyInterface $cacheStrategy = null) |
||
54 | { |
||
55 | $this->cacheStorage = $cacheStrategy !== null ? $cacheStrategy : new PrivateCacheStrategy(); |
||
56 | |||
57 | register_shutdown_function([$this, 'purgeReValidation']); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param Client $client |
||
62 | */ |
||
63 | public function setClient(Client $client) |
||
67 | |||
68 | /** |
||
69 | * @param CacheStrategyInterface $cacheStorage |
||
70 | */ |
||
71 | public function setCacheStorage(CacheStrategyInterface $cacheStorage) |
||
75 | |||
76 | /** |
||
77 | * @return CacheStrategyInterface |
||
78 | */ |
||
79 | public function getCacheStorage() |
||
83 | |||
84 | /** |
||
85 | * @param array $methods |
||
86 | */ |
||
87 | public function setHttpMethods(array $methods) |
||
91 | |||
92 | public function getHttpMethods() |
||
96 | |||
97 | /** |
||
98 | * Will be called at the end of the script. |
||
99 | */ |
||
100 | public function purgeReValidation() |
||
104 | |||
105 | /** |
||
106 | * @param callable $handler |
||
107 | * |
||
108 | * @return callable |
||
109 | */ |
||
110 | public function __invoke(callable $handler) |
||
220 | |||
221 | /** |
||
222 | * @param CacheStrategyInterface $cache |
||
223 | * @param RequestInterface $request |
||
224 | * @param ResponseInterface $response |
||
225 | * @return ResponseInterface |
||
226 | */ |
||
227 | protected static function addToCache( |
||
243 | |||
244 | /** |
||
245 | * @param RequestInterface $request |
||
246 | * @param CacheStrategyInterface $cacheStorage |
||
247 | * @param CacheEntry $cacheEntry |
||
248 | * |
||
249 | * @return bool if added |
||
250 | */ |
||
251 | protected function addReValidationRequest( |
||
278 | |||
279 | /** |
||
280 | * @param CacheEntry|null $cacheEntry |
||
281 | * |
||
282 | * @return null|ResponseInterface |
||
283 | */ |
||
284 | protected static function getStaleResponse(CacheEntry $cacheEntry = null) |
||
294 | |||
295 | /** |
||
296 | * @param RequestInterface $request |
||
297 | * @param CacheEntry $cacheEntry |
||
298 | * |
||
299 | * @return RequestInterface |
||
300 | */ |
||
301 | protected static function getRequestWithReValidationHeader(RequestInterface $request, CacheEntry $cacheEntry) |
||
318 | |||
319 | /** |
||
320 | * @param CacheStrategyInterface|null $cacheStorage |
||
321 | * |
||
322 | * @return CacheMiddleware the Middleware for Guzzle HandlerStack |
||
323 | * |
||
324 | * @deprecated Use constructor => `new CacheMiddleware()` |
||
325 | */ |
||
326 | public static function getMiddleware(CacheStrategyInterface $cacheStorage = null) |
||
330 | } |
||
331 |