Total Complexity | 70 |
Total Lines | 609 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Files 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 Files, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Files |
||
20 | { |
||
21 | /** |
||
22 | * Mine Types of files. |
||
23 | * |
||
24 | * @since 1.0.0 |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $mineTypes = []; |
||
29 | |||
30 | /** |
||
31 | * Types. |
||
32 | * |
||
33 | * @since 1.0.0 |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $types = []; |
||
38 | |||
39 | /** |
||
40 | * The default value for recursive create dirs. |
||
41 | * |
||
42 | * @since 1.0.0 |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $recursiveDirectories = true; |
||
47 | |||
48 | /** |
||
49 | * Default chmod. |
||
50 | * |
||
51 | * @since 1.0.0 |
||
52 | * |
||
53 | * @var int |
||
54 | */ |
||
55 | private $defCHMOD = 0755; |
||
56 | |||
57 | /** |
||
58 | * __construct. |
||
59 | * |
||
60 | * @param array $mines |
||
61 | * @param array $extensions |
||
62 | * |
||
63 | * @since 1.0.0 |
||
64 | */ |
||
65 | public function __construct($mines, $extensions) |
||
66 | { |
||
67 | $this->mineTypes = $mines; |
||
68 | $this->types = $extensions; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Create alphaz system dir. |
||
73 | * |
||
74 | * @since 1.0.0 |
||
75 | * @deprecated 3.0.0 |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function systemDirs() |
||
80 | { |
||
81 | $this->mkDir('../Storage'); |
||
82 | $this->mkDir('../Storage/Data'); |
||
83 | $this->mkDir('../Storage/Logs'); |
||
84 | $this->mkDir('../Storage/Session'); |
||
85 | $this->mkDir('../Storage/Backup'); |
||
86 | $this->mkDIr('../Storage/Cache'); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Change the default chmod. |
||
91 | * |
||
92 | * @param mixed $chomd Valid chmod |
||
93 | * |
||
94 | * @since 1.0.0 |
||
95 | * |
||
96 | * @return int |
||
97 | */ |
||
98 | public function changeDefaultChmod($chmod) |
||
99 | { |
||
100 | return ($chmod === null) ? $this->defCHMOD : $this->defCHMOD = $chmod; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Create recurisve dir. |
||
105 | * |
||
106 | * @param mixed $value recursive status true|false. |
||
107 | * |
||
108 | * @since 1.0.0 |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function recursiveCreateDir($value = null) |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Add the mine type. |
||
119 | * |
||
120 | * @param (string) $type Correct mine type. |
||
121 | * |
||
122 | * @since 1.0.0 |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | public function addMineTypes($type) |
||
127 | { |
||
128 | array_push($this->mineTypes, $type); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Add the extemsio. |
||
133 | * |
||
134 | * @param (string) $type Correct type. |
||
135 | * @param (string) $sub Extensions/ |
||
136 | * |
||
137 | * @since 1.0.0 |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function addExt($type, $ext) |
||
142 | { |
||
143 | array_push($this->types[$type], $ext); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Make the dir. |
||
148 | * |
||
149 | * @param (string) $name Name of dir with path. |
||
150 | * @param (string) $recursive Recursive mode create: null|true|false. |
||
151 | * @param (string) $chmod Directory permission on create: default 0755. |
||
152 | * |
||
153 | * @since 1.0.0 |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function mkDir($name, $recursive = null, $chmod = null) |
||
158 | { |
||
159 | // test the recursive mode with default value |
||
160 | $recursive = ($recursive === null) ? $this->recursiveDirectories : $recursive; |
||
|
|||
161 | // test the chmod with default value |
||
162 | $chmod = ($chmod === null) ? $this->defCHMOD : $chmod; |
||
163 | if (!is_dir($name)) { |
||
164 | return (mkdir($name)) ? true : false; |
||
165 | } |
||
166 | |||
167 | return false; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Change the permission. |
||
172 | * |
||
173 | * @param (string) $source Name of file or directory with path. |
||
174 | * @param (mixed) $pre Valid permission. |
||
175 | * |
||
176 | * @since 1.0.0 |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function permission($source, $pre) |
||
181 | { |
||
182 | if (!is_dir($source)) { |
||
183 | return (file_exists($source)) ? chmod($source, $pre) : false; |
||
184 | } |
||
185 | |||
186 | return false; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Change the owner of an array of files. |
||
191 | * |
||
192 | * @param (string) $source Name of file or directory with path. |
||
193 | * @param (array) $files Files to be copy. |
||
194 | * @param (mixed) $user The new owner user name. |
||
195 | * |
||
196 | * @since 1.0.0 |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function chown($source, $files, $user) |
||
201 | { |
||
202 | foreach ($files as $file => $value) { |
||
203 | if (file_exists($source.$value)) { |
||
204 | if (@chown($file, $user) === false) { |
||
205 | throw new \RuntimeException("The ownership of {$file} can not change", 500); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Sets access and modification time of file. |
||
213 | * |
||
214 | * @param (string) $source Name of file or directory with path. |
||
215 | * @param (array) $files Files to be copy. |
||
216 | * @param (int) $time The touch time as a Unix timestamp. |
||
217 | * @param (int) $atime The access time as a Unix timestamp. |
||
218 | * |
||
219 | * @since 1.0.0 |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function touch($source, $files, $time = null, $atime = null) |
||
224 | { |
||
225 | foreach ($files as $file => $value) { |
||
226 | if (file_exists($source.$value)) { |
||
227 | $touch = $time ? @touch($file, $time, $atime) : @touch($file); |
||
228 | if ($touch !== true) { |
||
229 | return false; |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Copy file. |
||
237 | * |
||
238 | * @param (string) $source Name of file or directory with path. |
||
239 | * @param (string) $target Target directory. |
||
240 | * @param (string) $files Files to be copy. |
||
241 | * |
||
242 | * @since 1.0.0 |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | public function copyFile($source, $target, $file) |
||
247 | { |
||
248 | $this->mkDir($target); |
||
249 | if (file_exists($source.$file)) { |
||
250 | copy($source.$file, $target.$file); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Copy files. |
||
256 | * |
||
257 | * @param (string) $source Name of file or directory with path. |
||
258 | * @param (string) $target Target directory. |
||
259 | * @param (array) $files Files to be copy. |
||
260 | * |
||
261 | * @since 1.0.0 |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | public function copyFiles($source, $target, $files) |
||
266 | { |
||
267 | foreach ($files as $file) { |
||
268 | $this->copyFile($source, $target, $file); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Move file. |
||
274 | * |
||
275 | * @param (string) $source Name of file or directory with path. |
||
276 | * @param (string) $target Target directory. |
||
277 | * @param (string) $file Files to be moved. |
||
278 | * |
||
279 | * @since 1.0.0 |
||
280 | * |
||
281 | * @return void |
||
282 | */ |
||
283 | public function moveFile($source, $target, $file) |
||
284 | { |
||
285 | if (file_exists($source.$file)) { |
||
286 | rename($source.$file, $target.$file); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Move files. |
||
292 | * |
||
293 | * @param (string) $source Name of file or directory with path. |
||
294 | * @param (string) $target Target directory. |
||
295 | * @param (array|string) $files Files to be moved. |
||
296 | * |
||
297 | * @since 1.0.0 |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | public function moveFiles($source, $target, $files) |
||
302 | { |
||
303 | foreach ($files as $file) { |
||
304 | $this->moveFile($source, $target, $files); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Delete file. |
||
310 | * |
||
311 | * @param (string) $file Name of file with path. |
||
312 | * |
||
313 | * @since 1.0.0 |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | public function deleteFile($file) |
||
318 | { |
||
319 | if (file_exists($file)) { |
||
320 | unlink($file); |
||
321 | } |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Delete files. |
||
326 | * |
||
327 | * @param (array) $file Name of file with path. |
||
328 | * |
||
329 | * @since 1.0.0 |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | public function deleteFiles($files) |
||
334 | { |
||
335 | foreach ($files as $file) { |
||
336 | $this->deleteFile($file); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Copy dir. |
||
342 | * |
||
343 | * @param (string) $source Directory with path. |
||
344 | * @param (string) $target Target directory. |
||
345 | * @param (string) $dir Directory to be copied. |
||
346 | * |
||
347 | * @since 1.0.0 |
||
348 | * |
||
349 | * @return void |
||
350 | */ |
||
351 | public function copyDir($source, $target, $dir) |
||
352 | { |
||
353 | $this->mkDir($target); |
||
354 | $serverOs = (new \alphaz\Common\OperatingSystem())->get(); |
||
355 | $command = ($serverOs === 'Windows') ? 'xcopy ' : 'cp -r '; |
||
356 | if (is_dir($source.$dir)) { |
||
357 | shell_exec($command.$source.$dir.' '.$target); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Copy dirs. |
||
363 | * |
||
364 | * @param (string) $source Directory with path. |
||
365 | * @param (string) $target Target directory. |
||
366 | * @param (array) $dirs Directories to be copied. |
||
367 | * |
||
368 | * @since 1.0.0 |
||
369 | * |
||
370 | * @return void |
||
371 | */ |
||
372 | public function copyDirs($source, $target, $dirs) |
||
373 | { |
||
374 | foreach ($dirs as $dir) { |
||
375 | $this->copyDir($source, $target, $dir); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Move dir. |
||
381 | * |
||
382 | * @param (string) $source Directory with path. |
||
383 | * @param (string) $target Target directory. |
||
384 | * @param (string) $dir Directory to be copied. |
||
385 | * |
||
386 | * @since 1.0.0 |
||
387 | * |
||
388 | * @return void |
||
389 | */ |
||
390 | public function moveDir($source, $target, $dir) |
||
397 | } |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Move dirs. |
||
402 | * |
||
403 | * @param (string) $source Directory with path. |
||
404 | * @param (string) $target Target directory. |
||
405 | * @param (array) $dirs Dirs to be copy. |
||
406 | * |
||
407 | * @since 1.0.0 |
||
408 | * |
||
409 | * @return void |
||
410 | */ |
||
411 | public function moveDirs($source, $target, $dirs) |
||
415 | } |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Delete dir. |
||
420 | * |
||
421 | * @param (string) $dir Directory with path. |
||
422 | * |
||
423 | * @since 1.0.0 |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function deleteDir($dir) |
||
428 | { |
||
429 | if (!file_exists($dir)) { |
||
430 | return true; |
||
431 | } |
||
432 | |||
433 | if (!is_dir($dir)) { |
||
434 | return unlink($dir); |
||
435 | } |
||
436 | foreach (scandir($dir) as $item) { |
||
437 | if ($item == '.' || $item == '..') { |
||
438 | continue; |
||
439 | } |
||
440 | $this->deleteDir($dir.DIRECTORY_SEPARATOR.$item); |
||
441 | |||
442 | return true; |
||
443 | } |
||
444 | |||
445 | return rmdir($dir); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Delete dirs. |
||
450 | * |
||
451 | * @param (array) $dirs Directory with path. |
||
452 | * |
||
453 | * @since 1.0.0 |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | public function deleteDirs($dirs) |
||
458 | { |
||
459 | foreach ($dirs as $dir) { |
||
460 | $this->deleteDir($dir); |
||
461 | } |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Upload file. |
||
466 | * |
||
467 | * @param (string) $file File to be uploaded. |
||
468 | * @param (string) $target Target where file should be uploaded. |
||
469 | * @param (string) $fileType Supported => image,media,docs,zip. |
||
470 | * @param (int) $maxSize File size to be allowed. |
||
471 | * |
||
472 | * @since 1.0.0 |
||
473 | * |
||
474 | * @return mixed |
||
475 | */ |
||
476 | public function fileUpload($file, $target, $fileType, $maxSize = 7992000000) |
||
540 | ]; |
||
541 | } |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Upload files. |
||
546 | * |
||
547 | * @param (array) $files Files to be uploaded. |
||
548 | * @param (string) $target Target where file should be upload. |
||
549 | * @param (string) $fileType Supported => image,media,docs,zip. |
||
550 | * @param (int) $count Number of file count. |
||
551 | * @param (int) $maxSize File size to be allowed. |
||
552 | * |
||
553 | * @since 1.0.0 |
||
554 | * |
||
555 | * @return void |
||
556 | */ |
||
557 | public function filesUpload($files, $target, $fileType, $count, $maxSize = 7992000000) |
||
630 |