Passed
Push — feature/improve-file-cache ( 6742d2 )
by Chema
04:18
created

AbstractPhpFileCache::resetCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
rs 10
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
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
    private string $cacheDir;
15
16 4
    public function __construct(string $cacheDir)
17
    {
18 4
        $this->cacheDir = $cacheDir;
19 4
        self::$cache[static::class] = $this->getExistingCache();
20
    }
21
22
    /**
23
     * @internal
24
     *
25
     * @return array<string,string>
26
     */
27 2
    public static function all(): array
28
    {
29 2
        return self::$cache[static::class];
30
    }
31
32 6
    public function has(string $cacheKey): bool
33
    {
34 6
        return isset(self::$cache[static::class][$cacheKey]);
35
    }
36
37 4
    public function get(string $cacheKey): string
38
    {
39 4
        return self::$cache[static::class][$cacheKey];
40
    }
41
42
    public function getAll(): array
43
    {
44
        return self::$cache[static::class];
45
    }
46
47 4
    public function put(string $cacheKey, string $className): void
48
    {
49 4
        self::$cache[static::class][$cacheKey] = $className;
50
51 4
        $fileContent = sprintf(
52
            '<?php return %s;',
53 4
            var_export(self::$cache[static::class], true)
54
        );
55
56 4
        file_put_contents($this->getAbsoluteCacheFilename(), $fileContent);
57
    }
58
59
    abstract protected function getCacheFilename(): string;
60
61
    /**
62
     * @return array<string,string>
63
     */
64 4
    private function getExistingCache(): array
65
    {
66 4
        $filename = $this->getAbsoluteCacheFilename();
67
68 4
        if (file_exists($filename)) {
69
            /** @var array<string,string> $content */
70 2
            $content = require $filename;
71
72 2
            return $content;
73
        }
74
75 2
        return [];
76
    }
77
78 6
    private function getAbsoluteCacheFilename(): string
79
    {
80 6
        if (!is_dir($this->cacheDir)
81 6
            && !mkdir($concurrentDirectory = $this->cacheDir, 0777, true)
82 6
            && !is_dir($concurrentDirectory)
83
        ) {
84
            throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
85
        }
86
87 6
        return $this->cacheDir . DIRECTORY_SEPARATOR . $this->getCacheFilename();
88
    }
89
}
90