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 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
| 52 | * value |
||
| 53 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
| 54 | * @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 | * We store a cache item for $cache_lifetime + max age of the response. |
||
| 57 | * @var array $methods list of request methods which can be cached |
||
| 58 | * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses. |
||
| 59 | * }. |
||
| 60 | */ |
||
| 61 | 13 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will |
||
| 77 | * cache responses with `private` cache directive. |
||
| 78 | * |
||
| 79 | * @param CacheItemPoolInterface $pool |
||
| 80 | * @param StreamFactory $streamFactory |
||
| 81 | * @param array $config For all possible config options see the constructor docs |
||
| 82 | * |
||
| 83 | * @return CachePlugin |
||
| 84 | */ |
||
| 85 | 1 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 101 | |||
| 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 | * |
||
| 106 | * @param CacheItemPoolInterface $pool |
||
| 107 | * @param StreamFactory $streamFactory |
||
| 108 | * @param array $config For all possible config options see the constructor docs |
||
| 109 | * |
||
| 110 | * @return CachePlugin |
||
| 111 | */ |
||
| 112 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | 10 | 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 | 6 | 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 | * |
||
| 217 | * @param int|null $maxAge |
||
| 218 | * |
||
| 219 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
| 220 | */ |
||
| 221 | 6 | private function calculateResponseExpiresAt($maxAge) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Verify that we can cache this response. |
||
| 232 | * |
||
| 233 | * @param ResponseInterface $response |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 6 | protected function isCacheable(ResponseInterface $response) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * 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 | 6 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param RequestInterface $request |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | 9 | private function createCacheKey(RequestInterface $request) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
| 295 | * |
||
| 296 | * @param ResponseInterface $response |
||
| 297 | * |
||
| 298 | * @return int|null |
||
| 299 | */ |
||
| 300 | 6 | private function getMaxAge(ResponseInterface $response) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Configure an options resolver. |
||
| 328 | * |
||
| 329 | * @param OptionsResolver $resolver |
||
| 330 | */ |
||
| 331 | 13 | private function configureOptions(OptionsResolver $resolver) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param CacheItemInterface $cacheItem |
||
| 374 | * |
||
| 375 | * @return ResponseInterface |
||
| 376 | */ |
||
| 377 | 2 | 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 | 2 | 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 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
| 434 | |||
| 435 | 5 | private function extractDirectives($directives, $extractDirectivesList) |
|
| 439 | } |
||
| 440 |
If you suppress an error, we recommend checking for the error condition explicitly: