Completed
Push — 2.0 ( b22aa0...07e083 )
by Marco
11:24 queued 07:13
created

Memory::clear()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3.0175
1
<?php namespace Comodojo\Cache\Drivers;
2
3
use \DateTime;
4
5
/**
6
 * @package     Comodojo Cache
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
21
class Memory extends AbstractDriver {
22
23
    const DRIVER_NAME = "memory";
24
25
    /**
26
     * Cache repository
27
     *
28
     * @param array
29
     */
30
    private $data = [];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function __construct(array $configuration = []) {}
36
37
        /**
38
         * {@inheritdoc}
39
         */
40 124
        public function test() {
41
42 124
        return true;
43
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 97
    public function get($key, $namespace) {
50
51 97
        return $this->checkMemory($key, $namespace) ?
52 97
            $this->data[$namespace][$key]['data'] : null;
53
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 90
    public function set($key, $namespace, $value, $ttl = null) {
60
61 90
        $this->checkNamespace($namespace, true);
62
63 90
        $expire = is_null($ttl) || $ttl == 0 ? 0 : new DateTime("now + $ttl secs");
64
65 90
        $this->data[$namespace][$key] = [
66 90
            'data' => $value,
67 90
            'expire' => $expire
68
        ];
69
70 90
        return true;
71
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 12
    public function delete($key, $namespace) {
78
79 12
        if ( !$this->checkMemory($key, $namespace) ) return false;
80
81 12
        unset($this->data[$namespace][$key]);
82
83 12
        return true;
84
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 22
    public function clear($namespace = null) {
91
92 22
        if ( $namespace == null ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $namespace of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
93
94 18
            $this->data = [];
95
96 18
            return true;
97
98
        } else {
99
100 4
            if ( $this->checkNamespace($namespace) ) {
101 4
                unset($this->data[$namespace]);
102 4
                return true;
103
            }
104
105
            return false;
106
107
        }
108
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 3 View Code Duplication
    public function getMultiple(array $keys, $namespace) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
116 3
        $result = [];
117
118 3
        foreach ( $keys as $key ) {
119 3
            $result[$key] = $this->get($key, $namespace);
120
        }
121
122 3
        return $result;
123
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 2 View Code Duplication
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
131 2
        $result = [];
132
133 2
        foreach ( $key_values as $key => $value ) {
134 2
            $result[$key] = $this->set($key, $namespace, $value, $ttl);
135
        }
136
137 2
        return !in_array(false, $result);
138
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 4 View Code Duplication
    public function deleteMultiple(array $keys, $namespace) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
146 4
        $result = [];
147
148 4
        foreach ( $keys as $key ) {
149 4
            $result[] = $this->delete($key, $namespace);
150
        }
151
152 4
        return !in_array(false, $result);
153
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 46
    public function has($key, $namespace) {
160
161 46
        return $this->checkMemory($key, $namespace);
162
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 4
    public function stats() {
169
170 4
        return ['objects' => count($this->data)];
171
172
    }
173
174 90
    private function checkNamespace($namespace, $create = false) {
175
176 90
        if ( array_key_exists($namespace, $this->data) ) {
177 17
            return true;
178 90
        } else if ( $create ) {
179 90
            $this->data[$namespace] = [];
180 90
            return true;
181
        } else {
182
            return false;
183
        }
184
185
    }
186
187 106
    private function checkMemory($key, $namespace) {
188
189 106
        $time = new DateTime('now');
190
191 106
        if ( !array_key_exists($namespace, $this->data)
192 106
            || !array_key_exists($key, $this->data[$namespace]) ) {
193 67
            return false;
194 84
        } else if ( $this->data[$namespace][$key]['expire'] === 0
195 84
            || $this->data[$namespace][$key]['expire'] > $time ) {
196 82
            return true;
197
        } else {
198 2
            unset($this->data[$namespace][$key]);
199 2
            return false;
200
        }
201
202
    }
203
204
}
205