Passed
Push — bugfix/support-windows ( 517fb4...0e99e7 )
by Chema
04:47
created

AbstractPhpFileCache::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 0
Metric Value
eloc 5
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 4.074
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver\Cache;
6
7
use RuntimeException;
8
9
abstract class AbstractPhpFileCache implements CacheInterface
10
{
11
    /** @var array<class-string,array<string,string>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string,array<string,string>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,array<string,string>>.
Loading history...
12
    private static array $cache = [];
13
14 4
    public function __construct(
15
        private string $cacheDir,
16
    ) {
17 4
        self::$cache[static::class] = $this->getExistingCache();
18
    }
19
20
    /**
21
     * @internal
22
     *
23
     * @return array<string,string>
24
     */
25 2
    public static function all(): array
26
    {
27 2
        return self::$cache[static::class];
28
    }
29
30 5
    public function has(string $cacheKey): bool
31
    {
32 5
        return isset(self::$cache[static::class][$cacheKey]);
33
    }
34
35 4
    public function get(string $cacheKey): string
36
    {
37 4
        return self::$cache[static::class][$cacheKey];
38
    }
39
40
    public function getAll(): array
41
    {
42
        return self::$cache[static::class];
43
    }
44
45 3
    public function put(string $cacheKey, string $className): void
46
    {
47 3
        self::$cache[static::class][$cacheKey] = $className;
48
49 3
        $fileContent = sprintf(
50 3
            '<?php return %s;',
51 3
            var_export(self::$cache[static::class], true),
52 3
        );
53
54 3
        file_put_contents($this->getAbsoluteCacheFilename(), $fileContent);
55
    }
56
57
    abstract protected function getCacheFilename(): string;
58
59
    /**
60
     * @return array<string,string>
61
     */
62 4
    private function getExistingCache(): array
63
    {
64 4
        $filename = $this->getAbsoluteCacheFilename();
65
66 4
        if (file_exists($filename)) {
67
            /** @var array<string,string> $content */
68 1
            $content = require $filename;
69
70 1
            return $content;
71
        }
72
73 3
        return [];
74
    }
75
76 5
    private function getAbsoluteCacheFilename(): string
77
    {
78 5
        if (!is_dir($this->cacheDir)
79 5
            && !mkdir($concurrentDirectory = $this->cacheDir, 0777, true)
80 5
            && !is_dir($concurrentDirectory)
81
        ) {
82
            throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
83
        }
84
85 5
        return $this->cacheDir . DIRECTORY_SEPARATOR . $this->getCacheFilename();
86
    }
87
}
88