Passed
Pull Request — master (#96)
by Arman
02:54
created

DatabaseAdapter::keyHash()   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 1
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\Database\Database;
18
use Psr\SimpleCache\CacheInterface;
19
use InvalidArgumentException;
20
use Exception;
21
22
/**
23
 * Class DatabaseAdapter
24
 * @package Quantum\Libraries\Cache\Adapters
25
 */
26
class DatabaseAdapter implements CacheInterface
27
{
28
29
    /**
30
     * @var int
31
     */
32
    private $ttl = 30;
33
34
    /**
35
     * @var string
36
     */
37
    private $prefix;
38
39
    /**
40
     * @var \Quantum\Libraries\Database\DbalInterface
41
     */
42
    private $cacheModel;
43
44
    /**
45
     * DatabaseAdapter constructor
46
     * @param array $params
47
     */
48
    public function __construct(array $params)
49
    {
50
        $this->ttl = $params['ttl'];
51
        $this->prefix = $params['prefix'];
52
        $this->cacheModel = Database::getInstance()->getOrm($params['table']);
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function get($key, $default = null)
59
    {
60
        if ($this->has($key)) {
61
            $cacheItem = $this->cacheModel->findOneBy('key', $this->keyHash($key));
62
63
            try {
64
                return unserialize($cacheItem->prop('value'));
0 ignored issues
show
Bug introduced by
It seems like $cacheItem->prop('value') can also be of type null; however, parameter $data of unserialize() does only seem to accept string, 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

64
                return unserialize(/** @scrutinizer ignore-type */ $cacheItem->prop('value'));
Loading history...
65
            } catch (Exception $e) {
66
                $this->delete($key);
67
                return $default;
68
            }
69
        }
70
71
        return $default;
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function getMultiple($keys, $default = null)
78
    {
79
        if (!is_array($keys)) {
80
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$keys')), E_WARNING);
81
        }
82
83
        $result = [];
84
85
        foreach ($keys as $key) {
86
            $result[$key] = $this->get($key, $default);
87
        }
88
89
        return $result;
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95
    public function has($key): bool
96
    {
97
        $cacheItem = $this->cacheModel->findOneBy('key', $this->keyHash($key));
98
99
        if (empty($cacheItem->asArray())) {
100
            return false;
101
        }
102
103
        if (time() - $cacheItem->prop('ttl') > $this->ttl) {
104
            $this->delete($key);
105
            return false;
106
        }
107
108
        return true;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function set($key, $value, $ttl = null)
115
    {
116
        $cacheItem = $this->cacheModel->findOneBy('key', $this->keyHash($key));
117
118
        if (empty($cacheItem->asArray())) {
119
            $cacheItem = $this->cacheModel->create();
120
        }
121
122
        $cacheItem->prop('key', $this->keyHash($key));
123
        $cacheItem->prop('value', serialize($value));
124
        $cacheItem->prop('ttl', time());
125
126
        return $cacheItem->save();
127
    }
128
129
    /**
130
     * @inheritDoc
131
     * @throws InvalidArgumentException
132
     */
133
    public function setMultiple($values, $ttl = null)
134
    {
135
        if (!is_array($values)) {
136
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING);
137
        }
138
139
        $results = [];
140
141
        foreach ($values as $key => $value) {
142
            $results[] = $this->set($key, $value, $ttl);
143
        }
144
145
        return !in_array(false, $results, true);
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function delete($key)
152
    {
153
        $cacheItem = $this->cacheModel->findOneBy('key', $this->keyHash($key));
154
155
        if (!empty($cacheItem->asArray())) {
156
            return $this->cacheModel->delete();
157
        }
158
159
        return false;
160
    }
161
162
    /**
163
     * @inheritDoc
164
     * @throws InvalidArgumentException
165
     */
166
    public function deleteMultiple($keys)
167
    {
168
        if (!is_array($keys)) {
169
            throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$keys')), E_WARNING);
170
        }
171
172
        $results = [];
173
174
        foreach ($keys as $key) {
175
            $results[] = $this->delete($key);
176
        }
177
178
        return !in_array(false, $results, true);
179
    }
180
181
    /**
182
     * @inheritDoc
183
     */
184
    public function clear()
185
    {
186
        return $this->cacheModel->deleteMany();
187
    }
188
189
    /**
190
     * Gets the hashed key
191
     * @param string $key
192
     * @return string
193
     */
194
    private function keyHash(string $key): string
195
    {
196
        return sha1($this->prefix . $key);
197
    }
198
199
}
200