Complex classes like HTTP_ConditionalGet 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 HTTP_ConditionalGet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 63 | class HTTP_ConditionalGet { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Does the client have a valid copy of the requested resource? |
||
| 67 | * |
||
| 68 | * You'll want to check this after instantiating the object. If true, do |
||
| 69 | * not send content, just call sendHeaders() if you haven't already. |
||
| 70 | * |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | public $cacheIsValid = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param array $spec options |
||
| 77 | * |
||
| 78 | * 'isPublic': (bool) if false, the Cache-Control header will contain |
||
| 79 | * "private", allowing only browser caching. (default false) |
||
| 80 | * |
||
| 81 | * 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers |
||
| 82 | * will be sent with content. This is recommended. |
||
| 83 | * |
||
| 84 | * 'encoding': (string) if set, the header "Vary: Accept-Encoding" will |
||
| 85 | * always be sent and a truncated version of the encoding will be appended |
||
| 86 | * to the ETag. E.g. "pub123456;gz". This will also trigger a more lenient |
||
| 87 | * checking of the client's If-None-Match header, as the encoding portion of |
||
| 88 | * the ETag will be stripped before comparison. |
||
| 89 | * |
||
| 90 | * 'contentHash': (string) if given, only the ETag header can be sent with |
||
| 91 | * content (only HTTP1.1 clients can conditionally GET). The given string |
||
| 92 | * should be short with no quote characters and always change when the |
||
| 93 | * resource changes (recommend md5()). This is not needed/used if |
||
| 94 | * lastModifiedTime is given. |
||
| 95 | * |
||
| 96 | * 'eTag': (string) if given, this will be used as the ETag header rather |
||
| 97 | * than values based on lastModifiedTime or contentHash. Also the encoding |
||
| 98 | * string will not be appended to the given value as described above. |
||
| 99 | * |
||
| 100 | * 'invalidate': (bool) if true, the client cache will be considered invalid |
||
| 101 | * without testing. Effectively this disables conditional GET. |
||
| 102 | * (default false) |
||
| 103 | * |
||
| 104 | * 'maxAge': (int) if given, this will set the Cache-Control max-age in |
||
| 105 | * seconds, and also set the Expires header to the equivalent GMT date. |
||
| 106 | * After the max-age period has passed, the browser will again send a |
||
| 107 | * conditional GET to revalidate its cache. |
||
| 108 | */ |
||
| 109 | public function __construct($spec) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get array of output headers to be sent |
||
| 162 | * |
||
| 163 | * In the case of 304 responses, this array will only contain the response |
||
| 164 | * code header: array('_responseCode' => 'HTTP/1.0 304 Not Modified') |
||
| 165 | * |
||
| 166 | * Otherwise something like: |
||
| 167 | * <code> |
||
| 168 | * array( |
||
| 169 | * 'Cache-Control' => 'max-age=0, public' |
||
| 170 | * ,'ETag' => '"foobar"' |
||
| 171 | * ) |
||
| 172 | * </code> |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function getHeaders() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Set the Content-Length header in bytes |
||
| 183 | * |
||
| 184 | * With most PHP configs, as long as you don't flush() output, this method |
||
| 185 | * is not needed and PHP will buffer all output and set Content-Length for |
||
| 186 | * you. Otherwise you'll want to call this to let the client know up front. |
||
| 187 | * |
||
| 188 | * @param int $bytes |
||
| 189 | * |
||
| 190 | * @return int copy of input $bytes |
||
| 191 | */ |
||
| 192 | public function setContentLength($bytes) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Send headers |
||
| 199 | * |
||
| 200 | * @see getHeaders() |
||
| 201 | * |
||
| 202 | * Note this doesn't "clear" the headers. Calling sendHeaders() will |
||
| 203 | * call header() again (but probably have not effect) and getHeaders() will |
||
| 204 | * still return the headers. |
||
| 205 | * |
||
| 206 | * @return null |
||
| 207 | */ |
||
| 208 | public function sendHeaders() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Exit if the client's cache is valid for this resource |
||
| 224 | * |
||
| 225 | * This is a convenience method for common use of the class |
||
| 226 | * |
||
| 227 | * @param int $lastModifiedTime if given, both ETag AND Last-Modified headers |
||
| 228 | * will be sent with content. This is recommended. |
||
| 229 | * |
||
| 230 | * @param bool $isPublic (default false) if true, the Cache-Control header |
||
| 231 | * will contain "public", allowing proxies to cache the content. Otherwise |
||
| 232 | * "private" will be sent, allowing only browser caching. |
||
| 233 | * |
||
| 234 | * @param array $options (default empty) additional options for constructor |
||
| 235 | */ |
||
| 236 | public static function check($lastModifiedTime = null, $isPublic = false, $options = array()) |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Get a GMT formatted date for use in HTTP headers |
||
| 252 | * |
||
| 253 | * <code> |
||
| 254 | * header('Expires: ' . HTTP_ConditionalGet::gmtdate($time)); |
||
| 255 | * </code> |
||
| 256 | * |
||
| 257 | * @param int $time unix timestamp |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public static function gmtDate($time) |
||
| 265 | |||
| 266 | protected $_headers = array(); |
||
| 267 | protected $_lmTime = null; |
||
| 268 | protected $_etag = null; |
||
| 269 | protected $_stripEtag = false; |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $hash |
||
| 273 | * |
||
| 274 | * @param string $scope |
||
| 275 | */ |
||
| 276 | protected function _setEtag($hash, $scope) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param int $time |
||
| 284 | */ |
||
| 285 | protected function _setLastModified($time) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Determine validity of client cache and queue 304 header if valid |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | protected function _isCacheValid() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | protected function resourceMatchedEtag() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $etag |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | protected function normalizeEtag($etag) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | protected function resourceNotModified() |
||
| 366 | } |
||
| 367 |