Completed
Push — master ( 49a516...79d061 )
by Marco
04:40
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 \Comodojo\Cache\Components\UniqueId;
4
use \DateTime;
5
use \Exception;
6
7
/**
8
 * Apcu provider
9
 *
10
 * @package     Comodojo Spare Parts
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     MIT
13
 *
14
 * LICENSE:
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
class Memory extends AbstractDriver {
26
27
    const DRIVER_NAME = "memory";
28
29
    private $data = [];
30
31
    public function __construct(array $configuration = []) {}
32
33 85
    public function test() {
34
35 85
        return true;
36
37
    }
38
39 63
    public function get($key, $namespace) {
40
41 63
        return $this->checkMemory($key, $namespace) ?
42 63
            $this->data[$namespace][$key]['data'] : null;
43
44
    }
45
46 64
    public function set($key, $namespace, $value, $ttl = null) {
47
48 64
        $this->checkNamespace($namespace, true);
49
50 64
        $expire = is_null($ttl) || $ttl == 0 ? 0 : new DateTime("now + $ttl secs");
51
52 64
        $this->data[$namespace][$key] = [
53 64
            'data' => $value,
54 64
            'expire' => $expire
55
        ];
56
57 64
        return true;
58
59
    }
60
61 6
    public function delete($key, $namespace) {
62
63 6
        if ( !$this->checkMemory($key, $namespace) ) return false;
64
65 6
        unset($this->data[$namespace][$key]);
66
67 6
        return true;
68
69
    }
70
71 11
    public function clear($namespace = null) {
72
73 11
        if ( $namespace == null ) {
74
75 9
            $this->data = [];
76
77 9
            return true;
78
79
        } else {
80
81 2
            if ( $this->checkNamespace($namespace) ) {
82 2
                unset($this->data[$namespace]);
83 2
                return true;
84
            }
85
86
            return false;
87
88
        }
89
90
    }
91
92 2 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...
93
94 2
        $result = [];
95
96 2
        foreach ( $keys as $key ) {
97 2
            $result[$key] = $this->get($key, $namespace);
98
        }
99
100 2
        return $result;
101
102
    }
103
104 1 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...
105
106 1
        $result = [];
107
108 1
        foreach ( $key_values as $key => $value ) {
109 1
            $result[$key] = $this->set($key, $namespace, $value, $ttl);
110
        }
111
112 1
        return !in_array(false, $result);
113
114
    }
115
116 3 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...
117
118 3
        $result = [];
119
120 3
        foreach ( $keys as $key ) {
121 3
            $result[] = $this->delete($key, $namespace);
122
        }
123
124 3
        return !in_array(false, $result);
125
126
    }
127
128 41
    public function has($key, $namespace) {
129
130 41
        return $this->checkMemory($key, $namespace);
131
132
    }
133
134 2
    public function stats() {
135
136 2
        return ['objects' => count($this->data)];
137
138
    }
139
140 64
    private function checkNamespace($namespace, $create = false) {
141
142 64
        if ( array_key_exists($namespace, $this->data) ) {
143 12
            return true;
144 64
        } else if ( $create ) {
145 64
            $this->data[$namespace] = [];
146 64
            return true;
147
        } else {
148
            return false;
149
        }
150
151
    }
152
153 71
    private function checkMemory($key, $namespace) {
154
155 71
        $time = new DateTime('now');
156
157 71
        if ( !array_key_exists($namespace, $this->data)
158 60
            || !array_key_exists($key, $this->data[$namespace]) ) {
159 39
            return false;
160 60
        } else if ( $this->data[$namespace][$key]['expire'] === 0
161 36
            || $this->data[$namespace][$key]['expire'] > $time ) {
162 59
            return true;
163
        } else {
164 1
            unset($this->data[$namespace][$key]);
165 1
            return false;
166
        }
167
168
    }
169
170
}
171