Passed
Pull Request — main (#72)
by Andrey
15:04
created

File::delete()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 3
Bugs 3 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 17
ccs 4
cts 4
cp 1
crap 4
rs 9.9666
c 3
b 3
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 SplFileInfo;
11
use Throwable;
12
13
class File
14
{
15
    /**
16
     * Get a list of filenames along a path.
17
     *
18
     * @param  string  $path
19
     *
20
     * @return array
21
     */
22 28
    public function names(string $path): array
23
    {
24 28
        $items = [];
25
26 28
        /** @var \DirectoryIterator $item */
27 28
        foreach (DirectoryHelper::all($path) as $item) {
28
            if ($item->isFile()) {
29
                $items[] = $item->getFilename();
30
            }
31
        }
32
33
        sort($items);
34
35
        return array_values($items);
36 10
    }
37
38 10
    /**
39
     * Save content to file.
40
     *
41
     * @param  string  $path
42
     * @param  string  $content
43
     * @param  int  $mode
44
     */
45
    public function store(string $path, string $content, int $mode = 0755): void
46
    {
47
        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

47
        DirectoryHelper::make(/** @scrutinizer ignore-type */ pathinfo($path, PATHINFO_DIRNAME), $mode);
Loading history...
48 28
49
        file_put_contents($path, $content);
50 28
    }
51
52 28
    /**
53
     * Checks if the file exists.
54 28
     *
55
     * @param  string  $path
56 28
     *
57 28
     * @return bool
58
     */
59
    public function exists(string $path): bool
60
    {
61
        return file_exists($path) && is_file($path);
62
    }
63
64 28
    /**
65
     * Deletes files in the specified paths.
66
     *
67
     * @param  string|string[]  $paths
68
     *
69
     * @return bool
70
     */
71
    public function delete($paths): bool
72
    {
73
        $paths = Arr::wrap($paths);
74 10
75
        $success = true;
76 10
77 4
        foreach ($paths as $path) {
78
            try {
79
                if (! @unlink($path)) {
80 8
                    $success = false;
81
                }
82
            } catch (Throwable $e) {
83
                $success = false;
84
            }
85
        }
86
87
        return $success;
88
    }
89
90 4
    /**
91
     * Checks if an object or link is a file at the specified path.
92 4
     *
93 2
     * @param  \DirectoryIterator|\SplFileInfo|string  $value
94
     *
95 2
     * @return bool
96
     */
97
    public function isFile($value): bool
98
    {
99
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
100
            return $value->isFile();
101
        }
102
103
        return is_file($value);
104
    }
105
106
    /**
107
     * Checks the existence of a file.
108
     *
109
     * @param  \DirectoryIterator|\SplFileInfo|string  $path
110
     *
111
     * @throws \Helldar\Support\Exceptions\FileNotFoundException
112
     */
113
    public function validate($path): void
114
    {
115
        if (! $this->isFile($path)) {
116
            throw new FileNotFoundException($path);
117
        }
118
    }
119
}
120