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 |
||
24 | final class CachePlugin implements Plugin |
||
25 | { |
||
26 | /** |
||
27 | * @var CacheItemPoolInterface |
||
28 | */ |
||
29 | private $pool; |
||
30 | |||
31 | /** |
||
32 | * @var StreamFactory |
||
33 | */ |
||
34 | private $streamFactory; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $config; |
||
40 | |||
41 | /** |
||
42 | * Cache directives indicating if a response can not be cached. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | private $noCacheFlags = ['no-cache', 'private', 'no-store']; |
||
47 | |||
48 | /** |
||
49 | * @param CacheItemPoolInterface $pool |
||
50 | * @param StreamFactory $streamFactory |
||
51 | * @param array $config { |
||
52 | * |
||
53 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
54 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
55 | * value |
||
56 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
57 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
58 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
59 | * We store a cache item for $cache_lifetime + max age of the response. |
||
60 | * @var array $methods list of request methods which can be cached |
||
61 | * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses |
||
62 | * @var CacheKeyGenerator $cache_key_generator an object to generate the cache key. Defaults to a new instance of SimpleGenerator |
||
63 | * @var CacheListener[] $cache_listeners an array of objects to act on the response based on the results of the cache check. |
||
64 | * Defaults to an empty array |
||
65 | * } |
||
66 | */ |
||
67 | 14 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
87 | |||
88 | /** |
||
89 | * This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will |
||
90 | * cache responses with `private` cache directive. |
||
91 | * |
||
92 | * @param CacheItemPoolInterface $pool |
||
93 | * @param StreamFactory $streamFactory |
||
94 | * @param array $config For all possible config options see the constructor docs |
||
95 | * |
||
96 | * @return CachePlugin |
||
97 | */ |
||
98 | 2 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
111 | |||
112 | /** |
||
113 | * This method will setup the cachePlugin in server cache mode. This is the default caching behavior it refuses to |
||
114 | * cache responses with the `private`or `no-cache` directives. |
||
115 | * |
||
116 | * @param CacheItemPoolInterface $pool |
||
117 | * @param StreamFactory $streamFactory |
||
118 | * @param array $config For all possible config options see the constructor docs |
||
119 | * |
||
120 | * @return CachePlugin |
||
121 | */ |
||
122 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 11 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
212 | |||
213 | /** |
||
214 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
215 | * returned is $maxAge. |
||
216 | * |
||
217 | * @param int|null $maxAge |
||
218 | * |
||
219 | * @return int|null Unix system time passed to the PSR-6 cache |
||
220 | */ |
||
221 | 6 | private function calculateCacheItemExpiresAfter($maxAge) |
|
229 | |||
230 | /** |
||
231 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
232 | * If-Modified-Since / If-None-Match request to validate the response. |
||
233 | * |
||
234 | * @param int|null $maxAge |
||
235 | * |
||
236 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
237 | */ |
||
238 | 6 | private function calculateResponseExpiresAt($maxAge) |
|
246 | |||
247 | /** |
||
248 | * Verify that we can cache this response. |
||
249 | * |
||
250 | * @param ResponseInterface $response |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | 6 | protected function isCacheable(ResponseInterface $response) |
|
269 | |||
270 | /** |
||
271 | * Get the value of a parameter in the cache control header. |
||
272 | * |
||
273 | * @param ResponseInterface $response |
||
274 | * @param string $name The field of Cache-Control to fetch |
||
275 | * |
||
276 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
277 | */ |
||
278 | 6 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
294 | |||
295 | /** |
||
296 | * @param RequestInterface $request |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | 10 | private function createCacheKey(RequestInterface $request) |
|
306 | |||
307 | /** |
||
308 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
309 | * |
||
310 | * @param ResponseInterface $response |
||
311 | * |
||
312 | * @return int|null |
||
313 | */ |
||
314 | 6 | private function getMaxAge(ResponseInterface $response) |
|
339 | |||
340 | /** |
||
341 | * Configure an options resolver. |
||
342 | * |
||
343 | * @param OptionsResolver $resolver |
||
344 | */ |
||
345 | 14 | private function configureOptions(OptionsResolver $resolver) |
|
389 | |||
390 | /** |
||
391 | * @param CacheItemInterface $cacheItem |
||
392 | * |
||
393 | * @return ResponseInterface |
||
394 | */ |
||
395 | 3 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
413 | |||
414 | /** |
||
415 | * Get the value of the "If-Modified-Since" header. |
||
416 | * |
||
417 | * @param CacheItemInterface $cacheItem |
||
418 | * |
||
419 | * @return string|null |
||
420 | */ |
||
421 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
434 | |||
435 | /** |
||
436 | * Get the ETag from the cached response. |
||
437 | * |
||
438 | * @param CacheItemInterface $cacheItem |
||
439 | * |
||
440 | * @return string|null |
||
441 | */ |
||
442 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
456 | |||
457 | /** |
||
458 | * Call the cache listeners, if they are set. |
||
459 | * |
||
460 | * @param RequestInterface $request |
||
461 | * @param ResponseInterface $response |
||
462 | * @param bool $cacheHit |
||
463 | * @param CacheItemInterface|null $cacheItem |
||
464 | * |
||
465 | * @return ResponseInterface |
||
466 | */ |
||
467 | 11 | private function handleCacheListeners(RequestInterface $request, ResponseInterface $response, $cacheHit, $cacheItem) |
|
475 | } |
||
476 |
If you suppress an error, we recommend checking for the error condition explicitly: