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

getAbsoluteProfilerFilename()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
ccs 5
cts 6
cp 0.8333
cc 4
nc 2
nop 0
crap 4.074
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