Completed
Push — 2.0 ( ca9e1a...453d0a )
by Marco
04:47
created

Manager::deleteMultiple()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 7
cts 8
cp 0.875
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
crap 5.0488
1
<?php namespace Comodojo\SimpleCache;
2
3
use \Comodojo\SimpleCache\Providers\AbstractProvider;
4
use \Comodojo\SimpleCache\Providers\Vacuum;
5
use \Comodojo\SimpleCache\Interfaces\SimpleCacheManagerInterface;
6
use \Comodojo\SimpleCache\Interfaces\EnhancedSimpleCacheInterface;
7
use \Comodojo\Cache\Traits\NamespaceTrait;
8
use \Comodojo\Cache\Traits\GenericManagerTrait;
9
use \Comodojo\SimpleCache\Components\StackManager;
10
use \Comodojo\Foundation\Validation\DataFilter;
11
use \Comodojo\SimpleCache\Components\ConfigurationParser;
12
use \Comodojo\Foundation\Base\Configuration;
13
use \Psr\Log\LoggerInterface;
14
use \ArrayObject;
15
16
/**
17
 *
18
 * @package     Comodojo Spare Parts
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
class Manager extends AbstractProvider implements SimpleCacheManagerInterface {
34
35
    use NamespaceTrait, GenericManagerTrait {
36
        GenericManagerTrait::setNamespace insteadof NamespaceTrait;
37
    }
38
39
    const DEFAULT_PICK_MODE = 1;
40
41
    protected $pick_mode;
42
43
    protected $stack;
44
45
    protected $align_cache;
46
47
    protected $vacuum;
48
49
    protected $selected;
50
51 32 View Code Duplication
    public function __construct(
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...
52
        $pick_mode = null,
53
        LoggerInterface $logger = null,
54
        $align_cache = true,
55
        $flap_interval = null
56
    ) {
57
58 32
        $this->pick_mode = DataFilter::filterInteger($pick_mode, 1, 6, self::DEFAULT_PICK_MODE);
59
60 32
        $this->align_cache = DataFilter::filterBoolean($align_cache, true);
61
62 32
        $stack = new ArrayObject([]);
63
64 32
        $this->stack = new StackManager($stack->getIterator());
65 32
        $this->stack->setFlapInterval($flap_interval);
66
67 32
        parent::__construct($logger);
68
69 32
        $this->vacuum = new Vacuum($this->logger);
70
71 32
        $this->logger->info("SimpleCache Manager online; pick mode ".$this->pick_mode);
72
73 32
    }
74
75 31
    public function addProvider(EnhancedSimpleCacheInterface $provider, $weight = 0) {
76
77 31
        return $this->genericAddProvider($provider, $weight);
78
79
    }
80
81 24
    public function get($key, $default = null) {
82
83 24
        return $this->selectFrom('GET', $key, $default);
84
85
    }
86
87 28
    public function set($key, $value, $ttl = null) {
88
89 28
        if (iterator_count($this->stack) == 0) return $this->vacuum->set($key, $value, $ttl);
90
91 27
        if ( $this->align_cache === false && $this->pick_mode < 5) {
92
            return $this->selectProvider()->set($key, $value, $ttl);
93
        }
94
95 27
        $result = [];
96
97 27
        foreach ($this->stack as $provider) {
98
99 27
            $pro = $provider[0];
100
101 27
            $provider_result = $pro->set($key, $value, $ttl);
102
103 26
            $this->logger->debug("Saving item $key into provider ".
104 26
                $pro->getId().": ".($provider_result ? "OK" : "NOK - ".$pro->getStateMessage()));
105
106 26
            $result[] = $provider_result;
107
108
        }
109
110 26
        return !in_array(false, $result);
111
112
    }
113
114 3 View Code Duplication
    public function delete($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...
115
116 3
        if (iterator_count($this->stack) == 0) return $this->vacuum->delete($key);
117
118 2
        if ( $this->align_cache === false && $this->pick_mode < 5) {
119
            return $this->selectProvider()->delete($key);
120
        }
121
122 2
        $result = [];
123
124 2
        foreach ($this->stack as $provider) {
125
126 2
            $result[] = $provider[0]->delete($key);
127
128
        }
129
130 2
        return !in_array(false, $result);
131
132
    }
133
134 2
    public function getMultiple($keys, $default = null) {
135
136 2
        return $this->selectFrom('GETMULTIPLE', $keys, $default);
137
138
    }
139
140 2
    public function setMultiple($values, $ttl = null) {
141
142 2
        if (iterator_count($this->stack) == 0) return $this->vacuum->setMultiple($values, $ttl);
143
144 1
        if ( $this->align_cache === false && $this->pick_mode < 5) {
145
            return $this->selectProvider()->setMultiple($values, $ttl);
146
        }
147
148 1
        $result = [];
149
150 1
        foreach ($this->stack as $provider) {
151
152 1
            $result[] = $provider[0]->setMultiple($values, $ttl);
153
154
        }
155
156 1
        return !in_array(false, $result);
157
158
    }
159
160 2 View Code Duplication
    public function deleteMultiple($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...
161
162 2
        if (iterator_count($this->stack) == 0) return $this->vacuum->deleteMultiple($keys);
163
164 1
        if ( $this->align_cache === false && $this->pick_mode < 5) {
165
            return $this->selectProvider()->deleteMultiple($keys);
166
        }
167
168 1
        $result = [];
169
170 1
        foreach ($this->stack as $provider) {
171
172 1
            $result[] = $provider[0]->deleteMultiple($keys);
173
174
        }
175
176 1
        return !in_array(false, $result);
177
178
    }
179
180 18
    public function has($key) {
181
182 18
        return $this->selectFrom('HAS', $key);
183
184
    }
185
186 1 View Code Duplication
    public static function createFromConfiguration(Configuration $configuration, LoggerInterface $logger) {
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...
187
188 1
        list($manager_configuration, $providers) = ConfigurationParser::parse($configuration, $logger);
189
190 1
        $manager = new Manager(...$manager_configuration);
191
192 1
        foreach ($providers as $name => $provider) {
193 1
            $instance = $provider->instance;
194 1
            $weight = $provider->weight;
195 1
            $id = $instance->getId();
196 1
            $logger->debug("Adding provider $name ($id) to cache manager (w $weight)");
197 1
            $manager->addProvider($instance, $weight);
198
        }
199
200 1
        return $manager;
201
202
    }
203
204 30
    protected function selectFrom($mode, $key, $default=null) {
205
206 30 View Code Duplication
        if ( $this->pick_mode < 5 ) {
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...
207
208 28
            $result = $this->fromSingleProvider($mode, $key, $default);
209
210 2
        } else if ( $this->pick_mode == 5 ) {
211
212 1
            $result = $this->fromAllProviders($mode, $key, $default);
213
214
        } else {
215
216 1
            $result = $this->traverse($mode, $key, $default);
217
218
        }
219
220 30
        return $result;
221
222
    }
223
224 28
    protected function fromSingleProvider($mode, $key, $default) {
225
226 28
        $provider = $this->selectProvider();
227
228
        switch ($mode) {
229
230 28
            case 'GET':
231 23
                $data = $provider->get($key, $default);
232 23
                break;
233
234 17
            case 'GETMULTIPLE':
235 2
                $data = $provider->getMultiple($key, $default);
236 2
                break;
237
238 16
            case 'HAS':
239 16
                $data = $provider->has($key);
240 16
                break;
241
242
        }
243
244 28
        return $data;
0 ignored issues
show
Bug introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
245
246
    }
247
248 1
    protected function fromAllProviders($mode, $key, $default) {
249
250 1
        $result = [];
251
252 1
        if ( $mode == 'GET' ) {
253
254 1
            foreach ($this->stack as $provider) {
255
256 1
                $result[] = $provider[0]->get($key, $default);
257
258
                // selected provider has no sense in this case
259 1
                $this->selected = $provider[0];
260
261
            }
262
263 1
            if ( count(array_unique($result)) == 1 ) return $result[0];
264
265 1
            return $default;
266
267 1
        } else if ( $mode == 'GETMULTIPLE' ) {
268
269
            foreach ($this->stack as $provider) {
270
271
                $result[] = $provider[0]->getMultiple($key, $default);
272
273
                // selected provider has no sense in this case
274
                $this->selected = $provider[0];
275
276
            }
277
278
            if ( count(array_unique($result)) == 1 ) return $result[0];
279
280
            $this->selected = $this->vacuum;
281
282
            return $this->vacuum->getMultiple($key, $default);
283
284
            // return array_combine($key, array_fill(0, count($key), $default));
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
285
286 View Code Duplication
        } else {
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...
287
288 1
            foreach ($this->stack as $provider) {
289
290 1
                $result[] = $provider[0]->has($key);
291
292
                // selected provider has no sense in this case
293 1
                $this->selected = $provider[0];
294
295
            }
296
297 1
            return !in_array(false, $result);
298
299
        }
300
301
    }
302
303 1
    protected function traverse($mode, $key, $default) {
304
305 1
        $this->stack->rewind();
306
307 1
        if ( $mode == 'GET' ) {
308
309
            foreach ($this->stack as $provider) {
310
311
                $item = $provider[0]->get($key);
312
313
                if ( $item !== null ) {
314
315
                    // selected provider
316
                    $this->selected = $provider[0];
317
318
                    return $item;
319
320
                }
321
322
            }
323
324
            // selected provider has no sense in this case
325
            $this->selected = $this->vacuum;
326
327
            return $default;
328
329 1
        } else if ( $mode == 'GETMULTIPLE' ) {
330
331
            foreach ($this->stack as $provider) {
332
333
                $items = $provider[0]->getMultiple($key);
334
335
                $item_unique = array_unique($items);
336
337
                if ( count($item_unique) > 1 && $item_unique[0] !== null ) {
338
339
                    // selected provider
340
                    $this->selected = $provider[0];
341
342
                    return $items;
343
344
                }
345
346
            }
347
348
            // selected provider has no sense in this case
349
            $this->selected = $this->vacuum;
350
351
            return $this->vacuum->getMultiple($key, $default);
352
353 View Code Duplication
        } else {
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...
354
355 1
            foreach ($this->stack as $provider) {
356
357 1
                $item = $provider[0]->has($key);
358
359 1
                if ( $item === true ) {
360
361
                    // selected provider has no sense in this case
362 1
                    $this->selected = $provider[0];
363
364 1
                    return true;
365
366
                }
367
368
            }
369
370
            // selected provider has no sense in this case
371
            $this->selected = $this->vacuum;
372
373
            return false;
374
375
        }
376
377
    }
378
379
}
380