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', 'no-store']; |
||
| 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 |
||
| 50 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
| 51 | * value |
||
| 52 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
| 53 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
| 54 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
| 55 | * We store a cache item for $cache_lifetime + max age of the response. |
||
| 56 | * @var array $methods list of request methods which can be cached |
||
| 57 | * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses. |
||
| 58 | * } |
||
| 59 | */ |
||
| 60 | 13 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will |
||
| 79 | * cache responses with `private` cache directive. |
||
| 80 | * |
||
| 81 | * @param CacheItemPoolInterface $pool |
||
| 82 | * @param StreamFactory $streamFactory |
||
| 83 | * @param array $config For all possible config options see the constructor docs |
||
| 84 | * |
||
| 85 | * @return CachePlugin |
||
| 86 | */ |
||
| 87 | 1 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * This method will setup the cachePlugin in server cache mode. This is the default caching behavior it refuses to |
||
| 103 | * cache responses with the `private`or `no-cache` directives. |
||
| 104 | * |
||
| 105 | * @param CacheItemPoolInterface $pool |
||
| 106 | * @param StreamFactory $streamFactory |
||
| 107 | * @param array $config For all possible config options see the constructor docs |
||
| 108 | * |
||
| 109 | * @return CachePlugin |
||
| 110 | */ |
||
| 111 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | 10 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
| 197 | * returned is $maxAge. |
||
| 198 | * |
||
| 199 | * @param int|null $maxAge |
||
| 200 | * |
||
| 201 | * @return int|null Unix system time passed to the PSR-6 cache |
||
| 202 | */ |
||
| 203 | 6 | private function calculateCacheItemExpiresAfter($maxAge) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
| 214 | * If-Modified-Since / If-None-Match request to validate the response. |
||
| 215 | * |
||
| 216 | * @param int|null $maxAge |
||
| 217 | * |
||
| 218 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
| 219 | */ |
||
| 220 | 6 | private function calculateResponseExpiresAt($maxAge) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Verify that we can cache this response. |
||
| 231 | * |
||
| 232 | * @param ResponseInterface $response |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | 6 | protected function isCacheable(ResponseInterface $response) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get the value of a parameter in the cache control header. |
||
| 254 | * |
||
| 255 | * @param ResponseInterface $response |
||
| 256 | * @param string $name The field of Cache-Control to fetch |
||
| 257 | * |
||
| 258 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
| 259 | */ |
||
| 260 | 6 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param RequestInterface $request |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 9 | private function createCacheKey(RequestInterface $request) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
| 294 | * |
||
| 295 | * @param ResponseInterface $response |
||
| 296 | * |
||
| 297 | * @return int|null |
||
| 298 | */ |
||
| 299 | 6 | private function getMaxAge(ResponseInterface $response) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Configure an options resolver. |
||
| 327 | * |
||
| 328 | * @param OptionsResolver $resolver |
||
| 329 | */ |
||
| 330 | 13 | private function configureOptions(OptionsResolver $resolver) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @param CacheItemInterface $cacheItem |
||
| 373 | * |
||
| 374 | * @return ResponseInterface |
||
| 375 | */ |
||
| 376 | 2 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Get the value of the "If-Modified-Since" header. |
||
| 389 | * |
||
| 390 | * @param CacheItemInterface $cacheItem |
||
| 391 | * |
||
| 392 | * @return string|null |
||
| 393 | */ |
||
| 394 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Get the ETag from the cached response. |
||
| 410 | * |
||
| 411 | * @param CacheItemInterface $cacheItem |
||
| 412 | * |
||
| 413 | * @return string|null |
||
| 414 | */ |
||
| 415 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
| 433 | } |
||
| 434 |
If you suppress an error, we recommend checking for the error condition explicitly: