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

AbstractFileProfiler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 1
b 0
f 0
dl 0
loc 31
rs 10
ccs 11
cts 12
cp 0.9167

3 Methods

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