Passed
Pull Request — master (#88)
by Arman
06:12 queued 03:03
created

FileCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum\Libraries\Cache\Adapters;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Psr\SimpleCache\CacheInterface;
19
use InvalidArgumentException;
20
use Quantum\Di\Di;
21
22
/**
23
 * Class FileCache
24
 * @package Quantum\Libraries\Cache\Adapters
25
 */
26
class FileCache implements CacheInterface
27
{
28
29
    /**
30
     * @var FileSystem
31
     */
32
    private $fs;
33
34
    /**
35
     * @var int
36
     */
37
    private $ttl = 30;
38
39
    /**
40
     * @var string
41
     */
42
    private $cacheDir;
43
44
    /**
45
     * FileCache constructor
46
     * @param array $params
47
     */
48
    public function __construct(array $params)
49
    {
50
        $this->fs = Di::get(FileSystem::class);
51
52
        $this->ttl = $params['ttl'];
53
54
        $this->cacheDir = $params['path'];
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function get($key, $default = null)
61
    {
62
        if ($this->has($key)) {
63
            $path = $this->getPath($key);
64
65
            $content = $this->fs->get($path);
66
67
            if (!$content) {
68
                return $default;
69
            }
70
71
            try {
72
                return unserialize($content);
73
            } catch (\Exception $e) {
74
                $this->fs->remove($path);
75
                return $default;
76
            }
77
        }
78
79
        return $default;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function getMultiple($keys, $default = null)
86
    {
87
        if (!is_array($keys)) {
88
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$keys')), E_WARNING);
89
        }
90
91
        $result = [];
92
93
        foreach ($keys as $key) {
94
            $result[$key] = $this->get($key, $default);
95
        }
96
97
        return $result;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function has($key): bool
104
    {
105
        $path = $this->getPath($key);
106
107
        if (!$this->fs->exists($path)) {
108
            return false;
109
        }
110
111
        if (time() - $this->fs->lastModified($path) > $this->ttl) {
112
            $this->delete($key);
113
            return false;
114
        }
115
116
        return true;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function set($key, $value, $ttl = null)
123
    {
124
        return $this->fs->put($this->getPath($key), serialize($value));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fs->put($t...ey), serialize($value)) also could return the type integer which is incompatible with the return type mandated by Psr\SimpleCache\CacheInterface::set() of boolean.
Loading history...
125
    }
126
127
    /**
128
     * @inheritDoc
129
     * @throws InvalidArgumentException
130
     */
131
    public function setMultiple($values, $ttl = null)
132
    {
133
        if (!is_array($values)) {
134
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING);
135
        }
136
137
        $results = [];
138
139
        foreach ($values as $key => $value) {
140
            $results[] = $this->set($key, $value, $ttl);
141
        }
142
143
        return !in_array(false, $results, true);
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    public function delete($key)
150
    {
151
        $path = $this->getPath($key);
152
153
        if ($this->fs->exists($path)) {
154
            return $this->fs->remove($path);
155
        }
156
157
        return false;
158
    }
159
160
    /**
161
     * @inheritDoc
162
     * @throws InvalidArgumentException
163
     */
164
    public function deleteMultiple($keys)
165
    {
166
        if (!is_array($keys)) {
167
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING);
168
        }
169
170
        $results = [];
171
172
        foreach ($keys as $key) {
173
            $results[] = $this->delete($key);
174
        }
175
176
        return !in_array(false, $results, true);
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    public function clear()
183
    {
184
        if (!$this->fs->isDirectory($this->cacheDir)) {
185
            return false;
186
        }
187
188
        $files = $this->fs->glob($this->cacheDir . DS . '*');
189
190
        if (!$files) {
191
            return false;
192
        }
193
194
        foreach ($files as $file) {
195
            $this->fs->remove($file);
196
        }
197
198
        return true;
199
    }
200
201
    /**
202
     * Gets the path for Get for the given cache key
203
     * @param string $key
204
     * @return string
205
     */
206
    private function getPath(string $key): string
207
    {
208
        return $this->cacheDir . DS . sha1($key);
209
    }
210
211
}
212