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

File::delete()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 7
Bugs 7 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 18
ccs 7
cts 9
cp 0.7778
crap 4.1755
rs 9.9666
c 7
b 7
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers\Filesystem;
4
5
use DirectoryIterator;
6
use Helldar\Support\Exceptions\FileNotFoundException;
7
use Helldar\Support\Facades\Helpers\Arr;
8
use Helldar\Support\Facades\Helpers\Filesystem\Directory as DirectoryHelper;
9
use Helldar\Support\Facades\Helpers\Instance;
10
use Helldar\Support\Facades\Helpers\Str;
11
use SplFileInfo;
12
use Throwable;
13
14
class File
15
{
16
    /**
17
     * Get a list of filenames along a path.
18
     *
19
     * @param  string  $path
20
     * @param  callable|null  $callback
21
     * @param  bool  $recursive
22
     *
23
     * @return array
24
     */
25 6
    public function names(string $path, callable $callback = null, bool $recursive = false): array
26
    {
27 6
        $items = [];
28
29
        /** @var \DirectoryIterator $item */
30 6
        foreach (DirectoryHelper::all($path) as $item) {
31 6
            if ($item->isFile()) {
32 6
                $name = $item->getFilename();
33
34 6
                if (! is_callable($callback) || $callback($name)) {
35 6
                    $items[] = $name;
36
                }
37
            }
38
39 6
            if ($recursive && $item->isDir() && ! $item->isDot()) {
40 2
                $prefix = (string) Str::of($item->getRealPath())
41 2
                    ->after(realpath($path))
42 2
                    ->trim('\\/');
43
44 2
                foreach ($this->names($item->getRealPath(), $callback, $recursive) as $value) {
45 2
                    $items[] = $prefix . '/' . $value;
46
                }
47
            }
48
        }
49
50 6
        sort($items);
51
52 6
        return array_values($items);
53
    }
54
55
    /**
56
     * Save content to file.
57
     *
58
     * @param  string  $path
59
     * @param  string  $content
60
     * @param  int  $mode
61
     *
62
     * @return string Returns the full path to the saved file.
63
     */
64 28
    public function store(string $path, string $content, int $mode = 0755): string
65
    {
66 28
        DirectoryHelper::make(pathinfo($path, PATHINFO_DIRNAME), $mode);
0 ignored issues
show
Bug introduced by
It seems like pathinfo($path, Helldar\...ystem\PATHINFO_DIRNAME) can also be of type array; however, parameter $path of Helldar\Support\Facades\...ystem\Directory::make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
        DirectoryHelper::make(/** @scrutinizer ignore-type */ pathinfo($path, PATHINFO_DIRNAME), $mode);
Loading history...
67
68 28
        file_put_contents($path, $content);
69
70 28
        return realpath($path);
71
    }
72
73
    /**
74
     * Checks if the file exists.
75
     *
76
     * @param  string  $path
77
     *
78
     * @return bool
79
     */
80 10
    public function exists(string $path): bool
81
    {
82 10
        return file_exists($path) && is_file($path);
83
    }
84
85
    /**
86
     * Deletes files in the specified paths.
87
     *
88
     * @param  string|string[]  $paths
89
     *
90
     * @return bool
91
     */
92 28
    public function delete($paths): bool
93
    {
94 28
        $paths = Arr::wrap($paths);
95
96 28
        $success = true;
97
98 28
        foreach ($paths as $path) {
99
            try {
100 28
                if (! @unlink($path)) {
101 28
                    $success = false;
102
                }
103
            }
104
            catch (Throwable $e) {
105
                $success = false;
106
            }
107
        }
108 28
109
        return $success;
110
    }
111
112
    /**
113
     * Ensure the file has been deleted.
114
     *
115
     * @param  string|array  $paths
116
     *
117
     * @return bool
118 14
     */
119
    public function ensureDelete($paths): bool
120 14
    {
121 4
        $paths = Arr::wrap($paths);
122
123
        $success = true;
124 12
125
        foreach ($paths as $path) {
126
            if ($this->exists($path) && ! $this->delete($path)) {
127
                $success = false;
128
            }
129
        }
130
131
        return $success;
132
    }
133
134 8
    /**
135
     * Checks if an object or link is a file at the specified path.
136 8
     *
137 4
     * @param  \DirectoryIterator|\SplFileInfo|string  $value
138
     *
139 4
     * @return bool
140
     */
141
    public function isFile($value): bool
142
    {
143
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
144
            return $value->isFile();
145
        }
146
147
        return is_file($value);
148
    }
149
150 4
    /**
151
     * Checks the existence of a file.
152 4
     *
153
     * @param  \DirectoryIterator|\SplFileInfo|string  $path
154 2
     *
155
     * @throws \Helldar\Support\Exceptions\FileNotFoundException
156
     */
157
    public function validate($path): void
158
    {
159
        if (! $this->isFile($path)) {
160
            throw new FileNotFoundException($path);
161
        }
162
    }
163
164
    /**
165
     * Checks the existence of a file and return full path if exist.
166
     *
167
     * @param  \DirectoryIterator|\SplFileInfo|string  $path
168
     *
169
     * @throws \Helldar\Support\Exceptions\FileNotFoundException
170
     *
171
     * @return string
172
     */
173
    public function validated($path): string
174
    {
175
        $this->validate($path);
176
177
        return realpath($path);
178
    }
179
}
180