| Total Complexity | 45 |
| Total Lines | 244 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 10 | class FilesystemPublisher extends Publisher |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var boolean |
||
| 15 | */ |
||
| 16 | protected $addTimestamp = false; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $destFolder = 'cache'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $fileExtension = 'php'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @return boolean |
||
| 30 | */ |
||
| 31 | public function getAddTimestamp() |
||
| 34 | } |
||
| 35 | |||
| 36 | public function setAddTimestamp($state) |
||
| 37 | { |
||
| 38 | $this->addTimestamp = $state; |
||
| 39 | return $this; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public function getDestPath() |
||
| 46 | { |
||
| 47 | $base = defined('PUBLIC_PATH') ? PUBLIC_PATH : BASE_PATH; |
||
| 48 | return $base . DIRECTORY_SEPARATOR . $this->getDestFolder(); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function setDestFolder($destFolder) |
||
| 52 | { |
||
| 53 | $this->destFolder = $destFolder; |
||
| 54 | return $this; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function getDestFolder() |
||
| 60 | } |
||
| 61 | |||
| 62 | public function setFileExtension($fileExtension) |
||
| 63 | { |
||
| 64 | $fileExtension = strtolower($fileExtension); |
||
| 65 | if (!in_array($fileExtension, ['html', 'php'])) { |
||
| 66 | throw new \InvalidArgumentException( |
||
| 67 | sprintf( |
||
| 68 | 'Bad file extension "%s" passed to %s::%s', |
||
| 69 | $fileExtension, |
||
| 70 | static::class, |
||
| 71 | __FUNCTION__ |
||
| 72 | ) |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | $this->fileExtension = $fileExtension; |
||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function getFileExtension() |
||
| 80 | { |
||
| 81 | return $this->fileExtension; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function purgeURL($url) |
||
| 85 | { |
||
| 86 | if (!$url) { |
||
| 87 | user_error("Bad url:" . var_export($url, true), E_USER_WARNING); |
||
| 88 | return; |
||
| 89 | } |
||
| 90 | if ($path = $this->URLtoPath($url)) { |
||
| 91 | $success = $this->deleteFromPath($path . '.html') && $this->deleteFromPath($path . '.php'); |
||
| 92 | return [ |
||
| 93 | 'success' => $success, |
||
| 94 | 'url' => $url, |
||
| 95 | 'path' => $this->getDestPath() . DIRECTORY_SEPARATOR . $path, |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | return [ |
||
| 99 | 'success' => false, |
||
| 100 | 'url' => $url, |
||
| 101 | 'path' => false, |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $url |
||
| 107 | * @return array A result array |
||
| 108 | */ |
||
| 109 | public function publishURL($url, $forcePublish = false) |
||
| 110 | { |
||
| 111 | if (!$url) { |
||
| 112 | user_error("Bad url:" . var_export($url, true), E_USER_WARNING); |
||
| 113 | return; |
||
| 114 | } |
||
| 115 | $success = false; |
||
| 116 | $response = $this->generatePageResponse($url); |
||
| 117 | $statusCode = $response->getStatusCode(); |
||
| 118 | $doPublish = ($forcePublish && $this->getFileExtension() == 'php') || $statusCode < 400; |
||
| 119 | |||
| 120 | if ($statusCode < 300) { |
||
| 121 | // publish success response |
||
| 122 | $success = $this->publishPage($response, $url); |
||
| 123 | } elseif ($statusCode < 400) { |
||
| 124 | // publish redirect response |
||
| 125 | $success = $this->publishRedirect($response, $url); |
||
| 126 | } elseif ($doPublish) { |
||
| 127 | // only publish error pages if we are able to send status codes via PHP |
||
| 128 | $success = $this->publishPage($response, $url); |
||
| 129 | } |
||
| 130 | return [ |
||
| 131 | 'published' => $doPublish, |
||
| 132 | 'success' => $success, |
||
| 133 | 'responsecode' => $statusCode, |
||
| 134 | 'url' => $url, |
||
| 135 | ]; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param HTTPResponse $response |
||
| 140 | * @param string $url |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | protected function publishRedirect($response, $url) |
||
| 144 | { |
||
| 145 | $success = true; |
||
| 146 | if ($path = $this->URLtoPath($url)) { |
||
| 147 | $location = $response->getHeader('Location'); |
||
| 148 | if ($this->getFileExtension() === 'php') { |
||
| 149 | $phpContent = $this->generatePHPCacheFile($response); |
||
| 150 | $success = $this->saveToPath($phpContent, $path . '.php'); |
||
| 151 | } |
||
| 152 | return $this->saveToPath($this->generateHTMLCacheRedirection($location), $path . '.html') && $success; |
||
| 153 | } |
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param HTTPResponse $response |
||
| 159 | * @param string $url |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | protected function publishPage($response, $url) |
||
| 163 | { |
||
| 164 | $success = true; |
||
| 165 | if ($path = $this->URLtoPath($url)) { |
||
| 166 | if ($this->getFileExtension() === 'php') { |
||
| 167 | $phpContent = $this->generatePHPCacheFile($response); |
||
| 168 | $success = $this->saveToPath($phpContent, $path . '.php'); |
||
| 169 | } |
||
| 170 | $responseBody = $response->getBody(); |
||
| 171 | if ($this->getAddTimestamp() === true) { |
||
| 172 | $responseBody = str_replace('</html>', "<!-- " . date('c') . " -->\n</html>", $responseBody); |
||
| 173 | } |
||
| 174 | |||
| 175 | return $this->saveToPath($responseBody, $path . '.html') && $success; |
||
| 176 | } |
||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $content |
||
| 182 | * @param string $filePath |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | protected function saveToPath($content, $filePath) |
||
| 186 | { |
||
| 187 | if (empty($content)) { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | $publishPath = $this->getDestPath() . DIRECTORY_SEPARATOR . $filePath; |
||
| 191 | Filesystem::makeFolder(dirname($publishPath)); |
||
| 192 | return file_put_contents($publishPath, $content) !== false; |
||
| 193 | } |
||
| 194 | |||
| 195 | protected function deleteFromPath($filePath) |
||
| 196 | { |
||
| 197 | $deletePath = $this->getDestPath() . DIRECTORY_SEPARATOR . $filePath; |
||
| 198 | if (file_exists($deletePath)) { |
||
| 199 | $success = unlink($deletePath); |
||
| 200 | } else { |
||
| 201 | $success = true; |
||
| 202 | } |
||
| 203 | |||
| 204 | return $success; |
||
| 205 | } |
||
| 206 | |||
| 207 | protected function URLtoPath($url) |
||
| 208 | { |
||
| 209 | return URLtoPath($url, BASE_URL, FilesystemPublisher::config()->get('domain_based_caching')); |
||
| 210 | } |
||
| 211 | |||
| 212 | protected function pathToURL($path) |
||
| 213 | { |
||
| 214 | if (strpos($path, $this->getDestPath()) === 0) { |
||
| 215 | //Strip off the full path of the cache dir from the front |
||
| 216 | $path = substr($path, strlen($this->getDestPath())); |
||
| 217 | } |
||
| 218 | |||
| 219 | // Strip off the file extension and leading / |
||
| 220 | $relativeURL = substr($path, 0, (strrpos($path, "."))); |
||
| 221 | $relativeURL = ltrim($relativeURL, '/'); |
||
| 222 | |||
| 223 | if (FilesystemPublisher::config()->get('domain_based_caching')) { |
||
| 224 | // factor in the domain as the top dir |
||
| 225 | return Director::protocol() . $relativeURL; |
||
| 226 | } |
||
| 227 | |||
| 228 | return $relativeURL == 'index' ? Director::absoluteBaseURL() : Director::absoluteURL($relativeURL); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function getPublishedURLs($dir = null, &$result = []) |
||
| 254 | } |
||
| 255 | } |
||
| 256 |