Completed
Pull Request — master (#4)
by Ankit
02:16
created

FileStore::createCacheFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
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 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
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 6
    public function get(string $key, bool $withExpired = false)
104
    {
105 6
        $contents = $this->getCacheContents();
106
107 6
        if (empty($contents[$key])) {
108 2
            return null;
109
        }
110
111 4
        if ($withExpired) {
112
            return $contents[$key];
113
        }
114
115 4
        return $this->isValid($key) ? $contents[$key] : null;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121 7
    public function set(string $key, $value)
122
    {
123 7
        $cacheFile = $this->getCacheFile();
124
125 7
        if ( ! file_exists($cacheFile) || ! $this->isValid($key)) {
126 7
            $this->createCacheFile();
127
        }
128
129 7
        $contents = json_decode(file_get_contents($cacheFile), true) ?? [];
130
131 7
        if ( ! empty($contents[$key]) && is_array($value)) {
132 3
            $contents[$key] = $value + $contents[$key];
133
        } else {
134 7
            $contents[$key] = $value;
135
        }
136
137 7
        return file_put_contents($cacheFile, json_encode($contents));
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143 1
    public function delete(string $key)
144
    {
145 1
        $contents = $this->getCacheContents();
146
147 1
        if (isset($contents[$key])) {
148 1
            unset($contents[$key]);
149
150 1
            return false !== file_put_contents($this->getCacheFile(), json_encode($contents));
151
        }
152
153 1
        return false;
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159
    public function keys() : array
160
    {
161
        return array_keys($this->getCacheContents());
0 ignored issues
show
Bug introduced by
It seems like $this->getCacheContents() can also be of type boolean; however, parameter $input of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
        return array_keys(/** @scrutinizer ignore-type */ $this->getCacheContents());
Loading history...
162
    }
163
164
    /**
165
     * Check if cache is still valid.
166
     *
167
     * @param string $key
168
     *
169
     * @return bool
170
     */
171 3
    public function isValid(string $key) : bool
172
    {
173 3
        $meta = $this->getCacheContents()[$key] ?? [];
174
175 3
        if (empty($meta['expires_at'])) {
176 1
            return false;
177
        }
178
179 2
        return Carbon::now() < Carbon::createFromFormat(self::RFC_7231, $meta['expires_at']);
180
    }
181
182
    /**
183
     * Get cache contents.
184
     *
185
     * @return array|bool
186
     */
187 3
    public function getCacheContents()
188
    {
189 3
        $cacheFile = $this->getCacheFile();
190
191 3
        if ( ! file_exists($cacheFile)) {
192 1
            return false;
193
        }
194
195 2
        return json_decode(file_get_contents($cacheFile), true) ?? [];
196
    }
197
}
198