Passed
Push — master ( fec7a7...e6f852 )
by Giuliano
03:19
created

Folder::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Maestriam\FileSystem\Entities;
4
5
use Maestriam\FileSystem\Foundation\Drive\PathSanitizer;
6
use Maestriam\FileSystem\Foundation\FolderReader;
7
8
class Folder
9
{
10
    private string $path;
11
12
    public function __construct(string $path)
13
    {
14
        $this->setPath($path);
15
    }
16
17
    public function sanitize()
18
    {
19
        return PathSanitizer::sanitize($this->path);
20
    }
21
22
    public function read(int $level) : array
23
    {
24
        return FolderReader::read($this->path, $level);
25
    }
26
27
    public function create()
28
    {
29
        if ($this->exists()) {
30
            return true;
31
        }
32
33
        mkdir($this->path, 0755, true);
34
    }
35
36
    public function delete(string $path = null)
37
    {
38
        $folder = $path ?? $this->path;
39
40
        $files = array_diff(scandir($folder), array('.', '..'));
41
42
        foreach ($files as $file) { 
43
44
            $item = "$folder/$file";
45
46
            is_dir($item) ? $this->delete($item) : unlink($item); 
47
        }
48
49
        return rmdir($folder);
50
    }
51
52
    public function exists() : bool
53
    {
54
        return (is_dir($this->path));
55
    }
56
57
    private function setPath(string $path)
58
    {
59
        $this->path = $path;
60
        return $this;
61
    }
62
}