Passed
Push — main ( 2b2dbc...8d3842 )
by Andrey
27:05 queued 25:36
created

File   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 5
Bugs 5 Features 0
Metric Value
eloc 28
dl 0
loc 126
ccs 33
cts 35
cp 0.9429
rs 10
c 5
b 5
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 4 2
A isFile() 0 7 2
A names() 0 18 5
A store() 0 5 1
A exists() 0 3 2
A delete() 0 17 4
A validated() 0 5 1
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
     * @param  callable|null  $callback
20
     *
21
     * @return array
22
     */
23 4
    public function names(string $path, callable $callback = null): array
24
    {
25 4
        $items = [];
26
27
        /** @var \DirectoryIterator $item */
28 4
        foreach (DirectoryHelper::all($path) as $item) {
29 4
            if ($item->isFile()) {
30 4
                $name = $item->getFilename();
31
32 4
                if (! is_callable($callback) || $callback($name)) {
33 4
                    $items[] = $item->getFilename();
34
                }
35
            }
36
        }
37
38 4
        sort($items);
39
40 4
        return array_values($items);
41
    }
42
43
    /**
44
     * Save content to file.
45
     *
46
     * @param  string  $path
47
     * @param  string  $content
48
     * @param  int  $mode
49
     */
50 28
    public function store(string $path, string $content, int $mode = 0755): void
51
    {
52 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

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