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 | * } |
||
| 49 | */ |
||
| 50 | 10 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | 8 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
| 141 | * returned is $maxAge. |
||
| 142 | * |
||
| 143 | * @param int|null $maxAge |
||
| 144 | * |
||
| 145 | * @return int|null Unix system time passed to the PSR-6 cache |
||
| 146 | */ |
||
| 147 | 4 | private function calculateCacheItemExpiresAfter($maxAge) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
| 158 | * If-Modified-Since / If-None-Match request to validate the response. |
||
| 159 | * |
||
| 160 | * @param int|null $maxAge |
||
| 161 | * |
||
| 162 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
| 163 | */ |
||
| 164 | 4 | private function calculateResponseExpiresAt($maxAge) |
|
| 165 | { |
||
| 166 | 4 | if ($maxAge === null) { |
|
| 167 | return; |
||
| 168 | } |
||
| 169 | |||
| 170 | 4 | return time() + $maxAge; |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Verify that we can cache this response. |
||
| 175 | * |
||
| 176 | * @param ResponseInterface $response |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | 4 | protected function isCacheable(ResponseInterface $response) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get the value of a parameter in the cache control header. |
||
| 197 | * |
||
| 198 | * @param ResponseInterface $response |
||
| 199 | * @param string $name The field of Cache-Control to fetch |
||
| 200 | * |
||
| 201 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
| 202 | */ |
||
| 203 | 4 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param RequestInterface $request |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | 7 | private function createCacheKey(RequestInterface $request) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
| 233 | * |
||
| 234 | * @param ResponseInterface $response |
||
| 235 | * |
||
| 236 | * @return int|null |
||
| 237 | */ |
||
| 238 | 4 | private function getMaxAge(ResponseInterface $response) |
|
| 239 | { |
||
| 240 | 4 | if (!$this->config['respect_cache_headers']) { |
|
| 241 | return $this->config['default_ttl']; |
||
| 242 | } |
||
| 243 | |||
| 244 | // check for max age in the Cache-Control header |
||
| 245 | 4 | $maxAge = $this->getCacheControlDirective($response, 'max-age'); |
|
| 246 | 4 | if (!is_bool($maxAge)) { |
|
| 247 | 1 | $ageHeaders = $response->getHeader('Age'); |
|
| 248 | 1 | foreach ($ageHeaders as $age) { |
|
| 249 | 1 | return $maxAge - ((int) $age); |
|
| 250 | } |
||
| 251 | |||
| 252 | return (int) $maxAge; |
||
| 253 | } |
||
| 254 | |||
| 255 | // check for ttl in the Expires header |
||
| 256 | 3 | $headers = $response->getHeader('Expires'); |
|
| 257 | 3 | foreach ($headers as $header) { |
|
| 258 | return (new \DateTime($header))->getTimestamp() - (new \DateTime())->getTimestamp(); |
||
| 259 | 3 | } |
|
| 260 | |||
| 261 | 3 | return $this->config['default_ttl']; |
|
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Configure an options resolver. |
||
| 266 | * |
||
| 267 | * @param OptionsResolver $resolver |
||
| 268 | */ |
||
| 269 | 10 | private function configureOptions(OptionsResolver $resolver) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param CacheItemInterface $cacheItem |
||
| 286 | * |
||
| 287 | * @return ResponseInterface |
||
| 288 | */ |
||
| 289 | 2 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get the value of the "If-Modified-Since" header. |
||
| 302 | * |
||
| 303 | * @param CacheItemInterface $cacheItem |
||
| 304 | * |
||
| 305 | * @return string|null |
||
| 306 | */ |
||
| 307 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get the ETag from the cached response. |
||
| 323 | * |
||
| 324 | * @param CacheItemInterface $cacheItem |
||
| 325 | * |
||
| 326 | * @return string|null |
||
| 327 | */ |
||
| 328 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
| 346 | } |
||
| 347 |