Completed
Pull Request — master (#14)
by Ankit
02:22
created

FileStore::getActualCacheKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace TusPhp\Cache;
4
5
use Carbon\Carbon;
6
7
class FileStore extends AbstractCache
8
{
9
    /** @var string */
10
    protected $cacheDir;
11
12
    /** @var string */
13
    protected $cacheFile;
14
15
    /**
16
     * FileStore constructor.
17
     */
18 1
    public function __construct()
19
    {
20 1
        $this->setCacheDir(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . '.cache' . DIRECTORY_SEPARATOR);
21 1
        $this->setCacheFile('tus_php.cache');
22 1
    }
23
24
    /**
25
     * Set cache dir.
26
     *
27
     * @param string $path
28
     *
29
     * @return self
30
     */
31 1
    public function setCacheDir(string $path) : self
32
    {
33 1
        $this->cacheDir = $path;
34
35 1
        return $this;
36
    }
37
38
    /**
39
     * Get cache dir.
40
     *
41
     * @return string
42
     */
43 1
    public function getCacheDir() : string
44
    {
45 1
        return $this->cacheDir;
46
    }
47
48
    /**
49
     * Set cache file.
50
     *
51
     * @param string $file
52
     *
53
     * @return self
54
     */
55 1
    public function setCacheFile(string $file) : self
56
    {
57 1
        $this->cacheFile = $file;
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * Get cache file.
64
     *
65
     * @return string
66
     */
67 3
    public function getCacheFile() : string
68
    {
69 3
        return $this->cacheDir . $this->cacheFile;
70
    }
71
72
    /**
73
     * Create cache dir if not exists.
74
     *
75
     * @return void
76
     */
77 2
    protected function createCacheDir()
78
    {
79 2
        if ( ! file_exists($this->cacheDir)) {
80 2
            mkdir($this->cacheDir);
81
        }
82 2
    }
83
84
    /**
85
     * Create cache file and add required meta.
86
     *
87
     * @return void
88
     */
89 2
    protected function createCacheFile()
90
    {
91 2
        $this->createCacheDir();
92
93 2
        $cacheFilePath = $this->getCacheFile();
94
95 2
        if ( ! file_exists($cacheFilePath)) {
96 2
            touch($cacheFilePath);
97
        }
98 2
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103 8
    public function get(string $key, bool $withExpired = false)
104
    {
105 8
        $key      = $this->getActualCacheKey($key);
106 8
        $contents = $this->getCacheContents();
107
108 8
        if (empty($contents[$key])) {
109 3
            return null;
110
        }
111
112 5
        if ($withExpired) {
113 1
            return $contents[$key];
114
        }
115
116 5
        return $this->isValid($key) ? $contents[$key] : null;
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122 8
    public function set(string $key, $value)
123
    {
124 8
        $key       = $this->getActualCacheKey($key);
125 8
        $cacheFile = $this->getCacheFile();
126
127 8
        if ( ! file_exists($cacheFile) || ! $this->isValid($key)) {
128 8
            $this->createCacheFile();
129
        }
130
131 8
        $contents = json_decode(file_get_contents($cacheFile), true) ?? [];
132
133 8
        if ( ! empty($contents[$key]) && is_array($value)) {
134 3
            $contents[$key] = $value + $contents[$key];
135
        } else {
136 8
            $contents[$key] = $value;
137
        }
138
139 8
        return file_put_contents($cacheFile, json_encode($contents));
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145 1
    public function delete(string $key) : bool
146
    {
147 1
        $key      = $this->getActualCacheKey($key);
148 1
        $contents = $this->getCacheContents();
149
150 1
        if (isset($contents[$key])) {
151 1
            unset($contents[$key]);
152
153 1
            return false !== file_put_contents($this->getCacheFile(), json_encode($contents));
154
        }
155
156 1
        return false;
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162 2
    public function keys() : array
163
    {
164 2
        $contents = $this->getCacheContents();
165
166 2
        if (is_array($contents)) {
167 1
            return array_keys($this->getCacheContents());
168
        }
169
170 1
        return [];
171
    }
172
173
    /**
174
     * Check if cache is still valid.
175
     *
176
     * @param string $key
177
     *
178
     * @return bool
179
     */
180 3
    public function isValid(string $key) : bool
181
    {
182 3
        $key  = $this->getActualCacheKey($key);
183 3
        $meta = $this->getCacheContents()[$key] ?? [];
184
185 3
        if (empty($meta['expires_at'])) {
186 1
            return false;
187
        }
188
189 2
        return Carbon::now() < Carbon::createFromFormat(self::RFC_7231, $meta['expires_at']);
190
    }
191
192
    /**
193
     * Get cache contents.
194
     *
195
     * @return array|bool
196
     */
197 3
    public function getCacheContents()
198
    {
199 3
        $cacheFile = $this->getCacheFile();
200
201 3
        if ( ! file_exists($cacheFile)) {
202 1
            return false;
203
        }
204
205 2
        return json_decode(file_get_contents($cacheFile), true) ?? [];
206
    }
207
208
    /**
209
     * Get actual cache key with prefix.
210
     *
211
     * @param string $key
212
     *
213
     * @return string
214
     */
215 1
    public function getActualCacheKey(string $key) : string
216
    {
217 1
        $prefix = $this->getPrefix();
218
219 1
        if (false === strpos($key, $prefix)) {
220 1
            $key = $prefix . $key;
221
        }
222
223 1
        return $key;
224
    }
225
}
226