Complex classes like CachePlugin 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 CachePlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | final class CachePlugin implements Plugin |
||
22 | { |
||
23 | /** |
||
24 | * @var CacheItemPoolInterface |
||
25 | */ |
||
26 | private $pool; |
||
27 | |||
28 | /** |
||
29 | * @var StreamFactory |
||
30 | */ |
||
31 | private $streamFactory; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $config; |
||
37 | |||
38 | /** |
||
39 | * Cache directives indicating if a response can not be cached. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $noCacheFlags = ['no-cache', 'private', 'no-store']; |
||
44 | |||
45 | /** |
||
46 | * @param CacheItemPoolInterface $pool |
||
47 | * @param StreamFactory $streamFactory |
||
48 | * @param array $config { |
||
49 | * |
||
50 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
51 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
52 | * value |
||
53 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
54 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
55 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
56 | * We store a cache item for $cache_lifetime + max age of the response. |
||
57 | * @var array $methods list of request methods which can be cached |
||
58 | * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses. |
||
59 | * } |
||
60 | */ |
||
61 | 13 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
62 | { |
||
63 | 13 | $this->pool = $pool; |
|
64 | 13 | $this->streamFactory = $streamFactory; |
|
65 | |||
66 | 13 | if (isset($config['respect_cache_headers']) && isset($config['respect_response_cache_directives'])) { |
|
67 | throw new \InvalidArgumentException( |
||
68 | 'You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives". '. |
||
69 | 'Use "respect_response_cache_directives" instead.' |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | 13 | $optionsResolver = new OptionsResolver(); |
|
74 | 13 | $this->configureOptions($optionsResolver); |
|
75 | 13 | $this->config = $optionsResolver->resolve($config); |
|
76 | 12 | } |
|
77 | |||
78 | /** |
||
79 | * This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will |
||
80 | * cache responses with `private` cache directive. |
||
81 | * |
||
82 | * @param CacheItemPoolInterface $pool |
||
83 | * @param StreamFactory $streamFactory |
||
84 | * @param array $config For all possible config options see the constructor docs |
||
85 | * |
||
86 | * @return CachePlugin |
||
87 | */ |
||
88 | 1 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
89 | { |
||
90 | // Allow caching of private requests |
||
91 | 1 | if (isset($config['respect_response_cache_directives'])) { |
|
92 | $config['respect_response_cache_directives'][] = 'no-cache'; |
||
93 | $config['respect_response_cache_directives'][] = 'max-age'; |
||
94 | $config['respect_response_cache_directives'] = array_unique($config['respect_response_cache_directives']); |
||
95 | } else { |
||
96 | 1 | $config['respect_response_cache_directives'] = ['no-cache', 'max-age']; |
|
97 | } |
||
98 | |||
99 | 1 | return new self($pool, $streamFactory, $config); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * This method will setup the cachePlugin in server cache mode. This is the default caching behavior it refuses to |
||
104 | * cache responses with the `private`or `no-cache` directives. |
||
105 | * |
||
106 | * @param CacheItemPoolInterface $pool |
||
107 | * @param StreamFactory $streamFactory |
||
108 | * @param array $config For all possible config options see the constructor docs |
||
109 | * |
||
110 | * @return CachePlugin |
||
111 | */ |
||
112 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 10 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
195 | |||
196 | /** |
||
197 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
198 | * returned is $maxAge. |
||
199 | * |
||
200 | * @param int|null $maxAge |
||
201 | * |
||
202 | * @return int|null Unix system time passed to the PSR-6 cache |
||
203 | */ |
||
204 | 6 | private function calculateCacheItemExpiresAfter($maxAge) |
|
212 | |||
213 | /** |
||
214 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
215 | * If-Modified-Since / If-None-Match request to validate the response. |
||
216 | * |
||
217 | * @param int|null $maxAge |
||
218 | * |
||
219 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
220 | */ |
||
221 | 6 | private function calculateResponseExpiresAt($maxAge) |
|
229 | |||
230 | /** |
||
231 | * Verify that we can cache this response. |
||
232 | * |
||
233 | * @param ResponseInterface $response |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | 6 | protected function isCacheable(ResponseInterface $response) |
|
252 | |||
253 | /** |
||
254 | * Get the value of a parameter in the cache control header. |
||
255 | * |
||
256 | * @param ResponseInterface $response |
||
257 | * @param string $name The field of Cache-Control to fetch |
||
258 | * |
||
259 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
260 | */ |
||
261 | 6 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
277 | |||
278 | /** |
||
279 | * @param RequestInterface $request |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | 9 | private function createCacheKey(RequestInterface $request) |
|
292 | |||
293 | /** |
||
294 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
295 | * |
||
296 | * @param ResponseInterface $response |
||
297 | * |
||
298 | * @return int|null |
||
299 | */ |
||
300 | 6 | private function getMaxAge(ResponseInterface $response) |
|
325 | |||
326 | /** |
||
327 | * Configure an options resolver. |
||
328 | * |
||
329 | * @param OptionsResolver $resolver |
||
330 | */ |
||
331 | 13 | private function configureOptions(OptionsResolver $resolver) |
|
371 | |||
372 | /** |
||
373 | * @param CacheItemInterface $cacheItem |
||
374 | * |
||
375 | * @return ResponseInterface |
||
376 | */ |
||
377 | 2 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
395 | |||
396 | /** |
||
397 | * Get the value of the "If-Modified-Since" header. |
||
398 | * |
||
399 | * @param CacheItemInterface $cacheItem |
||
400 | * |
||
401 | * @return string|null |
||
402 | */ |
||
403 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
416 | |||
417 | /** |
||
418 | * Get the ETag from the cached response. |
||
419 | * |
||
420 | * @param CacheItemInterface $cacheItem |
||
421 | * |
||
422 | * @return string|null |
||
423 | */ |
||
424 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
442 | } |
||
443 |
If you suppress an error, we recommend checking for the error condition explicitly: