Folder   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 60
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setPath() 0 4 1
A exists() 0 3 1
A sanitize() 0 3 1
A read() 0 3 1
A create() 0 7 2
A delete() 0 14 3
A files() 0 5 1
A __construct() 0 3 1
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
}