Passed
Branch v2 (aaaa8e)
by Brent
03:38
created

File::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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