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 |
||
| 19 | final class CachePlugin implements Plugin |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var CacheItemPoolInterface |
||
| 23 | */ |
||
| 24 | private $pool; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var StreamFactory |
||
| 28 | */ |
||
| 29 | private $streamFactory; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $config; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param CacheItemPoolInterface $pool |
||
| 38 | * @param StreamFactory $streamFactory |
||
| 39 | * @param array $config { |
||
| 40 | * |
||
| 41 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
| 42 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
| 43 | * value |
||
| 44 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
| 45 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
| 46 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
| 47 | * We store a cache item for $cache_lifetime + max age of the response. |
||
| 48 | * @var array $methods list of request methods which can be cached. |
||
| 49 | * } |
||
| 50 | */ |
||
| 51 | 12 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 60 | |||
| 61 | 1 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 68 | |||
| 69 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | 9 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
| 155 | * returned is $maxAge. |
||
| 156 | * |
||
| 157 | * @param int|null $maxAge |
||
| 158 | * |
||
| 159 | * @return int|null Unix system time passed to the PSR-6 cache |
||
| 160 | */ |
||
| 161 | 5 | private function calculateCacheItemExpiresAfter($maxAge) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
| 172 | * If-Modified-Since / If-None-Match request to validate the response. |
||
| 173 | * |
||
| 174 | * @param int|null $maxAge |
||
| 175 | * |
||
| 176 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
| 177 | */ |
||
| 178 | 5 | private function calculateResponseExpiresAt($maxAge) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Verify that we can cache this response. |
||
| 189 | * |
||
| 190 | * @param ResponseInterface $response |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | 5 | protected function isCacheable(ResponseInterface $response) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Get the value of a parameter in the cache control header. |
||
| 214 | * |
||
| 215 | * @param ResponseInterface $response |
||
| 216 | * @param string $name The field of Cache-Control to fetch |
||
| 217 | * |
||
| 218 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
| 219 | */ |
||
| 220 | 5 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param RequestInterface $request |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | 8 | private function createCacheKey(RequestInterface $request) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
| 254 | * |
||
| 255 | * @param ResponseInterface $response |
||
| 256 | * |
||
| 257 | * @return int|null |
||
| 258 | */ |
||
| 259 | 5 | private function getMaxAge(ResponseInterface $response) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Configure an options resolver. |
||
| 287 | * |
||
| 288 | * @param OptionsResolver $resolver |
||
| 289 | */ |
||
| 290 | 12 | private function configureOptions(OptionsResolver $resolver) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @param CacheItemInterface $cacheItem |
||
| 316 | * |
||
| 317 | * @return ResponseInterface |
||
| 318 | */ |
||
| 319 | 2 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Get the value of the "If-Modified-Since" header. |
||
| 332 | * |
||
| 333 | * @param CacheItemInterface $cacheItem |
||
| 334 | * |
||
| 335 | * @return string|null |
||
| 336 | */ |
||
| 337 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get the ETag from the cached response. |
||
| 353 | * |
||
| 354 | * @param CacheItemInterface $cacheItem |
||
| 355 | * |
||
| 356 | * @return string|null |
||
| 357 | */ |
||
| 358 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
| 376 | } |
||
| 377 |