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