Passed
Pull Request — main (#22)
by Andrey
26:45 queued 11:50
created

Directory::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers\Filesystem;
4
5
use DirectoryIterator;
6
use Helldar\Support\Exceptions\DirectoryNotFoundException;
7
8
final class Directory
9
{
10
    public function all(string $path): DirectoryIterator
11
    {
12
        if ($this->doesntExist($path)) {
13
            throw new DirectoryNotFoundException($path);
14
        }
15
16
        return new DirectoryIterator($path);
17
    }
18
19
    public function names(string $path): array
20
    {
21
        $items = [];
22
23
        foreach ($this->all($path) as $directory) {
24
            if ($directory->isDir() && ! $directory->isDot()) {
25
                $items[] = $directory->getFilename();
26
            }
27
        }
28
29
        sort($items);
30
31
        return array_values($items);
32
    }
33
34
    public function make(string $path, int $mode = 755): bool
35
    {
36
        if ($this->doesntExist($path)) {
37
            return mkdir($path, $mode, true);
38
        }
39
40
        return true;
41
    }
42
43
    public function exists(string $path): bool
44
    {
45
        return file_exists($path) && is_dir($path);
46
    }
47
48
    public function doesntExist(string $path): bool
49
    {
50
        return ! $this->exists($path);
51
    }
52
}
53