| Total Complexity | 47 | 
| Total Lines | 261 | 
| Duplicated Lines | 0 % | 
| Changes | 17 | ||
| 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, string $url) : bool  | 
            ||
| 173 | }  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * @param HTTPResponse $response  | 
            ||
| 177 | * @param string $url  | 
            ||
| 178 | * @return bool  | 
            ||
| 179 | */  | 
            ||
| 180 | protected function publishPage(string $response, string $url) : bool  | 
            ||
| 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(string $content, string $filePath): bool  | 
            ||
| 226 | }  | 
            ||
| 227 | |||
| 228 | protected function compressFile(string $publishPath): string  | 
            ||
| 229 |     { | 
            ||
| 230 | // read original  | 
            ||
| 231 |         $data = implode('', file($publishPath)); | 
            ||
| 232 | // encode it with highest level  | 
            ||
| 233 | $gzdata = gzencode($data, 9);  | 
            ||
| 234 | // new file  | 
            ||
| 235 | $publishPathGZipped = $publishPath . '.gz';  | 
            ||
| 236 | // remove  | 
            ||
| 237 | unlink($publishPathGZipped);  | 
            ||
| 238 | // open a new one  | 
            ||
| 239 | $fp = fopen($publishPathGZipped, 'w');  | 
            ||
| 240 | // write it  | 
            ||
| 241 | fwrite($fp, $gzdata);  | 
            ||
| 242 | // close it  | 
            ||
| 243 | fclose($fp);  | 
            ||
| 244 | @chmod($file, 0664);  | 
            ||
| 245 | |||
| 246 | return $publishPathGZipped;  | 
            ||
| 247 | }  | 
            ||
| 248 | |||
| 249 | protected function deleteFromPath(string $filePath) : bool  | 
            ||
| 262 | }  | 
            ||
| 263 | |||
| 264 | protected function URLtoPath(string $url): string  | 
            ||
| 265 |     { | 
            ||
| 266 |         return URLtoPath($url, BASE_URL, FilesystemPublisher::config()->get('domain_based_caching')); | 
            ||
| 267 | }  | 
            ||
| 268 | |||
| 269 | protected function pathToURL(string $path): string  | 
            ||
| 272 | }  | 
            ||
| 273 | }  | 
            ||
| 274 |