Completed
Push — 2.0 ( a52f91...c9f4d8 )
by Marco
12:16
created

Memcached   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 188
Duplicated Lines 16.49 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 3
dl 31
loc 188
rs 9.3999
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A test() 0 5 1
A get() 0 15 3
A set() 0 15 4
A delete() 11 11 2
A clear() 13 17 3
B getMultiple() 0 30 5
B setMultiple() 0 19 5
A deleteMultiple() 0 15 2
A has() 0 5 2
A stats() 0 5 1
A setNamespaceKey() 7 7 2
A getNamespaceKey() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Cache\Drivers;
2
3
use \Comodojo\Cache\Traits\InstanceTrait;
4
use \Comodojo\Cache\Components\UniqueId;
5
use \Memcached as MemcachedInstance;
6
use \Exception;
7
8
/**
9
 * memcached provider
10
 *
11
 * @package     Comodojo Spare Parts
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
class Memcached extends AbstractDriver {
27
28
    use InstanceTrait;
29
30
    const DRIVER_NAME = "memcached";
31
32
    public function __construct(array $configuration = []) {
33
34
        if ( class_exists('Memcached') === false ) throw new Exception("ext-memcached not available");
35
36
        $this->setInstance(new MemcachedInstance($configuration['persistent_id']));
37
        $this->getInstance()
38
            ->addServer($configuration['server'], $configuration['port'], $configuration['weight']);
39
40
    }
41
42
    public function test() {
43
44
        return sizeof($this->getInstance()->getServerList()) > 0;
45
46
    }
47
48
    public function get($key, $namespace) {
49
50
        $scope = $this->getNamespaceKey($namespace);
51
52
        if ( $scope === false ) return null;
53
54
        $shadowName = "$scope-$key";
55
56
        $instance = $this->getInstance();
57
58
        $item = $instance->get($shadowName);
59
60
        return $instance->getResultCode() == MemcachedInstance::RES_NOTFOUND ? null : $item;
61
62
    }
63
64
    public function set($key, $namespace, $value, $ttl = null) {
65
66
        if ( $ttl == null ) $ttl = 0;
67
68
        $scope = $this->getNamespaceKey($namespace);
69
70
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
71
72
        if ( $scope === false ) return false;
73
74
        $shadowName = "$scope-$key";
75
76
        return $this->getInstance()->set($shadowName, $value, $ttl);
77
78
    }
79
80 View Code Duplication
    public function delete($key, $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...
81
82
        $scope = $this->getNamespaceKey($namespace);
83
84
        if ( $scope === false ) return false;
85
86
        $shadowName = "$scope-$key";
87
88
        return $this->getInstance()->delete($shadowName);
89
90
    }
91
92
    public function clear($namespace = null) {
93
94 View Code Duplication
        if ( $namespace == null ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
96
            return $this->getInstance()->flush();
97
98
        } else {
99
100
            $scope = $this->getNamespaceKey($namespace);
101
102
            if ( $scope === false ) return false;
103
104
            return $this->getInstance()->delete($namespace);
105
106
        }
107
108
    }
109
110
    public function getMultiple(array $keys, $namespace) {
111
112
        if ( empty($keys) ) return [];
113
114
        $keypad = array_combine($keys, array_fill(0, count($keys), null));
115
116
        $scope = $this->getNamespaceKey($namespace);
117
118
        if ( $scope === false ) return $keypad;
119
120
        $keyscope = array_map(function($key) use($scope) {
121
            return "$scope-$key";
122
        }, $keys);
123
124
        if (version_compare(phpversion(), '7.0.0', '<')) {
125
            $data = $this->getInstance()->getMulti($keyscope, $null = null, MemcachedInstance::GET_PRESERVE_ORDER);
126
        } else {
127
            $data = $this->getInstance()->getMulti($keyscope, MemcachedInstance::GET_PRESERVE_ORDER);
128
        }
129
130
        $return = [];
131
132
        foreach ($data as $scoped_key => $value) {
133
            $key = substr($scoped_key, strlen("$scope-"));
134
            $return[$key] = $value;
135
        }
136
137
        return array_replace($keypad, $return);
138
139
    }
140
141
    public function setMultiple(array $key_values, $namespace, $ttl = null) {
142
143
        if ( $ttl == null ) $ttl = 0;
144
145
        $scope = $this->getNamespaceKey($namespace);
146
147
        if ( $scope === false ) $scope = $this->setNamespaceKey($namespace);
148
149
        if ( $scope === false ) return false;
150
151
        $shadowNames = [];
152
153
        foreach ($key_values as $key => $value) {
154
            $shadowNames["$scope-$key"] = $value;
155
        }
156
157
        return $this->getInstance()->setMulti($shadowNames, $ttl);
158
159
    }
160
161
    public function deleteMultiple(array $keys, $namespace) {
162
163
        $scope = $this->getNamespaceKey($namespace);
164
165
        if ( $scope === false ) return false;
166
167
        $shadowNames = array_map(function($key) use($scope) {
168
            return "$scope-$key";
169
        }, $keys);
170
171
        $delete = $this->getInstance()->deleteMulti($shadowNames);
172
173
        return count(array_diff(array_unique($delete), [true])) === 0;
174
175
    }
176
177
    public function has($key, $namespace) {
178
179
        return $this->get($key, $namespace) === null ? false : true;
180
181
    }
182
183
    public function stats() {
184
185
        return $this->getInstance()->getStats();
186
187
    }
188
189
    /**
190
     * Set namespace key
191
     *
192
     * @return  mixed
193
     */
194 View Code Duplication
    private function setNamespaceKey($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...
195
196
        $uId = UniqueId::get();
197
198
        return $this->getInstance()->set($namespace, $uId, 0) === false ? false : $uId;
199
200
    }
201
202
    /**
203
     * Get namespace key
204
     *
205
     * @return  string
206
     */
207
    private function getNamespaceKey($namespace) {
208
209
        return $this->getInstance()->get($namespace);
210
211
    }
212
213
}
214