Passed
Push — main ( ecd686...4ce043 )
by Andrey
01:26
created

Directory::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers\Filesystem;
4
5
use DirectoryIterator;
6
use FilesystemIterator;
7
use Helldar\Support\Exceptions\DirectoryNotFoundException;
8
use Helldar\Support\Facades\Helpers\Filesystem\File as FileHelper;
9
use Helldar\Support\Facades\Helpers\Instance;
10
use SplFileInfo;
11
12
final class Directory
13
{
14
    /**
15
     * Get a list of files and folders in a directory.
16
     *
17
     * @param  string  $path
18
     *
19
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
20
     *
21
     * @return DirectoryIterator
22
     */
23 8
    public function all(string $path): DirectoryIterator
24
    {
25 8
        if ($this->doesntExist($path)) {
26 4
            throw new DirectoryNotFoundException($path);
27
        }
28
29 4
        return new DirectoryIterator($path);
30
    }
31
32
    /**
33
     * Get a list of directory names along a path.
34
     *
35
     * @param  string  $path
36
     *
37
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
38
     *
39
     * @return array
40
     */
41 2
    public function names(string $path): array
42
    {
43 2
        $items = [];
44
45 2
        foreach ($this->all($path) as $directory) {
46 2
            if ($directory->isDir() && ! $directory->isDot()) {
47 2
                $items[] = $directory->getFilename();
48
            }
49
        }
50
51 2
        sort($items);
52
53 2
        return array_values($items);
54
    }
55
56
    /**
57
     * Create a directory at the specified path.
58
     *
59
     * @param  string  $path
60
     * @param  int  $mode
61
     *
62
     * @return bool
63
     */
64 28
    public function make(string $path, int $mode = 755): bool
65
    {
66 28
        return $this->doesntExist($path) ? mkdir($path, $mode, true) : true;
67
    }
68
69
    /**
70
     * Delete the directory with all contents in the specified path.
71
     *
72
     * @param  string  $path
73
     *
74
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
75
     *
76
     * @return bool
77
     */
78 30
    public function delete(string $path): bool
79
    {
80 30
        if (! $this->isDirectory($path)) {
81 4
            throw new DirectoryNotFoundException($path);
82
        }
83
84 28
        $items = new FilesystemIterator($path);
85
86 28
        $success = true;
87
88 28
        foreach ($items as $item) {
89 26
            $item->isDir() && ! $item->isLink()
90 26
                ? $this->delete($item->getPathname())
91 20
                : FileHelper::delete($item->getPathname());
92
        }
93
94 28
        @rmdir($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

94
        /** @scrutinizer ignore-unhandled */ @rmdir($path);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
96 28
        return $success;
97
    }
98
99
    /**
100
     * Check if the directory exists.
101
     *
102
     * @param  string  $path
103
     *
104
     * @return bool
105
     */
106 294
    public function exists(string $path): bool
107
    {
108 294
        return file_exists($path) && is_dir($path);
109
    }
110
111
    /**
112
     * Check if the directory doesn't exists.
113
     *
114
     * @param  string  $path
115
     *
116
     * @return bool
117
     */
118 38
    public function doesntExist(string $path): bool
119
    {
120 38
        return ! $this->exists($path);
121
    }
122
123
    /**
124
     * Check if object or path is a directory.
125
     *
126
     * @param  DirectoryIterator|\SplFileInfo|string  $value
127
     *
128
     * @return bool
129
     */
130 38
    public function isDirectory($value): bool
131
    {
132 38
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
133 2
            return $value->isDir();
134
        }
135
136 36
        return is_dir($value);
137
    }
138
139
    /**
140
     * Checks the existence of a directory.
141
     *
142
     * @param  DirectoryIterator|\SplFileInfo|string  $path
143
     *
144
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
145
     */
146 4
    public function validate($path): void
147
    {
148 4
        if (! $this->isDirectory($path)) {
149 2
            throw new DirectoryNotFoundException($path);
150
        }
151 2
    }
152
}
153