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 |
||
| 23 | final class CachePlugin implements Plugin |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var CacheItemPoolInterface |
||
| 27 | */ |
||
| 28 | private $pool; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var StreamFactory |
||
| 32 | */ |
||
| 33 | private $streamFactory; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $config; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Cache directives indicating if a response can not be cached. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $noCacheFlags = ['no-cache', 'private', 'no-store']; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param CacheItemPoolInterface $pool |
||
| 49 | * @param StreamFactory $streamFactory |
||
| 50 | * @param array $config { |
||
| 51 | * |
||
| 52 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
| 53 | * @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this |
||
| 54 | * value |
||
| 55 | * @var string $hash_algo The hashing algorithm to use when generating cache keys |
||
| 56 | * @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304 |
||
| 57 | * we have to store the cache for a longer time than the server originally says it is valid for. |
||
| 58 | * We store a cache item for $cache_lifetime + max age of the response. |
||
| 59 | * @var array $methods list of request methods which can be cached |
||
| 60 | * @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses |
||
| 61 | * @var CacheKeyGenerator $cache_key_generator a class to generate the cache key. Defaults to SimpleGenerator |
||
| 62 | * } |
||
| 63 | */ |
||
| 64 | 14 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will |
||
| 87 | * cache responses with `private` cache directive. |
||
| 88 | * |
||
| 89 | * @param CacheItemPoolInterface $pool |
||
| 90 | * @param StreamFactory $streamFactory |
||
| 91 | * @param array $config For all possible config options see the constructor docs |
||
| 92 | * |
||
| 93 | * @return CachePlugin |
||
| 94 | */ |
||
| 95 | 2 | public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * This method will setup the cachePlugin in server cache mode. This is the default caching behavior it refuses to |
||
| 111 | * cache responses with the `private`or `no-cache` directives. |
||
| 112 | * |
||
| 113 | * @param CacheItemPoolInterface $pool |
||
| 114 | * @param StreamFactory $streamFactory |
||
| 115 | * @param array $config For all possible config options see the constructor docs |
||
| 116 | * |
||
| 117 | * @return CachePlugin |
||
| 118 | */ |
||
| 119 | public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | 11 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be |
||
| 205 | * returned is $maxAge. |
||
| 206 | * |
||
| 207 | * @param int|null $maxAge |
||
| 208 | * |
||
| 209 | * @return int|null Unix system time passed to the PSR-6 cache |
||
| 210 | */ |
||
| 211 | 6 | private function calculateCacheItemExpiresAfter($maxAge) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Calculate the timestamp when a response expires. After that timestamp, we need to send a |
||
| 222 | * If-Modified-Since / If-None-Match request to validate the response. |
||
| 223 | * |
||
| 224 | * @param int|null $maxAge |
||
| 225 | * |
||
| 226 | * @return int|null Unix system time. A null value means that the response expires when the cache item expires |
||
| 227 | */ |
||
| 228 | 6 | private function calculateResponseExpiresAt($maxAge) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Verify that we can cache this response. |
||
| 239 | * |
||
| 240 | * @param ResponseInterface $response |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | 6 | protected function isCacheable(ResponseInterface $response) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Get the value of a parameter in the cache control header. |
||
| 262 | * |
||
| 263 | * @param ResponseInterface $response |
||
| 264 | * @param string $name The field of Cache-Control to fetch |
||
| 265 | * |
||
| 266 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
| 267 | */ |
||
| 268 | 6 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param RequestInterface $request |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | 10 | private function createCacheKey(RequestInterface $request) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
| 299 | * |
||
| 300 | * @param ResponseInterface $response |
||
| 301 | * |
||
| 302 | * @return int|null |
||
| 303 | */ |
||
| 304 | 6 | private function getMaxAge(ResponseInterface $response) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Configure an options resolver. |
||
| 332 | * |
||
| 333 | * @param OptionsResolver $resolver |
||
| 334 | */ |
||
| 335 | 14 | private function configureOptions(OptionsResolver $resolver) |
|
| 336 | { |
||
| 337 | 14 | $resolver->setDefaults([ |
|
| 338 | 14 | 'cache_lifetime' => 86400 * 30, // 30 days |
|
| 339 | 14 | 'default_ttl' => 0, |
|
| 340 | //Deprecated as of v1.3, to be removed in v2.0. Use respect_response_cache_directives instead |
||
| 341 | 14 | 'respect_cache_headers' => null, |
|
| 342 | 14 | 'hash_algo' => 'sha1', |
|
| 343 | 14 | 'methods' => ['GET', 'HEAD'], |
|
| 344 | 14 | 'respect_response_cache_directives' => ['no-cache', 'private', 'max-age', 'no-store'], |
|
| 345 | 14 | 'cache_key_generator' => null, |
|
| 346 | 14 | ]); |
|
| 347 | |||
| 348 | 14 | $resolver->setAllowedTypes('cache_lifetime', ['int', 'null']); |
|
| 349 | 14 | $resolver->setAllowedTypes('default_ttl', ['int', 'null']); |
|
| 350 | 14 | $resolver->setAllowedTypes('respect_cache_headers', ['bool', 'null']); |
|
| 351 | 14 | $resolver->setAllowedTypes('methods', 'array'); |
|
| 352 | 14 | $resolver->setAllowedTypes('cache_key_generator', ['null', 'Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator']); |
|
| 353 | 14 | $resolver->setAllowedValues('hash_algo', hash_algos()); |
|
| 354 | $resolver->setAllowedValues('methods', function ($value) { |
||
| 355 | /* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */ |
||
| 356 | 14 | $matches = preg_grep('/[^A-Z0-9!#$%&\'*+\-.^_`|~]/', $value); |
|
| 357 | |||
| 358 | 14 | return empty($matches); |
|
| 359 | 14 | }); |
|
| 360 | |||
| 361 | $resolver->setNormalizer('respect_cache_headers', function (Options $options, $value) { |
||
| 362 | 14 | if (null !== $value) { |
|
| 363 | @trigger_error('The option "respect_cache_headers" is deprecated since version 1.3 and will be removed in 2.0. Use "respect_response_cache_directives" instead.', E_USER_DEPRECATED); |
||
|
|
|||
| 364 | } |
||
| 365 | |||
| 366 | 14 | return null === $value ? true : $value; |
|
| 367 | 14 | }); |
|
| 368 | |||
| 369 | 14 | $resolver->setNormalizer('respect_response_cache_directives', function (Options $options, $value) { |
|
| 370 | 13 | if (false === $options['respect_cache_headers']) { |
|
| 371 | return []; |
||
| 372 | } |
||
| 373 | |||
| 374 | 13 | return $value; |
|
| 375 | 14 | }); |
|
| 376 | 14 | } |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param CacheItemInterface $cacheItem |
||
| 380 | * |
||
| 381 | * @return ResponseInterface |
||
| 382 | */ |
||
| 383 | 3 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Get the value of the "If-Modified-Since" header. |
||
| 404 | * |
||
| 405 | * @param CacheItemInterface $cacheItem |
||
| 406 | * |
||
| 407 | * @return string|null |
||
| 408 | */ |
||
| 409 | 2 | private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Get the ETag from the cached response. |
||
| 425 | * |
||
| 426 | * @param CacheItemInterface $cacheItem |
||
| 427 | * |
||
| 428 | * @return string|null |
||
| 429 | */ |
||
| 430 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
| 448 | } |
||
| 449 |
If you suppress an error, we recommend checking for the error condition explicitly: