Passed
Push — develop ( e32fe8...a6721e )
by Brent
02:31
created

File::relativePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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