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

File::base()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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
    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