Passed
Push — main ( ecd686...4ce043 )
by Andrey
01:26
created

File::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
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 SplFileInfo;
11
use Throwable;
12
13
class File
14
{
15
    /**
16
     * Save content to file.
17
     *
18
     * @param  string  $path
19
     * @param  string  $content
20
     * @param  int  $mode
21
     */
22 24
    public function store(string $path, string $content, int $mode = 755): void
23
    {
24 24
        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

24
        DirectoryHelper::make(/** @scrutinizer ignore-type */ pathinfo($path, PATHINFO_DIRNAME), $mode);
Loading history...
25
26 24
        file_put_contents($path, $content);
27 24
    }
28
29
    /**
30
     * Checks if the file exists.
31
     *
32
     * @param  string  $path
33
     *
34
     * @return bool
35
     */
36 10
    public function exists(string $path): bool
37
    {
38 10
        return file_exists($path) && is_file($path);
39
    }
40
41
    /**
42
     * Deletes files in the specified paths.
43
     *
44
     * @param  string|string[]  $paths
45
     *
46
     * @return bool
47
     */
48 24
    public function delete($paths): bool
49
    {
50 24
        $paths = Arr::wrap($paths);
51
52 24
        $success = true;
53
54 24
        foreach ($paths as $path) {
55
            try {
56 24
                if (! @unlink($path)) {
57 24
                    $success = false;
58
                }
59
            }
60
            catch (Throwable $e) {
61
                $success = false;
62
            }
63
        }
64
65 24
        return $success;
66
    }
67
68
    /**
69
     * Checks if an object or link is a file at the specified path.
70
     *
71
     * @param  \DirectoryIterator|\SplFileInfo|string  $value
72
     *
73
     * @return bool
74
     */
75 10
    public function isFile($value): bool
76
    {
77 10
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
78 4
            return $value->isFile();
79
        }
80
81 8
        return is_file($value);
82
    }
83
84
    /**
85
     * Checks the existence of a file.
86
     *
87
     * @param  \DirectoryIterator|\SplFileInfo|string  $path
88
     *
89
     * @throws \Helldar\Support\Exceptions\FileNotFoundException
90
     */
91 4
    public function validate($path): void
92
    {
93 4
        if (! $this->isFile($path)) {
94 2
            throw new FileNotFoundException($path);
95
        }
96 2
    }
97
}
98