Passed
Pull Request — main (#76)
by Andrey
27:17 queued 12:21
created

Directory::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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 14
    public function all(string $path): DirectoryIterator
24
    {
25 14
        if ($this->doesntExist($path)) {
26 4
            throw new DirectoryNotFoundException($path);
27
        }
28
29 10
        return new DirectoryIterator($path);
30
    }
31
32
    /**
33
     * Get a list of directory names along a path.
34
     *
35
     * @param  string  $path
36
     * @param  callable|null  $callback
37
     *
38
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
39
     *
40
     * @return array
41
     */
42 4
    public function names(string $path, callable $callback = null): array
43
    {
44 4
        $items = [];
45
46
        /** @var \DirectoryIterator $directory */
47 4
        foreach ($this->all($path) as $directory) {
48 4
            if ($directory->isDir() && ! $directory->isDot()) {
49 4
                $name = $directory->getFilename();
50
51 4
                if (! is_callable($callback) || $callback($name)) {
52 4
                    $items[] = $name;
53
                }
54
            }
55
        }
56
57 4
        sort($items);
58
59 4
        return array_values($items);
60
    }
61
62
    /**
63
     * Create a directory at the specified path.
64
     *
65
     * @param  string  $path
66
     * @param  int  $mode
67
     *
68
     * @return bool
69
     */
70 32
    public function make(string $path, int $mode = 0755): bool
71
    {
72 32
        return $this->doesntExist($path) ? mkdir($path, $mode, true) : true;
73
    }
74
75
    /**
76
     * Delete the directory with all contents in the specified path.
77
     *
78
     * @param  string  $path
79
     *
80
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
81
     *
82
     * @return bool
83
     */
84 34
    public function delete(string $path): bool
85
    {
86 34
        if (! $this->isDirectory($path)) {
87 4
            throw new DirectoryNotFoundException($path);
88
        }
89
90 32
        $items = new FilesystemIterator($path);
91
92 32
        $success = true;
93
94 32
        foreach ($items as $item) {
95 30
            $item->isDir() && ! $item->isLink()
96 30
                ? $this->delete($item->getPathname())
97 24
                : FileHelper::delete($item->getPathname());
98
        }
99
100 32
        @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

100
        /** @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...
101
102 32
        return $success;
103
    }
104
105
    /**
106
     * Check if the directory exists.
107
     *
108
     * @param  string  $path
109
     *
110
     * @return bool
111
     */
112 340
    public function exists(string $path): bool
113
    {
114 340
        return file_exists($path) && is_dir($path);
115
    }
116
117
    /**
118
     * Check if the directory doesn't exists.
119
     *
120
     * @param  string  $path
121
     *
122
     * @return bool
123
     */
124 48
    public function doesntExist(string $path): bool
125
    {
126 48
        return ! $this->exists($path);
127
    }
128
129
    /**
130
     * Check if object or path is a directory.
131
     *
132
     * @param  DirectoryIterator|\SplFileInfo|string  $value
133
     *
134
     * @return bool
135
     */
136 42
    public function isDirectory($value): bool
137
    {
138 42
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
139 2
            return $value->isDir();
140
        }
141
142 40
        return is_dir($value);
143
    }
144
145
    /**
146
     * Checks the existence of a directory.
147
     *
148
     * @param  DirectoryIterator|\SplFileInfo|string  $path
149
     *
150
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
151
     */
152 4
    public function validate($path): void
153
    {
154 4
        if (! $this->isDirectory($path)) {
155 2
            throw new DirectoryNotFoundException($path);
156
        }
157 2
    }
158
159
    /**
160
     * Checks the existence of a directory and return full path if exist.
161
     *
162
     * @param  DirectoryIterator|\SplFileInfo|string  $path
163
     *
164
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
165
     *
166
     * @return string
167
     */
168
    public function validated($path): string
169
    {
170
        $this->validate($path);
171
172
        return realpath($path);
173
    }
174
}
175