Total Complexity | 11 |
Total Lines | 60 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | class Folder |
||
10 | { |
||
11 | private string $path; |
||
12 | |||
13 | public function __construct(string $path) |
||
14 | { |
||
15 | $this->setPath($path); |
||
16 | } |
||
17 | |||
18 | public function sanitize() |
||
19 | { |
||
20 | return PathSanitizer::sanitize($this->path); |
||
21 | } |
||
22 | |||
23 | public function read(int $level) : array |
||
24 | { |
||
25 | return FolderReader::read($this->path, $level); |
||
26 | } |
||
27 | |||
28 | public function files(string $pattern = null) : array |
||
29 | { |
||
30 | $search = new FileSearch($this->path); |
||
31 | |||
32 | return $search->files($pattern); |
||
33 | } |
||
34 | |||
35 | public function create() |
||
36 | { |
||
37 | if ($this->exists()) { |
||
38 | return true; |
||
39 | } |
||
40 | |||
41 | mkdir($this->path, 0755, true); |
||
42 | } |
||
43 | |||
44 | public function delete(string $path = null) |
||
45 | { |
||
46 | $folder = $path ?? $this->path; |
||
47 | |||
48 | $files = array_diff(scandir($folder), array('.', '..')); |
||
49 | |||
50 | foreach ($files as $file) { |
||
51 | |||
52 | $item = "$folder/$file"; |
||
53 | |||
54 | is_dir($item) ? $this->delete($item) : unlink($item); |
||
55 | } |
||
56 | |||
57 | return rmdir($folder); |
||
58 | } |
||
59 | |||
60 | public function exists() : bool |
||
61 | { |
||
62 | return (is_dir($this->path)); |
||
63 | } |
||
64 | |||
65 | private function setPath(string $path) |
||
69 | } |
||
70 | } |