Passed
Push — feature/gacela-file-cache ( a1109a...a7fc9c )
by Jesús
03:57
created

AbstractJsonFileProfiler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 6
eloc 9
dl 0
loc 28
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateProfiler() 0 5 1
A __construct() 0 3 1
A getAbsoluteProfilerFilename() 0 10 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver\Profiler;
6
7
use RuntimeException;
8
9
use function json_encode;
10
11
abstract class AbstractJsonFileProfiler implements FileProfilerInterface
12
{
13
    private string $profilerDir;
14
15 1
    public function __construct(string $profilerDir)
16
    {
17 1
        $this->profilerDir = $profilerDir;
18
    }
19
20 1
    public function updateProfiler(array $data): void
21
    {
22 1
        $fileContent = json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
23
24 1
        file_put_contents($this->getAbsoluteProfilerFilename(), $fileContent);
25
    }
26
27
    abstract protected function getProfilerFilename(): string;
28
29 1
    private function getAbsoluteProfilerFilename(): string
30
    {
31 1
        if (!is_dir($this->profilerDir)
32 1
            && !mkdir($concurrentDirectory = $this->profilerDir, 0777, true)
33 1
            && !is_dir($concurrentDirectory)
34
        ) {
35
            throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
36
        }
37
38 1
        return $this->profilerDir . DIRECTORY_SEPARATOR . $this->getProfilerFilename();
39
    }
40
}
41