| Total Complexity | 45 |
| Total Lines | 238 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
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 = 'php'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @return string |
||
| 25 | */ |
||
| 26 | public function getDestPath() |
||
| 27 | { |
||
| 28 | $base = defined('PUBLIC_PATH') ? PUBLIC_PATH : BASE_PATH; |
||
| 29 | return $base . DIRECTORY_SEPARATOR . $this->getDestFolder(); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function setDestFolder($destFolder) |
||
| 33 | { |
||
| 34 | $this->destFolder = $destFolder; |
||
| 35 | return $this; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function getDestFolder() |
||
| 39 | { |
||
| 40 | return $this->destFolder; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function setFileExtension($fileExtension) |
||
| 44 | { |
||
| 45 | $fileExtension = strtolower($fileExtension); |
||
| 46 | if (!in_array($fileExtension, ['html', 'php'], true)) { |
||
| 47 | throw new \InvalidArgumentException( |
||
| 48 | sprintf( |
||
| 49 | 'Bad file extension "%s" passed to %s::%s', |
||
| 50 | $fileExtension, |
||
| 51 | static::class, |
||
| 52 | __FUNCTION__ |
||
| 53 | ) |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | $this->fileExtension = $fileExtension; |
||
| 57 | return $this; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getFileExtension() |
||
| 63 | } |
||
| 64 | |||
| 65 | public function purgeURL($url) |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $url |
||
| 88 | * @param bool $forcePublish |
||
| 89 | * @return array A result array |
||
| 90 | */ |
||
| 91 | public function publishURL($url, $forcePublish = false) |
||
| 92 | { |
||
| 93 | if (!$url) { |
||
| 94 | user_error('Bad url:' . var_export($url, true), E_USER_WARNING); |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | $success = false; |
||
| 98 | $response = $this->generatePageResponse($url); |
||
| 99 | $statusCode = $response->getStatusCode(); |
||
| 100 | $doPublish = ($forcePublish && $this->getFileExtension() === 'php') || $statusCode < 400; |
||
|
|
|||
| 101 | |||
| 102 | if ($statusCode >= 300 && $statusCode < 400) { |
||
| 103 | // publish redirect response |
||
| 104 | $success = $this->publishRedirect($response, $url); |
||
| 105 | } elseif ($doPublish) { |
||
| 106 | $success = $this->publishPage($response, $url); |
||
| 107 | } |
||
| 108 | return [ |
||
| 109 | 'published' => $doPublish, |
||
| 110 | 'success' => $success, |
||
| 111 | 'responsecode' => $statusCode, |
||
| 112 | 'url' => $url, |
||
| 113 | ]; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param HTTPResponse $response |
||
| 118 | * @param string $url |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | protected function publishRedirect($response, $url) |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param HTTPResponse $response |
||
| 137 | * @param string $url |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | protected function publishPage($response, $url) |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * returns true on success and false on failure |
||
| 155 | * |
||
| 156 | * @param string $content |
||
| 157 | * @param string $filePath |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | protected function saveToPath($content, $filePath) |
||
| 161 | { |
||
| 162 | if (empty($content)) { |
||
| 163 | return false; |
||
| 164 | } |
||
| 165 | |||
| 166 | // Write to a temporary file first |
||
| 167 | $temporaryPath = tempnam(TEMP_PATH, 'filesystempublisher_'); |
||
| 168 | if (file_put_contents($temporaryPath, $content) === false) { |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | |||
| 172 | // Move the temporary file to the desired location (prevents unlocked files from being read during write) |
||
| 173 | $publishPath = $this->getDestPath() . DIRECTORY_SEPARATOR . $filePath; |
||
| 174 | Filesystem::makeFolder(dirname($publishPath)); |
||
| 175 | |||
| 176 | $successWithPublish = rename($temporaryPath, $publishPath); |
||
| 177 | if ($successWithPublish) { |
||
| 178 | if (FilesystemPublisher::config()->get('use_gzip_compression')) { |
||
| 179 | $publishPath = $this->compressFile($publishPath); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | return file_exists($publishPath); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Compress the html file and store it gzipped |
||
| 187 | * |
||
| 188 | * @param string $publishPath |
||
| 189 | * @return string The path of the compressed file |
||
| 190 | */ |
||
| 191 | protected function compressFile($publishPath) |
||
| 192 | { |
||
| 193 | $data = file_get_contents($publishPath); |
||
| 194 | $gzData = gzencode($data, 9); |
||
| 195 | $gzPublishPath = $publishPath . '.gz'; |
||
| 196 | unlink($gzPublishPath); |
||
| 197 | file_put_contents($gzPublishPath, $gzData); |
||
| 198 | return $gzPublishPath; |
||
| 199 | } |
||
| 200 | |||
| 201 | protected function deleteFromPath($filePath) |
||
| 202 | { |
||
| 203 | $deletePath = $this->getDestPath() . DIRECTORY_SEPARATOR . $filePath; |
||
| 204 | if (file_exists($deletePath)) { |
||
| 205 | $success = unlink($deletePath); |
||
| 206 | } else { |
||
| 207 | $success = true; |
||
| 208 | } |
||
| 209 | if (file_exists($deletePath . '.gz')) { |
||
| 210 | $success = unlink($deletePath . '.gz') && $success; |
||
| 211 | } |
||
| 212 | |||
| 213 | return $success; |
||
| 214 | } |
||
| 215 | |||
| 216 | protected function URLtoPath($url) |
||
| 217 | { |
||
| 218 | return URLtoPath($url, BASE_URL, FilesystemPublisher::config()->get('domain_based_caching')); |
||
| 219 | } |
||
| 220 | |||
| 221 | protected function pathToURL($path) |
||
| 222 | { |
||
| 223 | return PathToURL($path, $this->getDestPath(), FilesystemPublisher::config()->get('domain_based_caching')); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function getPublishedURLs($dir = null, &$result = []) |
||
| 249 | } |
||
| 250 | } |
||
| 251 |