Completed
Push — 2.0 ( ace65e...4344f7 )
by Marco
09:00 queued 06:46
created

AbstractEnhancedProvider   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 314
Duplicated Lines 47.77 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 75.93%

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 10
dl 150
loc 314
ccs 82
cts 108
cp 0.7593
rs 8.8
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
getStats() 0 1 ?
A getProperties() 0 5 1
A test() 19 19 2
A saveDeferred() 11 11 1
A checkQueueNamespace() 14 14 3
B getItem() 0 24 4
A hasItem() 20 20 3
A clear() 0 16 2
A clearNamespace() 16 16 2
A deleteItem() 20 20 3
A save() 0 20 3
B getItems() 0 31 5
A deleteItems() 16 16 2
B commit() 25 25 4

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\Providers;
2
3
use \Comodojo\Cache\Traits\StatefulTrait;
4
use \Comodojo\Cache\Traits\NamespaceTrait;
5
use \Comodojo\Cache\Interfaces\EnhancedCacheItemPoolInterface;
6
use \Comodojo\Cache\Item;
7
use \Comodojo\Cache\Components\KeyValidator;
8
use \Comodojo\Foundation\Utils\UniqueId;
9
use \Comodojo\Foundation\Utils\ClassProperties;
10
use \Psr\Log\LoggerInterface;
11
use \Psr\Cache\CacheItemInterface;
12
use \DateTime;
13
use \Comodojo\Exception\CacheException;
14
use \Comodojo\Exception\InvalidCacheArgumentException;
15
use \Exception;
16
17
/**
18
 * @package     Comodojo Cache
19
 * @author      Marco Giovinazzi <[email protected]>
20
 * @license     MIT
21
 *
22
 * LICENSE:
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
 * THE SOFTWARE.
31
 */
