| Total Complexity | 49 |
| Total Lines | 269 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| Bugs | 0 | Features | 1 |
Complex classes like FilesystemPublisher 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.
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 FilesystemPublisher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class FilesystemPublisher extends Publisher |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $destFolder = 'cache'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $fileExtension = 'php'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private static $use_gzip_compression = false; |
||
|
|
|||
| 29 | |||
| 30 | /** |
||
| 31 | * avoid caching any pages with name"SecurityID" - an indication that a |
||
| 32 | * form my be present that requires a fresh SecurityID |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | private static $lazy_form_recognition = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | public function getDestPath() |
||
| 41 | { |
||
| 42 | $base = defined('PUBLIC_PATH') ? PUBLIC_PATH : BASE_PATH; |
||
| 43 | return $base . DIRECTORY_SEPARATOR . $this->getDestFolder(); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function setDestFolder($destFolder) |
||
| 47 | { |
||
| 48 | $this->destFolder = $destFolder; |
||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function getDestFolder() |
||
| 53 | { |
||
| 54 | return $this->destFolder; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function setFileExtension($fileExtension) |
||
| 58 | { |
||
| 59 | $fileExtension = strtolower($fileExtension); |
||
| 60 | if (!in_array($fileExtension, ['html', 'php'], true)) { |
||
| 61 | throw new \InvalidArgumentException( |
||
| 62 | sprintf( |
||
| 63 | 'Bad file extension "%s" passed to %s::%s', |
||
| 64 | $fileExtension, |
||
| 65 | static::class, |
||
| 66 | __FUNCTION__ |
||
| 67 | ) |
||
| 68 | ); |
||
| 69 | } |
||
| 70 | $this->fileExtension = $fileExtension; |
||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getFileExtension() |
||
| 77 | } |
||
| 78 | |||
| 79 | public function purgeURL($url) |
||
| 80 | { |
||
| 81 | if (!$url) { |
||
| 82 | user_error('Bad url:' . var_export($url, true), E_USER_WARNING); |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | if ($path = $this->URLtoPath($url)) { |
||
| 86 | $success = $this->deleteFromPath($path . '.html') && $this->deleteFromPath($path . '.php'); |
||
| 87 | return [ |
||
| 88 | 'success' => $success, |
||
| 89 | 'url' => $url, |
||
| 90 | 'path' => $this->getDestPath() . DIRECTORY_SEPARATOR . $path, |
||
| 91 | ]; |
||
| 92 | } |
||
| 93 | return [ |
||
| 94 | 'success' => false, |
||
| 95 | 'url' => $url, |
||
| 96 | 'path' => false, |
||
| 97 | ]; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function purgeAll() |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $url |
||
| 109 | * @param bool $forcePublish (optional) |
||
| 110 | * @return array A result array |
||
| 111 | */ |
||
| 112 | public function publishURL($url, $forcePublish = false) |
||
| 138 | ]; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param HTTPResponse $response |
||
| 143 | * @param string $url |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | protected function publishRedirect($response, $url) |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param HTTPResponse $response |
||
| 162 | * @param string $url |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | protected function publishPage($response, $url) |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * returns true on access and false on failure |
||
| 188 | * @param string $content |
||
| 189 | * @param string $filePath |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | protected function saveToPath($content, $filePath) |
||
| 193 | { |
||
| 194 | if (empty($content)) { |
||
| 195 | return false; |
||
| 196 | } |
||
| 197 | |||
| 198 | // Write to a temporary file first |
||
| 199 | $temporaryPath = tempnam(TEMP_PATH, 'filesystempublisher_'); |
||
| 200 | if (file_put_contents($temporaryPath, $content) === false) { |
||
| 201 | return false; |
||
| 202 | } |
||
| 203 | |||
| 204 | // Move the temporary file to the desired location (prevents unlocked files from being read during write) |
||
| 205 | $publishPath = $this->getDestPath() . DIRECTORY_SEPARATOR . $filePath; |
||
| 206 | Filesystem::makeFolder(dirname($publishPath)); |
||
| 207 | |||
| 208 | $successWithPublish = rename($temporaryPath, $publishPath); |
||
| 209 | if ($successWithPublish) { |
||
| 210 | if (FilesystemPublisher::config()->get('use_gzip_compression')) { |
||
| 211 | $publishPath = $this->compressFile($publishPath); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | return file_exists($publishPath); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Compress the html file and store it gzipped |
||
| 220 | * |
||
| 221 | * @param string $publishPath |
||
| 222 | * @return string The path of the compressed file |
||
| 223 | */ |
||
| 224 | protected function compressFile($publishPath) |
||
| 225 | { |
||
| 226 | $data = file_get_contents($publishPath); |
||
| 227 | $gzData = gzencode($data, 9); |
||
| 228 | $gzPublishPath = $publishPath . '.gz'; |
||
| 229 | unlink($gzPublishPath); |
||
| 230 | file_put_contents($gzPublishPath, $gzData); |
||
| 231 | return $gzPublishPath; |
||
| 232 | } |
||
| 233 | |||
| 234 | protected function deleteFromPath($filePath) |
||
| 235 | { |
||
| 236 | $deletePath = $this->getDestPath() . DIRECTORY_SEPARATOR . $filePath; |
||
| 237 | if (file_exists($deletePath)) { |
||
| 238 | $success = unlink($deletePath); |
||
| 239 | } else { |
||
| 240 | $success = true; |
||
| 241 | } |
||
| 242 | if (file_exists($deletePath . '.gz')) { |
||
| 243 | $success = unlink($deletePath . '.gz') && $success; |
||
| 244 | } |
||
| 245 | |||
| 246 | return $success; |
||
| 247 | } |
||
| 248 | |||
| 249 | protected function URLtoPath($url) |
||
| 250 | { |
||
| 251 | return URLtoPath($url, BASE_URL, FilesystemPublisher::config()->get('domain_based_caching')); |
||
| 252 | } |
||
| 253 | |||
| 254 | protected function pathToURL($path) |
||
| 255 | { |
||
| 256 | return PathToURL($path, $this->getDestPath(), FilesystemPublisher::config()->get('domain_based_caching')); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function getPublishedURLs($dir = null, &$result = []) |
||
| 282 | } |
||
| 283 | } |
||
| 284 |