Passed
Pull Request — master (#26)
by Dmitriy
06:22 queued 03:41
created

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