Passed
Pull Request — master (#26)
by Dmitriy
03:42
created

FastRouteCache::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 30
ccs 0
cts 17
cp 0
crap 12
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\FastRoute;
6
7
use Psr\SimpleCache\CacheInterface;
8
9
class FastRouteCache implements CacheInterface
10
{
11
    /**
12
     * Template used when generating the cache file.
13
     */
14
    public const CACHE_TEMPLATE = <<< 'EOT'
15
<?php
16
return %s;
17
EOT;
18
    /**
19
     * Cache file path relative to the project directory.
20
     *
21
     * @var string
22
     */
23
    private string $cacheFile = __DIR__ . '/../../../../runtime/cache/fastroute.php.cache';
24
25
    public function __construct($cacheFilePath = null)
26
    {
27
        if ($cacheFilePath !== null) {
28
            $this->cacheFile = $cacheFilePath;
29
        }
30
    }
31
32
    public function get($key, $default = null): array
33
    {
34
        set_error_handler(
35
            static function () {
36
            },
37
            E_WARNING
38
        ); // suppress php warnings
39
        $data = include $this->cacheFile;
40
        restore_error_handler();
41
42
        // Cache file does not exist
43
        if (false === $data) {
44
            throw new \RuntimeException(
45
                sprintf(
46
                    'File "%s"; doesn\'t exist',
47
                    $this->cacheFile
48
                )
49
            );
50
        }
51
52
        if (!is_array($data)) {
53
            throw new \RuntimeException(
54
                sprintf(
55
                    'Invalid cache file "%s"; cache file MUST return an array',
56
                    $this->cacheFile
57
                )
58
            );
59
        }
60
61
        return $data;
62
    }
63
64
    public function set($key, $value, $ttl = null): void
65
    {
66
        $cacheDir = dirname($this->cacheFile);
67
68
        if (!is_dir($cacheDir)) {
69
            throw new \RuntimeException(
70
                sprintf(
71
                    'The cache directory "%s" does not exist',
72
                    $cacheDir
73
                )
74
            );
75
        }
76
77
        if (!is_writable($cacheDir)) {
78
            throw new \RuntimeException(
79
                sprintf(
80
                    'The cache directory "%s" is not writable',
81
                    $cacheDir
82
                )
83
            );
84
        }
85
86
        if (file_exists($this->cacheFile) && !is_writable($this->cacheFile)) {
87
            throw new \RuntimeException(
88
                sprintf(
89
                    'The cache file %s is not writable',
90
                    $this->cacheFile
91
                )
92
            );
93
        }
94
95
        $result = file_put_contents(
96
            $this->cacheFile,
97
            sprintf(self::CACHE_TEMPLATE, var_export($value, true)),
98
            LOCK_EX
99
        );
100
101
        if ($result === false) {
102
            throw new \RuntimeException(
103
                sprintf(
104
                    'Can\'t write file "%s"',
105
                    $this->cacheFile
106
                )
107
            );
108
        }
109
    }
110
111
    public function delete($key): bool
112
    {
113
        return unlink($this->cacheFile);
114
    }
115
116
    public function clear(): bool
117
    {
118
        return unlink($this->cacheFile);
119
    }
120
121
    public function has($key): bool
122
    {
123
        return file_exists($this->cacheFile);
124
    }
125
126
    public function setMultiple($values, $ttl = null)
127
    {
128
        throw new \RuntimeException('Method is not implemented');
129
    }
130
131
    public function getMultiple($keys, $default = null)
132
    {
133
        throw new \RuntimeException('Method is not implemented');
134
    }
135
136
    public function deleteMultiple($keys)
137
    {
138
        throw new \RuntimeException('Method is not implemented');
139
    }
140
}
141