Passed
Push — master ( fe1bfe...d29151 )
by Giuliano
02:24
created

Folder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Maestriam\FileSystem\Entities;
4
5
use Maestriam\FileSystem\Foundation\Drive\PathSanitizer;
6
use Maestriam\FileSystem\Foundation\FileSearch;
7
use Maestriam\FileSystem\Foundation\FolderReader;
8
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)
66
    {
67
        $this->path = $path;
68
        return $this;
69
    }
70
}