Passed
Push — feature/change-cache-to-profil... ( 1b58b5...c0f98d )
by Chema
03:41
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
c 0
b 0
f 0
dl 0
loc 28
rs 10
ccs 10
cts 11
cp 0.9091

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAbsoluteCacheFilename() 0 10 4
A updateProfiler() 0 5 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