AbstractPhpFileCache::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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