Passed
Push — main ( 8aefd5...83f2f3 )
by Andrey
23:12 queued 21:38
created

File::ensureDelete()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
c 0
b 0
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 32
    public function store(string $path, string $content, int $mode = 0755): string
65
    {
66 32
        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 32
        file_put_contents($path, $content);
69
70 32
        return realpath($path);
71
    }
72
73
    /**
74
     * Checks if the file exists.
75
     *
76
     * @param  string  $path
77
     *
78
     * @return bool
79
     */
80 14
    public function exists(string $path): bool
81
    {
82 14
        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 32
    public function delete($paths): bool
93
    {
94 32
        $paths = Arr::wrap($paths);
95
96 32
        $success = true;
97
98 32
        foreach ($paths as $path) {
99
            try {
100 32
                if (! @unlink($path)) {
101 32
                    $success = false;
102
                }
103
            } catch (Throwable $e) {
104
                $success = false;
105
            }
106
        }
107
108 32
        return $success;
109
    }
110
111
    /**
112
     * Ensure the file has been deleted.
113
     *
114
     * @param  array|string  $paths
115
     *
116
     * @return bool
117
     */
118 4
    public function ensureDelete($paths): bool
119
    {
120 4
        $paths = Arr::wrap($paths);
121
122 4
        $success = true;
123
124 4
        foreach ($paths as $path) {
125 4
            if ($this->exists($path) && ! $this->delete($path)) {
126
                $success = false;
127
            }
128
        }
129
130 4
        return $success;
131
    }
132
133
    /**
134
     * Checks if an object or link is a file at the specified path.
135
     *
136
     * @param  \DirectoryIterator|\SplFileInfo|string  $value
137
     *
138
     * @return bool
139
     */
140 14
    public function isFile($value): bool
141
    {
142 14
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
143 4
            return $value->isFile();
144
        }
145
146 12
        return is_file($value);
147
    }
148
149
    /**
150
     * Checks the existence of a file.
151
     *
152
     * @param  \DirectoryIterator|\SplFileInfo|string  $path
153
     *
154
     * @throws \Helldar\Support\Exceptions\FileNotFoundException
155
     */
156 8
    public function validate($path): void
157
    {
158 8
        if (! $this->isFile($path)) {
159 4
            throw new FileNotFoundException($path);
160
        }
161 4
    }
162
163
    /**
164
     * Checks the existence of a file and return full path if exist.
165
     *
166
     * @param  \DirectoryIterator|\SplFileInfo|string  $path
167
     *
168
     * @throws \Helldar\Support\Exceptions\FileNotFoundException
169
     *
170
     * @return string
171
     */
172 4
    public function validated($path): string
173
    {
174 4
        $this->validate($path);
175
176 2
        return realpath($path);
177
    }
178
}
179