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 | * @param CacheItemPoolInterface $pool |
||
39 | * @param StreamFactory $streamFactory |
||
40 | * @param array $config { |
||
41 | * |
||
42 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
43 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
44 | * value |
||
45 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
46 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
47 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
48 | * We store a cache item for $cache_lifetime + max age of the response. |
||
49 | * @var array $methods list of request methods which can be cached. |
||
50 | * } |
||
51 | 12 | */ |
|
52 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
61 | |||
62 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
75 | |||
76 | 8 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
80 | |||
81 | 1 | /** |
|
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
||
159 | |||
160 | /** |
||
161 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
162 | * returned is $maxAge. |
||
163 | * |
||
164 | * @param int|null $maxAge |
||
165 | 5 | * |
|
166 | * @return int|null Unix system time passed to the PSR-6 cache |
||
167 | 5 | */ |
|
168 | private function calculateCacheItemExpiresAfter($maxAge) |
||
176 | |||
177 | /** |
||
178 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
179 | * If-Modified-Since / If-None-Match request to validate the response. |
||
180 | * |
||
181 | 5 | * @param int|null $maxAge |
|
182 | * |
||
183 | 5 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
|
184 | 1 | */ |
|
185 | private function calculateResponseExpiresAt($maxAge) |
||
193 | 4 | ||
194 | /** |
||
195 | * Verify that we can cache this response. |
||
196 | * |
||
197 | * @param ResponseInterface $response |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | protected function isCacheable(ResponseInterface $response) |
||
218 | 5 | ||
219 | /** |
||
220 | * Get the value of a parameter in the cache control header. |
||
221 | * |
||
222 | * @param ResponseInterface $response |
||
223 | * @param string $name The field of Cache-Control to fetch |
||
224 | * |
||
225 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
226 | 8 | */ |
|
227 | private function getCacheControlDirective(ResponseInterface $response, $name) |
||
243 | 5 | ||
244 | /** |
||
245 | 5 | * @param RequestInterface $request |
|
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | private function createCacheKey(RequestInterface $request) |
||
258 | |||
259 | /** |
||
260 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
261 | 4 | * |
|
262 | 4 | * @param ResponseInterface $response |
|
263 | * |
||
264 | 4 | * @return int|null |
|
265 | */ |
||
266 | 4 | private function getMaxAge(ResponseInterface $response) |
|
291 | 12 | ||
292 | /** |
||
293 | 12 | * Configure an options resolver. |
|
294 | 12 | * |
|
295 | 12 | * @param OptionsResolver $resolver |
|
296 | */ |
||
297 | private function configureOptions(OptionsResolver $resolver) |
||
329 | 2 | ||
330 | /** |
||
331 | 2 | * @param CacheItemInterface $cacheItem |
|
332 | * |
||
333 | * @return ResponseInterface |
||
334 | */ |
||
335 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
||
345 | 2 | ||
346 | /** |
||
347 | * Get the value of the "If-Modified-Since" header. |
||
348 | * |
||
349 | 2 | * @param CacheItemInterface $cacheItem |
|
350 | * |
||
351 | * @return string|null |
||
352 | */ |
||
353 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
366 | |||
367 | /** |
||
368 | * Get the ETag from the cached response. |
||
369 | * |
||
370 | * @param CacheItemInterface $cacheItem |
||
371 | * |
||
372 | * @return string|null |
||
373 | */ |
||
374 | private function getETag(CacheItemInterface $cacheItem) |
||
392 | } |
||
393 |
If you suppress an error, we recommend checking for the error condition explicitly: