Complex classes like CacheUtil 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 CacheUtil, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class CacheUtil |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Method to add a Cache-Control header to a PSR-7 response, which should enable caching. |
||
| 28 | * Set the `private` parameter to false to enable shared caches to cache the response (for |
||
| 29 | * example when the response is usable by everyone and does not contain individual information). |
||
| 30 | * With the `$maxAge` parameter you can specify how many seconds a response should be cached. |
||
| 31 | * |
||
| 32 | * By default this method specifies a `private` cache, which caches for 10 minutes. For more |
||
| 33 | * options use the `withCacheControl` method. |
||
| 34 | * |
||
| 35 | * @see withCacheControl |
||
| 36 | * |
||
| 37 | * @param ResponseInterface $response PSR-7 response to add the header to |
||
| 38 | * @param bool $public True for public, false for private |
||
| 39 | * @param int $maxAge How many seconds the response should be cached. Default: 600 (10 min) |
||
| 40 | * @return ResponseInterface |
||
| 41 | * @throws InvalidArgumentException If the type is invalid |
||
| 42 | */ |
||
| 43 | 2 | public function withCache(ResponseInterface $response, $public = false, $maxAge = 600) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Method to add a Cache-Control header to a PSR-7 response, which should prevent caching. |
||
| 54 | * Adds `no-cache, no-store, must-revalidate`. Use the `withCacheControl` method for more |
||
| 55 | * options. |
||
| 56 | * |
||
| 57 | * @see withCacheControl |
||
| 58 | * |
||
| 59 | * @param ResponseInterface $response PSR-7 response to prevent caching |
||
| 60 | * @return ResponseInterface |
||
| 61 | */ |
||
| 62 | 1 | public function withCachePrevention(ResponseInterface $response) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Method to add an Expires header to a PSR-7 response. Use this header if you have a specific |
||
| 72 | * time when the representation will expire, otherwise use the more fine-tuned `Cache-Control` |
||
| 73 | * header with the `max-age` directive. If you need to support the old HTTP/1.0 protocol and |
||
| 74 | * want to set a relative expiration, use the `withRelativeExpires` method. |
||
| 75 | * |
||
| 76 | * @see withCache |
||
| 77 | * @see withCacheControl |
||
| 78 | * @see withRelativeExpires |
||
| 79 | * @link https://tools.ietf.org/html/rfc7234#section-5.3 |
||
| 80 | * |
||
| 81 | * @param ResponseInterface $response PSR-7 response to add the header to |
||
| 82 | * @param int|string|DateTime $time UNIX timestamp, date string or DateTime object |
||
| 83 | * @return ResponseInterface |
||
| 84 | * @throws InvalidArgumentException If the time could not be parsed |
||
| 85 | */ |
||
| 86 | 3 | public function withExpires(ResponseInterface $response, $time) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Method to add a relative `Expires` header to a PSR-7 response. Use this header if want to |
||
| 93 | * support the old HTTP/1.0 protocol and have a relative expiration time. Otherwise use the |
||
| 94 | * `Cache-Control` header with the `max-age` directive. |
||
| 95 | * |
||
| 96 | * @see withCache |
||
| 97 | * @see withCacheControl |
||
| 98 | * @see withExpires |
||
| 99 | * @link https://tools.ietf.org/html/rfc7234#section-5.3 |
||
| 100 | * |
||
| 101 | * @param ResponseInterface $response PSR-7 response to add the header to |
||
| 102 | * @param int $seconds Number of seconds the response should be cached |
||
| 103 | * @return ResponseInterface |
||
| 104 | * @throws InvalidArgumentException If the seconds parameter is not an integer |
||
| 105 | */ |
||
| 106 | 2 | public function withRelativeExpires(ResponseInterface $response, $seconds) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Method to add an ETag header to a PSR-7 response. If possible, always add an ETag to a |
||
| 119 | * response you want to cache. If the last modified time is easily accessible, use both the |
||
| 120 | * ETag and Last-Modified header. Use a weak ETag if you want to compare if a resource is only |
||
| 121 | * semantically the same. |
||
| 122 | * |
||
| 123 | * @see withLastModified |
||
| 124 | * @link https://tools.ietf.org/html/rfc7232#section-2.3 |
||
| 125 | * |
||
| 126 | * @param ResponseInterface $response PSR-7 response to add the header to |
||
| 127 | * @param string $eTag ETag to add |
||
| 128 | * @param bool $weak If the provided ETag is weak |
||
| 129 | * @return ResponseInterface |
||
| 130 | * @throws InvalidArgumentException if the ETag value is not valid |
||
| 131 | */ |
||
| 132 | 3 | public function withETag(ResponseInterface $response, $eTag, $weak = false) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Method to add a Last-Modified header to a PSR-7 response. Add a Last-Modified header if you |
||
| 144 | * have an easy access to the last modified time, otherwise use only an ETag. |
||
| 145 | * |
||
| 146 | * The provided time can be an UNIX timestamp, a parseable string or a DateTime instance. |
||
| 147 | * |
||
| 148 | * @see withETag |
||
| 149 | * @see getTimeFromValue |
||
| 150 | * @link https://tools.ietf.org/html/rfc7232#section-2.2 |
||
| 151 | * |
||
| 152 | * @param ResponseInterface $response PSR-7 response to add the header to |
||
| 153 | * @param int|string|DateTime $time UNIX timestamp, date string or DateTime object |
||
| 154 | * @return ResponseInterface |
||
| 155 | * @throws InvalidArgumentException If the time could not be parsed |
||
| 156 | */ |
||
| 157 | 1 | public function withLastModified(ResponseInterface $response, $time) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Method to add a Cache-Control header to the provided PSR-7 message. |
||
| 164 | * |
||
| 165 | * @link https://tools.ietf.org/html/rfc7234#section-5.2 |
||
| 166 | * |
||
| 167 | * @param MessageInterface $message PSR-7 message to add the Cache-Control header to |
||
| 168 | * @param CacheControl $cacheControl Cache-Control object to add to the message |
||
| 169 | * @return MessageInterface The PSR-7 message with the added Cache-Control header |
||
| 170 | * @throws InvalidArgumentException If the Cache-Control header is invalid |
||
| 171 | */ |
||
| 172 | 1 | public function withCacheControl(MessageInterface $message, CacheControl $cacheControl) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Checks if a request has included validators (`ETag` and/or `Last-Modified` Date) which allow |
||
| 179 | * to determine if the client has the current resource state. The method will check if the |
||
| 180 | * `If-Match` and/or `If-Unmodified-Since` header is present. |
||
| 181 | * |
||
| 182 | * This method can be used for unsafe conditional requests (neither GET nor HEAD). If the |
||
| 183 | * request did not include a state validator (method returns `false`), abort the execution and |
||
| 184 | * return a `428` http status code (or `403` if you only want to use the original status |
||
| 185 | * codes). If the requests includes state validators (method returns `true`), you can continue |
||
| 186 | * and check if the client has the current state with the `hasCurrentState` method. |
||
| 187 | * |
||
| 188 | * @see hasCurrentState |
||
| 189 | * @link https://tools.ietf.org/html/rfc7232#section-6 |
||
| 190 | * |
||
| 191 | * @param RequestInterface $request PSR-7 request to check |
||
| 192 | * @return bool True if the request includes state validators, false if it has no validators |
||
| 193 | */ |
||
| 194 | 4 | public function hasStateValidator(RequestInterface $request) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Checks if the provided PSR-7 request has the current resource state. The method will check |
||
| 201 | * the `If-Match` and `If-Modified-Since` headers with the current ETag (and/or the Last-Modified |
||
| 202 | * date if provided). In addition, for a request which is not GET or HEAD, the method will check |
||
| 203 | * the `If-None-Match` header. |
||
| 204 | * |
||
| 205 | * Use this method to check conditional unsafe requests and to prevent lost updates. If the |
||
| 206 | * request does not have the current resource state (method returns `false`), abort and return |
||
| 207 | * status code `412`. In contrast, if the client has the current version of the resource (method |
||
| 208 | * returns `true`) you can safely continue the execution and update/delete the resource. |
||
| 209 | * |
||
| 210 | * @link https://tools.ietf.org/html/rfc7232#section-6 |
||
| 211 | * |
||
| 212 | * @param RequestInterface $request PSR-7 request to check |
||
| 213 | * @param string $eTag Current ETag of the resource |
||
| 214 | * @param null|int|string|DateTime $lastModified Current Last-Modified date (optional) |
||
| 215 | * @return bool True if the request has the current resource state, false if the state is outdated |
||
| 216 | * @throws InvalidArgumentException If the Last-Modified date could not be parsed |
||
| 217 | */ |
||
| 218 | 20 | public function hasCurrentState(RequestInterface $request, $eTag, $lastModified = null) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Method to check if the response is not modified and the request still has a valid cache, by |
||
| 246 | * comparing the `ETag` headers. If no `ETag` is available and the method is GET or HEAD, the |
||
| 247 | * `Last-Modified` header is used for comparison. |
||
| 248 | * |
||
| 249 | * Returns `true` if the request is not modified and the cache is still valid. In this case the |
||
| 250 | * application should return an empty response with the status code `304`. If the returned |
||
| 251 | * value is `false`, the client either has no cached representation or has an outdated cache. |
||
| 252 | * |
||
| 253 | * @link https://tools.ietf.org/html/rfc7232#section-6 |
||
| 254 | * |
||
| 255 | * @param RequestInterface $request Request to check against |
||
| 256 | * @param ResponseInterface $response Response with ETag and/or Last-Modified header |
||
| 257 | * @return bool True if not modified, false if invalid cache |
||
| 258 | * @throws InvalidArgumentException If the current Last-Modified date could not be parsed |
||
| 259 | */ |
||
| 260 | 15 | public function isNotModified(RequestInterface $request, ResponseInterface $response) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Method to check if a response can be cached by a shared cache. The method will check if the |
||
| 279 | * response status is cacheable and if the Cache-Control header explicitly disallows caching. |
||
| 280 | * The method does NOT check if the response is fresh. If you want to store the response on the |
||
| 281 | * application side, check both `isCacheable` and `isFresh`. |
||
| 282 | * |
||
| 283 | * @see isFresh |
||
| 284 | * @link https://tools.ietf.org/html/rfc7234#section-3 |
||
| 285 | * |
||
| 286 | * @param ResponseInterface $response Response to check if it is cacheable |
||
| 287 | * @return bool True if the response is cacheable by a shared cache and false otherwise |
||
| 288 | */ |
||
| 289 | 4 | public function isCacheable(ResponseInterface $response) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Method to check if a response is still fresh. This is useful if you want to know if can still |
||
| 305 | * serve a saved response or if you have to create a new response. The method returns true if the |
||
| 306 | * lifetime of the response is available and is greater than the age of the response. If the |
||
| 307 | * method returns false, the response is outdated and should be renewed. In case no lifetime |
||
| 308 | * information is available (no Cache-Control and Expires header), the method returns `null`. |
||
| 309 | * |
||
| 310 | * @see getLifetime |
||
| 311 | * @see getAge |
||
| 312 | * @link https://tools.ietf.org/html/rfc7234#section-4.2 |
||
| 313 | * |
||
| 314 | * @param ResponseInterface $response Response to check |
||
| 315 | * @return bool|null True if the response is still fresh and false otherwise. Null if unknown |
||
| 316 | */ |
||
| 317 | 4 | public function isFresh(ResponseInterface $response) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Returns the lifetime of the provided response (how long the response should be cached once it |
||
| 329 | * is created). The method will lookup the `s-maxage` Cache-Control directive first and fallback |
||
| 330 | * to the `max-age` directive. If both Cache-Control directives are not available, the `Expires` |
||
| 331 | * header is used to compute the lifetime. Returns the lifetime in seconds if available and |
||
| 332 | * `null` if the lifetime cannot be calculated. |
||
| 333 | * |
||
| 334 | * @param ResponseInterface $response Response to get the lifetime from |
||
| 335 | * @return int|null Lifetime in seconds or null if not available |
||
| 336 | */ |
||
| 337 | 7 | public function getLifetime(ResponseInterface $response) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Returns the age of the response (how long the response is already cached). Uses the `Age` |
||
| 359 | * header to determine the age. If the header is not available, the `Date` header is used |
||
| 360 | * instead. The method will return the age in seconds or `null` if the age cannot be calculated. |
||
| 361 | * |
||
| 362 | * @param ResponseInterface $response Response to get the age from |
||
| 363 | * @return int|null Age in seconds or null if not available |
||
| 364 | */ |
||
| 365 | 3 | public function getAge(ResponseInterface $response) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Returns a formatted timestamp of the time parameter, to use in the HTTP headers. The time |
||
| 382 | * parameter can be an UNIX timestamp, a parseable string or a DateTime object. |
||
| 383 | * |
||
| 384 | * @link https://secure.php.net/manual/en/datetime.formats.php |
||
| 385 | * |
||
| 386 | * @param int|string|DateTime $time Timestamp, date string or DateTime object |
||
| 387 | * @return string Formatted timestamp |
||
| 388 | * @throws InvalidArgumentException If the time could not be parsed |
||
| 389 | */ |
||
| 390 | 6 | protected function getTimeFromValue($time) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Returns the Unix timestamp of the time parameter. The parameter can be an Unix timestamp, |
||
| 417 | * string or a DateTime object. |
||
| 418 | * |
||
| 419 | * @param int|string|DateTime $time |
||
| 420 | * @return int Unix timestamp |
||
| 421 | * @throws InvalidArgumentException If the time could not be parsed |
||
| 422 | */ |
||
| 423 | 4 | protected function getTimestampFromValue($time) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Parses the Cache-Control header of a response and returns the Cache-Control object. |
||
| 442 | * |
||
| 443 | * @param ResponseInterface $response |
||
| 444 | * @return ResponseCacheControl |
||
| 445 | */ |
||
| 446 | 1 | protected function getCacheControl(ResponseInterface $response) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Method to check if the current ETag matches the ETag of the request. |
||
| 453 | * |
||
| 454 | * @link https://tools.ietf.org/html/rfc7232#section-2.3.2 |
||
| 455 | * |
||
| 456 | * @param string $currentETag The current ETag |
||
| 457 | * @param string $requestETags The ETags from the request |
||
| 458 | * @param bool $weak Whether to do a weak comparison (default: strong) |
||
| 459 | * @return bool True if the current ETag matches the ETags of the request, false otherwise |
||
| 460 | */ |
||
| 461 | 25 | private function matchesETag($currentETag, $requestETags, $weak = false) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Method to check if the current Last-Modified date matches the date of the request. |
||
| 487 | * |
||
| 488 | * @param int|string|DateTime $currentModified Current Last-Modified date |
||
| 489 | * @param string $requestModified Last-Modified date of the request |
||
| 490 | * @return bool True if the current date matches the date of the request, false otherwise |
||
| 491 | * @throws InvalidArgumentException If the current date could not be parsed |
||
| 492 | */ |
||
| 493 | 8 | private function matchesModified($currentModified, $requestModified) |
|
| 501 | } |
||
| 502 |