Test Failed
Push — develop ( 32ca41...230235 )
by Brent
04:59
created

File   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

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