Passed
Pull Request — main (#126)
by Andrey
14:31
created

Directory::validated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
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 Helldar\Support\Facades\Helpers\Str;
11
use SplFileInfo;
12
13
class Directory
14
{
15
    /**
16
     * Get a list of files and folders in a directory.
17
     *
18
     * @param  string  $path
19
     *
20
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
21
     *
22
     * @return DirectoryIterator
23
     */
24 18
    public function all(string $path): DirectoryIterator
25
    {
26 18
        if ($this->doesntExist($path)) {
27 4
            throw new DirectoryNotFoundException($path);
28
        }
29
30 14
        return new DirectoryIterator($path);
31
    }
32
33
    /**
34
     * Get a list of directory names along a path.
35
     *
36
     * @param  string  $path
37
     * @param  callable|null  $callback
38
     * @param  bool  $recursive
39
     *
40
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
41
     *
42
     * @return array
43
     */
44 6
    public function names(string $path, callable $callback = null, bool $recursive = false): array
45
    {
46 6
        $items = [];
47
48
        /** @var \DirectoryIterator $directory */
49 6
        foreach ($this->all($path) as $directory) {
50 6
            if ($directory->isDir() && ! $directory->isDot()) {
51 6
                $name = $directory->getFilename();
52
53 6
                if (! is_callable($callback) || $callback($name)) {
54 6
                    $items[] = $name;
55
                }
56
            }
57
58 6
            if ($recursive && $directory->isDir() && ! $directory->isDot()) {
59 2
                $prefix = (string) Str::of($directory->getRealPath())
60 2
                    ->after(realpath($path))
61 2
                    ->trim('\\/');
62
63 2
                foreach ($this->names($directory->getRealPath(), $callback, $recursive) as $value) {
64 2
                    $items[] = $prefix . '/' . $value;
65
                }
66
            }
67
        }
68
69 6
        sort($items);
70
71 6
        return array_values($items);
72
    }
73
74
    /**
75
     * Create a directory at the specified path.
76
     *
77
     * @param  string  $path
78
     * @param  int  $mode
79
     *
80
     * @return bool
81
     */
82 34
    public function make(string $path, int $mode = 0755): bool
83
    {
84 34
        return ! $this->doesntExist($path) || mkdir($path, $mode, true);
85
    }
86
87
    /**
88
     * Delete the directory with all contents in the specified path.
89
     *
90
     * @param  string  $path
91
     *
92
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
93
     *
94
     * @return bool
95
     */
96 36
    public function delete(string $path): bool
97
    {
98 36
        if (! $this->isDirectory($path)) {
99 4
            throw new DirectoryNotFoundException($path);
100
        }
101
102 34
        $items = new FilesystemIterator($path);
103
104 34
        $success = true;
105
106 34
        foreach ($items as $item) {
107 32
            $item->isDir() && ! $item->isLink()
108 32
                ? $this->delete($item->getPathname())
109 24
                : FileHelper::delete($item->getPathname());
110
        }
111
112 34
        @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

112
        /** @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...
113
114 34
        return $success;
115
    }
116
117
    /**
118
     * Ensure the directory has been deleted.
119
     *
120
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
121
     */
122
    public function ensureDelete(string $path): bool
123
    {
124
        return $this->doesntExist($path) || $this->delete($path);
125
    }
126 2
127
    /**
128 2
     * Ensure created directory exists.
129 2
     *
130
     * @param  string  $path
131
     * @param  int  $mode
132 2
     * @param  bool  $can_delete
133 2
     *
134
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
135 2
     */
136
    public function ensureDirectory(string $path, int $mode = 0755, bool $can_delete = false): void
137
    {
138
        if ($can_delete && $this->exists($path)) {
139
            $this->delete($path);
140
        }
141
142
        if ($this->doesntExist($path)) {
143
            $this->make($path, $mode);
144 611
        }
145
    }
146 611
147
    /**
148
     * Check if the directory exists.
149
     *
150
     * @param  string  $path
151
     *
152
     * @return bool
153
     */
154
    public function exists(string $path): bool
155
    {
156 54
        return file_exists($path) && is_dir($path);
157
    }
158 54
159
    /**
160
     * Check if the directory doesn't exists.
161
     *
162
     * @param  string  $path
163
     *
164
     * @return bool
165
     */
166
    public function doesntExist(string $path): bool
167
    {
168 48
        return ! $this->exists($path);
169
    }
170 48
171 2
    /**
172
     * Check if object or path is a directory.
173
     *
174 46
     * @param  DirectoryIterator|\SplFileInfo|string  $value
175
     *
176
     * @return bool
177
     */
178
    public function isDirectory($value): bool
179
    {
180
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
181
            return $value->isDir();
182
        }
183
184 8
        return is_dir($value);
185
    }
186 8
187 4
    /**
188
     * Checks the existence of a directory.
189 4
     *
190
     * @param  DirectoryIterator|\SplFileInfo|string  $path
191
     *
192
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
193
     */
194
    public function validate($path): void
195
    {
196
        if (! $this->isDirectory($path)) {
197
            throw new DirectoryNotFoundException($path);
198
        }
199
    }
200 4
201
    /**
202 4
     * Checks the existence of a directory and return full path if exist.
203
     *
204 2
     * @param  DirectoryIterator|\SplFileInfo|string  $path
205
     *
206
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
207
     *
208
     * @return string
209
     */
210
    public function validated($path): string
211
    {
212
        $this->validate($path);
213
214
        return realpath($path);
215
    }
216
}
217