Passed
Pull Request — master (#91)
by Arman
03:55 queued 01:11
created

DatabaseAdapter::getMultiple()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 13
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 \Quantum\Libraries\Database\DbalInterface
36
     */
37
    private $cacheModel;
38
39
    /**
40
     * DatabaseAdapter constructor
41
     * @param array $params
42
     */
43
    public function __construct(array $params)
44
    {
45
        $this->ttl = $params['ttl'];
46
        $this->cacheModel = Database::getInstance()->getOrm($params['table']);
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function get($key, $default = null)
53
    {
54
        if ($this->has($key)) {
55
            $cacheItem = $this->cacheModel->findOneBy('key', sha1($key));
56
57
            try {
58
                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

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