| Total Complexity | 47 |
| Total Lines | 251 |
| Duplicated Lines | 0 % |
| Changes | 16 | ||
| 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 |
||
| 11 | class FilesystemPublisher extends Publisher |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $destFolder = 'cache'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $fileExtension = 'html'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var bool |
||
| 25 | */ |
||
| 26 | private static $use_gzip_compression = true; |
||
|
|
|||
| 27 | |||
| 28 | /** |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | public function getDestPath() |
||
| 32 | { |
||
| 33 | $base = defined('PUBLIC_PATH') ? PUBLIC_PATH : BASE_PATH; |
||
| 34 | return $base . DIRECTORY_SEPARATOR . $this->getDestFolder(); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function setDestFolder($destFolder) |
||
| 38 | { |
||
| 39 | $this->destFolder = $destFolder; |
||
| 40 | return $this; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function getDestFolder() |
||
| 44 | { |
||
| 45 | return $this->destFolder; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function setFileExtension($fileExtension) |
||
| 49 | { |
||
| 50 | $fileExtension = strtolower($fileExtension); |
||
| 51 | if (! in_array($fileExtension, ['html', 'php'], true)) { |
||
| 52 | throw new \InvalidArgumentException( |
||
| 53 | sprintf( |
||
| 54 | 'Bad file extension "%s" passed to %s::%s', |
||
| 55 | $fileExtension, |
||
| 56 | static::class, |
||
| 57 | __FUNCTION__ |
||
| 58 | ) |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | $this->fileExtension = $fileExtension; |
||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getFileExtension() |
||
| 68 | } |
||
| 69 | |||
| 70 | public function purgeURL(string $url): array |
||
| 88 | ]; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function purgeAll(): bool |
||
| 92 | { |
||
| 93 | Filesystem::removeFolder($this->getDestPath()); |
||
| 94 | |||
| 95 | return file_exists($this->getDestPath()) ? false : true; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $url |
||
| 100 | * @return array A result array |
||
| 101 | */ |
||
| 102 | public function publishURL(string $url, ?bool $forcePublish = false): array |
||
| 103 | { |
||
| 104 | if (!$url) { |
||
| 105 | user_error('Bad url:' . var_export($url, true), E_USER_WARNING); |
||
| 106 | return []; |
||
| 107 | } |
||
| 108 | $success = false; |
||
| 109 | $response = $this->generatePageResponse($url); |
||
| 110 | $statusCode = $response->getStatusCode(); |
||
| 111 | $doPublish = ($forcePublish && $this->getFileExtension() === 'php') || $statusCode < 400; |
||
| 112 | |||
| 113 | if ($statusCode < 300) { |
||
| 114 | // publish success response |
||
| 115 | $success = $this->publishPage($response, $url); |
||
| 116 | } elseif ($statusCode < 400) { |
||
| 117 | // publish redirect response |
||
| 118 | $success = $this->publishRedirect($response, $url); |
||
| 119 | } elseif ($doPublish) { |
||
| 120 | // only publish error pages if we are able to send status codes via PHP |
||
| 121 | $success = $this->publishPage($response, $url); |
||
| 122 | } |
||
| 123 | return [ |
||
| 124 | 'published' => $doPublish, |
||
| 125 | 'success' => $success, |
||
| 126 | 'responsecode' => $statusCode, |
||
| 127 | 'url' => $url, |
||
| 128 | ]; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function getPublishedURLs(?string $dir = null, array &$result = []): array |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param HTTPResponse $response |
||
| 158 | * @param string $url |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | protected function publishRedirect($response, $url) |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param HTTPResponse $response |
||
| 177 | * @param string $url |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | protected function publishPage($response, $url) |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * returns true on access and false on failure |
||
| 199 | * @param string $content |
||
| 200 | * @param string $filePath |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | protected function saveToPath($content, $filePath): bool |
||
| 204 | { |
||
| 205 | if (empty($content)) { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | // Write to a temporary file first |
||
| 210 | $temporaryPath = tempnam(TEMP_PATH, 'filesystempublisher_'); |
||
| 211 | if (file_put_contents($temporaryPath, $content) === false) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | |||
| 215 | // Move the temporary file to the desired location (prevents unlocked files from being read during write) |
||
| 216 | $publishPath = $this->getDestPath() . DIRECTORY_SEPARATOR . $filePath; |
||
| 217 | Filesystem::makeFolder(dirname($publishPath)); |
||
| 218 | $successWithPublish = rename($temporaryPath, $publishPath); |
||
| 219 | if ($successWithPublish) { |
||
| 220 | if (FilesystemPublisher::config()->get('use_gzip_compression')) { |
||
| 221 | $publishPath = $this->compressFile($publishPath); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | return file_exists($publishPath); |
||
| 226 | } |
||
| 227 | |||
| 228 | protected function compressFile(string $publishPath): string |
||
| 229 | { |
||
| 230 | //we keep the html file for now ... so use second parameter to achieve this. |
||
| 231 | $publishPathGZipped = $publishPath . '.gz'; |
||
| 232 | exec('rm ' . $publishPathGZipped . ' -rf'); |
||
| 233 | exec('gzip ' . $publishPath); |
||
| 234 | exec('chmod 0777 ' . $publishPathGZipped); |
||
| 235 | |||
| 236 | return $publishPathGZipped; |
||
| 237 | } |
||
| 238 | |||
| 239 | protected function deleteFromPath($filePath) |
||
| 252 | } |
||
| 253 | |||
| 254 | protected function URLtoPath(string $url): string |
||
| 255 | { |
||
| 256 | return URLtoPath($url, BASE_URL, FilesystemPublisher::config()->get('domain_based_caching')); |
||
| 257 | } |
||
| 258 | |||
| 259 | protected function pathToURL(string $path): string |
||
| 262 | } |
||
| 263 | } |
||
| 264 |