Passed
Push — feature/change-cache-to-profil... ( 1b58b5...c0f98d )
by Chema
03:41
created

AbstractJsonFileProfiler::updateProfiler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver;
6
7
use RuntimeException;
8
9
use function json_encode;
10
11
abstract class AbstractJsonFileProfiler implements FileProfilerInterface
12
{
13
    private string $cacheDir;
14
15 4
    public function __construct(string $cacheDir)
16
    {
17 4
        $this->cacheDir = $cacheDir;
18
    }
19
20 2
    public function updateProfiler(array $cache): void
21
    {
22 2
        $fileContent = json_encode($cache, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
23
24 2
        file_put_contents($this->getAbsoluteCacheFilename(), $fileContent);
25
    }
26
27
    abstract protected function getCacheFilename(): string;
28
29 2
    private function getAbsoluteCacheFilename(): string
30
    {
31 2
        if (!is_dir($this->cacheDir)
32 2
            && !mkdir($concurrentDirectory = $this->cacheDir, 0777, true)
33 2
            && !is_dir($concurrentDirectory)
34
        ) {
35
            throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
36
        }
37
38 2
        return $this->cacheDir . '/' . $this->getCacheFilename();
39
    }
40
}
41