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

File   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A base() 0 4 1
A path() 0 8 1
A relativePath() 0 6 1
A read() 0 12 2
A write() 0 8 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 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