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 |
||
20 | final class CachePlugin implements Plugin |
||
21 | { |
||
22 | /** |
||
23 | * @var CacheItemPoolInterface |
||
24 | */ |
||
25 | private $pool; |
||
26 | |||
27 | /** |
||
28 | * @var StreamFactory |
||
29 | */ |
||
30 | private $streamFactory; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $config; |
||
36 | |||
37 | /** |
||
38 | * Cache directives indicating if a response can not be cached. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $noCacheFlags = ['no-cache', 'private']; |
||
43 | |||
44 | /** |
||
45 | * @param CacheItemPoolInterface $pool |
||
46 | * @param StreamFactory $streamFactory |
||
47 | * @param array $config { |
||
48 | * |
||
49 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them. This option is |
||
50 | * deprecated, use `respect_response_cache_directives` instead |
||
51 | 12 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
|
52 | * value |
||
53 | 12 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
|
54 | 12 | * @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 | 12 | * We store a cache item for $cache_lifetime + max age of the response. |
|
57 | 12 | * @var array $methods list of request methods which can be cached |
|
58 | 12 | * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses. |
|
59 | 11 | * }. |
|
60 | */ |
||
61 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
77 | 3 | ||
78 | /** |
||
79 | 3 | * 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 | 1 | * |
|
82 | * @param CacheItemPoolInterface $pool |
||
83 | * @param StreamFactory $streamFactory |
||
84 | * @param array $config For all possible config options see the constructor docs |
||
85 | 2 | * |
|
86 | 2 | * @return CachePlugin |
|
87 | 2 | */ |
|
88 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
101 | 1 | ||
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 | 1 | * |
|
106 | 1 | * @param CacheItemPoolInterface $pool |
|
107 | 1 | * @param StreamFactory $streamFactory |
|
108 | 1 | * @param array $config For all possible config options see the constructor docs |
|
109 | 1 | * |
|
110 | * @return CachePlugin |
||
111 | 1 | */ |
|
112 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
116 | 4 | ||
117 | 4 | /** |
|
118 | 4 | * {@inheritdoc} |
|
119 | 4 | */ |
|
120 | 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 | 5 | 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 | 5 | * |
|
217 | * @param int|null $maxAge |
||
218 | 5 | * |
|
219 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
220 | */ |
||
221 | private function calculateResponseExpiresAt($maxAge) |
||
229 | 8 | ||
230 | 1 | /** |
|
231 | 1 | * Verify that we can cache this response. |
|
232 | * |
||
233 | 8 | * @param ResponseInterface $response |
|
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | protected function isCacheable(ResponseInterface $response) |
||
252 | 1 | ||
253 | 1 | /** |
|
254 | 1 | * 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 | 4 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
277 | 12 | ||
278 | 12 | /** |
|
279 | 12 | * @param RequestInterface $request |
|
280 | 12 | * |
|
281 | 12 | * @return string |
|
282 | 12 | */ |
|
283 | private function createCacheKey(RequestInterface $request) |
||
292 | |||
293 | 12 | /** |
|
294 | 12 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
|
295 | 12 | * |
|
296 | * @param ResponseInterface $response |
||
297 | * |
||
298 | * @return int|null |
||
299 | */ |
||
300 | private function getMaxAge(ResponseInterface $response) |
||
325 | |||
326 | /** |
||
327 | * Configure an options resolver. |
||
328 | 2 | * |
|
329 | 2 | * @param OptionsResolver $resolver |
|
330 | */ |
||
331 | 2 | private function configureOptions(OptionsResolver $resolver) |
|
371 | |||
372 | /** |
||
373 | * @param CacheItemInterface $cacheItem |
||
374 | * |
||
375 | * @return ResponseInterface |
||
376 | */ |
||
377 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
||
387 | |||
388 | /** |
||
389 | * Get the value of the "If-Modified-Since" header. |
||
390 | * |
||
391 | * @param CacheItemInterface $cacheItem |
||
392 | * |
||
393 | * @return string|null |
||
394 | */ |
||
395 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
||
408 | |||
409 | /** |
||
410 | * Get the ETag from the cached response. |
||
411 | * |
||
412 | * @param CacheItemInterface $cacheItem |
||
413 | * |
||
414 | * @return string|null |
||
415 | */ |
||
416 | private function getETag(CacheItemInterface $cacheItem) |
||
434 | } |
||
435 |
If you suppress an error, we recommend checking for the error condition explicitly: