File::ensureDelete()   A
last analyzed

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
 * This file is part of the "andrey-helldar/support" project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Andrey Helldar <[email protected]>
9
 *
10
 * @copyright 2021 Andrey Helldar
11
 *
12
 * @license MIT
13
 *
14
 * @see https://github.com/andrey-helldar/support
15
 */
16
17
namespace Helldar\Support\Helpers\Filesystem;
18
19
use DirectoryIterator;
20
use Helldar\Support\Exceptions\FileNotFoundException;
21
use Helldar\Support\Facades\Helpers\Arr;
22
use Helldar\Support\Facades\Helpers\Filesystem\Directory as DirectoryHelper;
23
use Helldar\Support\Facades\Helpers\Instance;
24
use Helldar\Support\Facades\Helpers\Str;
25
use SplFileInfo;
26
use Throwable;
27
28
class File
29
{
30
    /**
31
     * Get a list of filenames along a path.
32
     *
33
     * @param  string  $path
34
     * @param  callable|null  $callback
35
     * @param  bool  $recursive
36
     *
37
     * @return array
38
     */
39 6
    public function names(string $path, callable $callback = null, bool $recursive = false): array
40
    {
41 6
        $items = [];
42
43
        /** @var \DirectoryIterator $item */
44 6
        foreach (DirectoryHelper::all($path) as $item) {
45 6
            if ($item->isFile()) {
46 6
                $name = $item->getFilename();
47
48 6
                if (! is_callable($callback) || $callback($name)) {
49 6
                    $items[] = $name;
50
                }
51
            }
52
53 6
            if ($recursive && $item->isDir() && ! $item->isDot()) {
54 2
                $prefix = (string) Str::of($item->getRealPath())
55 2
                    ->after(realpath($path))
56 2
                    ->trim('\\/');
57
58 2
                foreach ($this->names($item->getRealPath(), $callback, $recursive) as $value) {
59 2
                    $items[] = $prefix . '/' . $value;
60
                }
61
            }
62
        }
63
64 6
        sort($items);
65
66 6
        return array_values($items);
67
    }
68
69
    /**
70
     * Save content to file.
71
     *
72
     * @param  string  $path
73
     * @param  string  $content
74
     * @param  int  $mode
75
     *
76
     * @return string Returns the full path to the saved file.
77
     */
78 30
    public function store(string $path, string $content, int $mode = 0755): string
79
    {
80 30
        DirectoryHelper::ensureDirectory(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\...tory::ensureDirectory() 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

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