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