32
33
abstract class AbstractEnhancedProvider
34
    extends AbstractProvider
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "AbstractProvider" and comma; 1 found
Loading history...
35
    implements EnhancedCacheItemPoolInterface {
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
36
37
    use StatefulTrait;
38
    use NamespaceTrait;
39
40
    protected $driver;
41
42
    protected $default_properties = [];
43
44
    protected $properties;
45
46
    private $queue = [];
47
48 211 View Code Duplication
    public function __construct(array $properties = [], LoggerInterface $logger = 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...
49
50 211
        parent::__construct($logger);
51
52 211
        $this->properties = ClassProperties::create($this->default_properties)->merge($properties);
53
54 211
        $this->setId(UniqueId::generate(64));
55
56 211
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    abstract public function getStats();
62
63 82
    public function getProperties() {
64
65 82
        return $this->properties;
66
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 176
    public function getItem($key) {
73
74 176
        if ( KeyValidator::validateKey($key) === false ) {
75
            throw new InvalidCacheArgumentException('Invalid key provided');
76 28
        }
77
78
        try {
79
80 176
            $data = $this->driver->get($key, $this->getNamespace());
81
82 176
        } catch (Exception $e) {
83
84
            $this->setState(self::CACHE_ERROR, $e->getMessage());
85
            $data = null;
86
87
        }
88
89 176
        if ( $data === null ) return new Item($key);
90
91 93
        $item = new Item($key, true);
92
93 93
        return $item->set(unserialize($data));
94
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 42 View Code Duplication
    public function hasItem($key) {
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...
101
102 42
        if ( KeyValidator::validateKey($key) === false ) {
103
            throw new InvalidCacheArgumentException('Invalid key provided');
104
        }
105
106
        try {
107
108 42
            $data = $this->driver->has($key, $this->getNamespace());
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
110 42
        } catch (Exception $e) {
111
112
            $this->setState(self::CACHE_ERROR, $e->getMessage());
113
            $data = false;
114
115
        }
116
117 42
        return $data;
118
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 57
    public function clear() {
125
126
        try {
127
128 57
            $data = $this->driver->clear();
129
130 57
        } catch (Exception $e) {
131
132
            $this->setState(self::CACHE_ERROR, $e->getMessage());
133
            $data = false;
134
135
        }
136
137 57
        return $data;
138
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 8 View Code Duplication
    public function clearNamespace() {
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
        try {
147
148 8
            $data = $this->driver->clear($this->getNamespace());
149
150 8
        } catch (Exception $e) {
151
152
            $this->setState(self::CACHE_ERROR, $e->getMessage());
153
            $data = false;
154
155
        }
156
157 8
        return $data;
158
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 10 View Code Duplication
    public function deleteItem($key) {
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...
165
166 10
        if ( KeyValidator::validateKey($key) === false ) {
167
            throw new InvalidCacheArgumentException('Invalid key provided');
168
        }
169
170
        try {
171
172 10
            $data = $this->driver->delete($key, $this->getNamespace());
173
174 10
        } catch (Exception $e) {
175
176
            $this->setState(self::CACHE_ERROR, $e->getMessage());
177
            $data = false;
178
179
        }
180
181 10
        return $data;
182
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 169
    public function save(CacheItemInterface $item) {
189
190 169
        $ttl = $item->getTtl();
191
192 169
        if ( $ttl < 0 ) return false;
193
194
        try {
195
196 120
            $data = $this->driver->set($item->getKey(), $this->getNamespace(), serialize($item->getRaw()), $ttl);
197
198 120
        } catch (Exception $e) {
199
200
            $this->setState(self::CACHE_ERROR, $e->getMessage());
201
            $data = false;
202
203
        }
204
205 120
        return $data;
206
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 211 View Code Duplication
    public function test() {
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...
213
214 211
        if ( $this->driver->test() ) {
215
216 211
            $this->setState(self::CACHE_SUCCESS);
217
218 211
            return true;
219
220
        }
221
222
        $error = $this->driver->getName()." driver unavailable, disabling provider ".$this->getId()." administratively";
223
224
        $this->logger->error($error);
225
226
        $this->setState(self::CACHE_ERROR, $error);
227
228
        return false;
229
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 12
    public function getItems(array $keys = []) {
236
237 12
        if ( empty($keys) ) return [];
238
239 6
        $result = [];
240
241
        try {
242
243 6
            $data = $this->driver->getMultiple($keys, $this->getNamespace());
244
245 6
        } catch (Exception $e) {
246
247
            $this->setState(self::CACHE_ERROR, $e->getMessage());
248
            $data = array_combine($keys, array_fill(0, count($keys), null));
249
250
        }
251
252 6
        foreach ( $data as $key => $value ) {
253
254 6
            if ( $value == null ) {
255
                $result[$key] = new Item($key);
256
            } else {
257 6
                $result[$key] = new Item($key, true);
258 6
                $result[$key]->set(unserialize($value));
259
            }
260
261 6
        }
262
263 6
        return $result;
264
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270 12 View Code Duplication
    public function deleteItems(array $keys) {
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...
271
272
        try {
273
274 12
            $data = $this->driver->deleteMultiple($keys, $this->getNamespace());
275
276 12
        } catch (Exception $e) {
277
278
            $this->setState(self::CACHE_ERROR, $e->getMessage());
279
            $data = false;
280
281
        }
282
283 12
        return $data;
284
285
    }
286
287
    /**
288
     * {@inheritdoc}
289
     */
290 12 View Code Duplication
    public function saveDeferred(CacheItemInterface $item) {
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...
291
292 12
        $this->checkQueueNamespace(true);
293
294 12
        $namespace = $this->getNamespace();
295
296 12
        $this->queue[$namespace][$item->getKey()] = $item;
297
298 12
        return true;
299
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305 12 View Code Duplication
    public function commit() {
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...
306
307 12
        $result = [];
308
309 12
        $active_namespace = $this->getNamespace();
310
311 12
        foreach ( $this->queue as $namespace => $queue ) {
312
313 12
            $this->setNamespace($namespace);
314
315 12
            foreach ( $queue as $key => $item ) {
316
317 12
                $result[] = $this->save($item);
318
319 12
            }
320
321 12
        }
322
323 12
        $this->queue = [];
324
325 12
        $this->setNamespace($active_namespace);
326
327 12
        return in_array(false, $result) ? false : true;
328
329
    }
330
331 12 View Code Duplication
    private function checkQueueNamespace($create = false) {
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...
332
333 12
        $namespace = $this->getNamespace();
334
335 12
        if ( array_key_exists($namespace, $this->queue) ) {
336 6
            return true;
337 12
        } else if ( $create ) {
338 12
            $this->queue[$namespace] = [];
339 12
            return true;
340
        } else {
341
            return false;
342
        }
343
344
    }
345
346
}
347