Test Failed
Push — develop ( 32ca41...230235 )
by Brent
04:59
created

File::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
7
class File
8
{
9
    /** @var Filesystem */
10
    private static $fs;
11
12
    /** @var string */
13
    private static $base;
14
15 37
    public static function base(?string $base): void
16
    {
17 37
        self::$base = rtrim($base, '/');
18 37
    }
19
20 37
    public static function path(string $path = null): string
21
    {
22 37
        $path = str_replace(self::$base, '', $path);
23 37
        $path = ltrim($path, '/');
24 37
        $path = "/{$path}";
25
26 37
        return self::$base . $path;
27
    }
28
29 1
    public static function relativePath(?string $path): string
30
    {
31 1
        $path = str_replace(self::$base, '', $path);
32
33 1
        return ".{$path}";
34
    }
35
36 11
    public static function read(string $path): ?string
37
    {
38 11
        $path = self::path($path);
39
40 11
        if (! file_exists(self::path($path))) {
41
            return null;
42
        }
43
44 11
        $contents = @file_get_contents(self::path($path));
45
46 11
        return $contents ?? null;
47
    }
48
49 1
    public static function write(string $path, $content = null): void
50
    {
51 1
        if (! self::$fs) {
52
            self::$fs = new Filesystem();
53
        }
54
55 1
        self::$fs->dumpFile(self::path($path), $content);
56 1
    }
57
}
58