Completed
Push — orm-dm-info ( abf332 )
by Nikolaos
02:10
created

Memory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0019

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1.0019
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Storage\Adapter;
15
16
use DateInterval;
17
use Phalcon\Collection;
18
use Phalcon\Factory\Exception as ExceptionAlias;
19
use Phalcon\Storage\Exception;
20
use Phalcon\Storage\SerializerFactory;
21
22
/**
23
 * Memory adapter
24
 *
25
 * @property Collection $data
26
 * @property array      $options
27
 */
28
class Memory extends AbstractAdapter
29
{
30
    /**
31
     * @var Collection
32
     */
33
    protected $data;
34
35
    /**
36
     * @var array
37
     */
38
    protected $options = [];
39
40
    /**
41
     * Memory constructor.
42
     *
43
     * @param SerializerFactory $factory
44
     * @param array             $options
45
     *
46
     * @throws Exception
47
     * @throws ExceptionAlias
48
     */
49 32
    public function __construct(SerializerFactory $factory, array $options = [])
50
    {
51
        /**
52
         * Lets set some defaults and options here
53
         */
54 32
        $this->prefix  = "ph-memo-";
55 32
        $this->options = $options;
56 32
        $this->data    = new Collection();
57
58 32
        parent::__construct($factory, $options);
59
60 32
        $this->initSerializer();
61 32
    }
62
63
    /**
64
     * Flushes/clears the cache
65
     */
66 6
    public function clear(): bool
67
    {
68 6
        $this->data->clear();
69
70 6
        return true;
71
    }
72
73
    /**
74
     * Decrements a stored number
75
     *
76
     * @param string $key
77
     * @param int    $value
78
     *
79
     * @return bool|int
80
     */
81 2
    public function decrement(string $key, int $value = 1)
82
    {
83 2
        $prefixedKey = $this->getPrefixedKey($key);
84 2
        $result      = $this->data->has($prefixedKey);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->data->has($prefixedKey); of type boolean adds the type boolean to the return on line 94 which is incompatible with the return type declared by the interface Phalcon\Storage\Adapter\...terInterface::decrement of type integer.
Loading history...
85
86 2
        if ($result) {
87 2
            $current  = $this->data->get($prefixedKey);
88 2
            $newValue = (int) $current - $value;
89 2
            $result   = $newValue;
90
91 2
            $this->data->set($prefixedKey, $newValue);
92
        }
93
94 2
        return $result;
95
    }
96
97
    /**
98
     * Deletes data from the adapter
99
     *
100
     * @param string $key
101
     *
102
     * @return bool
103
     */
104 6
    public function delete(string $key): bool
105
    {
106 6
        $prefixedKey = $this->getPrefixedKey($key);
107 6
        $exists      = $this->data->has($prefixedKey);
108
109 6
        $this->data->remove($prefixedKey);
110
111 6
        return $exists;
112
    }
113
114
    /**
115
     * Reads data from the adapter
116
     *
117
     * @param string     $key
118
     * @param mixed|null $defaultValue
119
     *
120
     * @return mixed
121
     */
122 6
    public function get(string $key, $defaultValue = null)
123
    {
124 6
        $prefixedKey = $this->getPrefixedKey($key);
125 6
        $content     = $this->data->get($prefixedKey);
126
127 6
        return $this->getUnserializedData($content, $defaultValue);
128
    }
129
130
    /**
131
     * Always returns null
132
     *
133
     * @return null
134
     */
135 2
    public function getAdapter()
136
    {
137 2
        return $this->adapter;
138
    }
139
140
    /**
141
     * Stores data in the adapter
142
     *
143
     * @param string $prefix
144
     *
145
     * @return array
146
     */
147 2
    public function getKeys(string $prefix = ""): array
148
    {
149 2
        return $this->getFilteredKeys(
150 2
            $this->data->getKeys(),
151
            $prefix
152
        );
153
    }
154
155
    /**
156
     * Checks if an element exists in the cache
157
     *
158
     * @param string $key
159
     *
160
     * @return bool
161
     */
162 12
    public function has(string $key): bool
163
    {
164 12
        $prefixedKey = $this->getPrefixedKey($key);
165
166 12
        return $this->data->has($prefixedKey);
167
    }
168
169
    /**
170
     * Increments a stored number
171
     *
172
     * @param string $key
173
     * @param int    $value
174
     *
175
     * @return bool|int
176
     */
177 2
    public function increment(string $key, int $value = 1)
178
    {
179 2
        $prefixedKey = $this->getPrefixedKey($key);
180 2
        $result      = $this->data->has($prefixedKey);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->data->has($prefixedKey); of type boolean adds the type boolean to the return on line 190 which is incompatible with the return type declared by the interface Phalcon\Storage\Adapter\...terInterface::increment of type integer.
Loading history...
181
182 2
        if ($result) {
183 2
            $current  = $this->data->get($prefixedKey);
184 2
            $newValue = (int) $current + $value;
185 2
            $result   = $newValue;
186
187 2
            $this->data->set($prefixedKey, $newValue);
188
        }
189
190 2
        return $result;
191
    }
192
193
    /**
194
     * Stores data in the adapter
195
     *
196
     * @param string                $key
197
     * @param mixed                 $value
198
     * @param DateInterval|int|null $ttl
199
     *
200
     * @return bool
201
     * @throws \Exception
202
     */
203 18
    public function set(string $key, $value, $ttl = null): bool
204
    {
205 18
        $content     = $this->getSerializedData($value);
206 18
        $prefixedKey = $this->getPrefixedKey($key);
207
208 18
        $this->data->set($prefixedKey, $content);
209
210 18
        return true;
211
    }
212
}
213