| Conditions | 11 |
| Paths | 288 |
| Total Lines | 55 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 100 | public function applyToResponse($originator, HTTPRequest $request, HTTPResponse $response) |
||
| 101 | { |
||
| 102 | $cacheAge = $this->getCacheAge(); |
||
| 103 | $vary = $this->getVary(); |
||
| 104 | |||
| 105 | // Allow overriding max-age from the object hooked up to the policed controller. |
||
| 106 | if ($originator->hasMethod('getCacheAge')) { |
||
| 107 | $extendedCacheAge = $originator->getCacheAge($cacheAge); |
||
| 108 | if ($extendedCacheAge !== null) { |
||
| 109 | $cacheAge = $extendedCacheAge; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | // Development sites have frequently changing templates; this can get stuffed up by the code |
||
| 114 | // below. |
||
| 115 | if (Director::isDev() && $this->config()->get('disable_cache_age_in_dev')) { |
||
| 116 | $cacheAge = 0; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Same for vary, but probably less useful. |
||
| 120 | if ($originator->hasMethod('getVary')) { |
||
| 121 | $extendedVary = $originator->getVary($vary); |
||
| 122 | if ($extendedVary !== null) { |
||
| 123 | $vary = $extendedVary; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // Enable caching via core APIs |
||
| 128 | HTTPCacheControlMiddleware::singleton()->enableCache(); |
||
| 129 | |||
| 130 | if ($cacheAge > 0) { |
||
| 131 | HTTPCacheControlMiddleware::singleton()->setMaxAge($cacheAge); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Merge vary into response |
||
| 135 | if ($vary) { |
||
| 136 | HTTPCacheControlMiddleware::singleton()->addVary($vary); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Find out when the URI was last modified. Allows customisation, but fall back HTTP timestamp collector. |
||
| 140 | if ($originator->hasMethod('getModificationTimestamp')) { |
||
| 141 | $timestamp = $originator->getModificationTimestamp(); |
||
| 142 | } else { |
||
| 143 | $timestamp = HTTP::$modification_date; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($timestamp) { |
||
| 147 | $response->addHeader("Last-Modified", self::gmt_date($timestamp)); |
||
| 148 | } |
||
| 149 | |||
| 150 | $expires = time() + HTTPCacheControlMiddleware::singleton()->getDirective('max-age'); |
||
| 151 | $response->addHeader("Expires", self::gmt_date($expires)); |
||
| 152 | |||
| 153 | // Now that we've generated them, either output them or attach them to the SS_HTTPResponse as appropriate |
||
| 154 | HTTPCacheControlMiddleware::singleton()->applyToResponse($response); |
||
| 155 | } |
||
| 157 |