Passed
Push — feature/change-cache-to-profil... ( 160b60 )
by Chema
04:07
created

AbstractFileProfiler::getAbsoluteCacheFilename()   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 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
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;
6
7
use RuntimeException;
8
9
abstract class AbstractFileProfiler implements FileProfilerInterface
10
{
11
    private string $cacheDir;
12
13 4
    public function __construct(string $cacheDir)
14
    {
15 4
        $this->cacheDir = $cacheDir;
16
    }
17
18 2
    public function updateFileCache(array $cache): void
19
    {
20 2
        $fileContent = sprintf(
21
            '<?php return %s;',
22 2
            var_export($cache, true)
23
        );
24
25 2
        file_put_contents($this->getAbsoluteCacheFilename(), $fileContent);
26
    }
27
28
    abstract protected function getCacheFilename(): string;
29
30 2
    private function getAbsoluteCacheFilename(): string
31
    {
32 2
        if (!is_dir($this->cacheDir)
33 2
            && !mkdir($concurrentDirectory = $this->cacheDir, 0777, true)
34 2
            && !is_dir($concurrentDirectory)
35
        ) {
36
            throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
37
        }
38
39 2
        return $this->cacheDir . '/' . $this->getCacheFilename();
40
    }
41
}
42