Passed
Push — develop ( ec1e42...80559e )
by Brent
03:22
created

File::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
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
    private static $fs;
10
    private static $base;
11
12 50
    public static function base(?string $base): void
13
    {
14 50
        self::$base = rtrim($base, '/');
15 50
    }
16
17 50
    public static function path(string $path = null): string
18
    {
19 50
        $path = str_replace(self::$base, '', $path);
20 50
        $path = ltrim($path, '/');
21 50
        $path = "/{$path}";
22
23 50
        return self::$base . $path;
24
    }
25
26 22
    public static function read(string $path): ?string
27
    {
28 22
        $path = self::path($path);
29
30 22
        if (! file_exists(self::path($path))) {
31
            return null;
32
        }
33
34 22
        $contents = @file_get_contents(self::path($path));
35
36 22
        return $contents ?? null;
37
    }
38
39 12
    public static function write(string $path, $content = null): void
40
    {
41 12
        if (! self::$fs) {
42 1
            self::$fs = new Filesystem();
43
        }
44
45 12
        self::$fs->dumpFile(self::path($path), $content);
46 12
    }
47
}
48