Total Complexity | 49 |
Total Lines | 276 |
Duplicated Lines | 0 % |
Changes | 20 | ||
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() |
||
75 | { |
||
76 | return $this->fileExtension; |
||
77 | } |
||
78 | |||
79 | public function purgeURL(string $url): array |
||
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(): bool |
||
101 | { |
||
102 | Filesystem::removeFolder($this->getDestPath()); |
||
103 | |||
104 | return file_exists($this->getDestPath()) ? false : true; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $url |
||
109 | * @return array A result array |
||
110 | */ |
||
111 | public function publishURL(string $url, ?bool $forcePublish = false): array |
||
112 | { |
||
113 | if (! $url) { |
||
114 | user_error('Bad url:' . var_export($url, true), E_USER_WARNING); |
||
115 | return []; |
||
116 | } |
||
117 | $success = false; |
||
118 | $response = $this->generatePageResponse($url); |
||
119 | $statusCode = $response->getStatusCode(); |
||
120 | $doPublish = ($forcePublish && $this->getFileExtension() === 'php') || $statusCode < 400; |
||
121 | |||
122 | if ($statusCode < 300) { |
||
123 | // publish success response |
||
124 | $success = $this->publishPage($response, $url); |
||
125 | } elseif ($statusCode < 400) { |
||
126 | // publish redirect response |
||
127 | $success = $this->publishRedirect($response, $url); |
||
128 | } elseif ($doPublish) { |
||
129 | // only publish error pages if we are able to send status codes via PHP |
||
130 | $success = $this->publishPage($response, $url); |
||
131 | } |
||
132 | return [ |
||
133 | 'published' => $doPublish, |
||
134 | 'success' => $success, |
||
135 | 'responsecode' => $statusCode, |
||
136 | 'url' => $url, |
||
137 | ]; |
||
138 | } |
||
139 | |||
140 | public function getPublishedURLs(?string $dir = null, array &$result = []): array |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param HTTPResponse $response |
||
167 | * @param string $url |
||
168 | * @return bool |
||
169 | */ |
||
170 | protected function publishRedirect($response, string $url) : bool |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param HTTPResponse $response |
||
186 | * @param string $url |
||
187 | * @return bool |
||
188 | */ |
||
189 | protected function publishPage(string $response, string $url) : bool |
||
190 | { |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * returns true on access and false on failure |
||
216 | * @param string $content |
||
217 | * @param string $filePath |
||
218 | * @return bool |
||
219 | */ |
||
220 | protected function saveToPath(string $content, string $filePath): bool |
||
221 | { |
||
222 | if (empty($content)) { |
||
223 | return false; |
||
224 | } |
||
225 | |||
226 | // Write to a temporary file first |
||
227 | $temporaryPath = tempnam(TEMP_PATH, 'filesystempublisher_'); |
||
228 | if (file_put_contents($temporaryPath, $content) === false) { |
||
229 | return false; |
||
230 | } |
||
231 | |||
232 | // Move the temporary file to the desired location (prevents unlocked files from being read during write) |
||
233 | $publishPath = $this->getDestPath() . DIRECTORY_SEPARATOR . $filePath; |
||
234 | Filesystem::makeFolder(dirname($publishPath)); |
||
235 | $successWithPublish = rename($temporaryPath, $publishPath); |
||
236 | if ($successWithPublish) { |
||
237 | if (FilesystemPublisher::config()->get('use_gzip_compression')) { |
||
238 | $publishPath = $this->compressFile($publishPath); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | return file_exists($publishPath); |
||
243 | } |
||
244 | |||
245 | protected function compressFile(string $publishPath): string |
||
246 | { |
||
247 | // read original |
||
248 | $data = implode('', file($publishPath)); |
||
249 | // encode it with highest level |
||
250 | $gzdata = gzencode($data, 9); |
||
251 | // new file |
||
252 | $publishPathGZipped = $publishPath . '.gz'; |
||
253 | // remove |
||
254 | unlink($publishPathGZipped); |
||
255 | // open a new one |
||
256 | $fp = fopen($publishPathGZipped, 'w'); |
||
257 | // write it |
||
258 | fwrite($fp, $gzdata); |
||
259 | // close it |
||
260 | fclose($fp); |
||
261 | @chmod($file, 0664); |
||
262 | |||
263 | return $publishPathGZipped; |
||
264 | } |
||
265 | |||
266 | protected function deleteFromPath(string $filePath) : bool |
||
279 | } |
||
280 | |||
281 | protected function URLtoPath(string $url): string |
||
282 | { |
||
283 | return URLtoPath($url, BASE_URL, FilesystemPublisher::config()->get('domain_based_caching')); |
||
284 | } |
||
285 | |||
286 | protected function pathToURL(string $path): string |
||
289 | } |
||
290 | } |
||
291 |