Completed
Push — master ( 696f4b...995f66 )
by Arman
17s queued 15s
created

FileAdapter::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 FileAdapter
24
 * @package Quantum\Libraries\Cache\Adapters
25
 */
26
class FileAdapter 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
     * FileAdapter constructor
46
     * @param array $params
47
     */
48
    public function __construct(array $params)
49
    {
50
        $this->fs = Di::get(FileSystem::class);
51
        $this->ttl = $params['ttl'];
52
        $this->cacheDir = $params['path'];
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function get($key, $default = null)
59
    {
60
        if ($this->has($key)) {
61
            $cacheItem = $this->fs->get($this->getPath($key));
62
63
            if (!$cacheItem) {
64
                return $default;
65
            }
66
67
            try {
68
                return unserialize($cacheItem);
69
            } catch (\Exception $e) {
70
                $this->delete($key);
71
                return $default;
72
            }
73
        }
74
75
        return $default;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function getMultiple($keys, $default = null)
82
    {
83
        if (!is_array($keys)) {
84
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$keys')), E_WARNING);
85
        }
86
87
        $result = [];
88
89
        foreach ($keys as $key) {
90
            $result[$key] = $this->get($key, $default);
91
        }
92
93
        return $result;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function has($key): bool
100
    {
101
        $path = $this->getPath($key);
102
103
        if (!$this->fs->exists($path)) {
104
            return false;
105
        }
106
107
        if (time() - $this->fs->lastModified($path) > $this->ttl) {
108
            $this->delete($key);
109
            return false;
110
        }
111
112
        return true;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function set($key, $value, $ttl = null)
119
    {
120
        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...
121
    }
122
123
    /**
124
     * @inheritDoc
125
     * @throws InvalidArgumentException
126
     */
127
    public function setMultiple($values, $ttl = null)
128
    {
129
        if (!is_array($values)) {
130
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING);
131
        }
132
133
        $results = [];
134
135
        foreach ($values as $key => $value) {
136
            $results[] = $this->set($key, $value, $ttl);
137
        }
138
139
        return !in_array(false, $results, true);
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function delete($key)
146
    {
147
        $path = $this->getPath($key);
148
149
        if ($this->fs->exists($path)) {
150
            return $this->fs->remove($path);
151
        }
152
153
        return false;
154
    }
155
156
    /**
157
     * @inheritDoc
158
     * @throws InvalidArgumentException
159
     */
160
    public function deleteMultiple($keys)
161
    {
162
        if (!is_array($keys)) {
163
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$keys')), E_WARNING);
164
        }
165
166
        $results = [];
167
168
        foreach ($keys as $key) {
169
            $results[] = $this->delete($key);
170
        }
171
172
        return !in_array(false, $results, true);
173
    }
174
175
    /**
176
     * @inheritDoc
177
     */
178
    public function clear()
179
    {
180
        if (!$this->fs->isDirectory($this->cacheDir)) {
181
            return false;
182
        }
183
184
        $files = $this->fs->glob($this->cacheDir . DS . '*');
185
186
        if (!$files) {
187
            return false;
188
        }
189
190
        foreach ($files as $file) {
191
            $this->fs->remove($file);
192
        }
193
194
        return true;
195
    }
196
197
    /**
198
     * Gets the path for Get for the given cache key
199
     * @param string $key
200
     * @return string
201
     */
202
    private function getPath(string $key): string
203
    {
204
        return $this->cacheDir . DS . sha1($key);
205
    }
206
207
}
208