Completed
Push — master ( b16e40...058ee0 )
by Marco
07:56
created

Manager::fromAllProviders()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 54
Code Lines 20

Duplication

Lines 14
Ratio 25.93 %

Code Coverage

Tests 13
CRAP Score 10.0155

Importance

Changes 0
Metric Value
dl 14
loc 54
ccs 13
cts 19
cp 0.6842
rs 7.4119
c 0
b 0
f 0
cc 8
eloc 20
nc 10
nop 3
crap 10.0155

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 35 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 35
        $this->pick_mode = DataFilter::filterInteger($pick_mode, 1, 6, self::DEFAULT_PICK_MODE);
59
60 35
        $this->align_cache = DataFilter::filterBoolean($align_cache, false);
61
62 35
        $stack = new ArrayObject([]);
63
64 35
        $this->stack = new StackManager($stack->getIterator());
65 35
        $this->stack->setFlapInterval($flap_interval);
66
67 35
        parent::__construct($logger);
68
69 35
        $this->vacuum = new Vacuum($this->logger);
70
71 35
        $this->logger->debug("SimpleCache Manager online; pick mode ".$this->pick_mode);
72
73 35
    }
74
75 34
    public function addProvider(EnhancedSimpleCacheInterface $provider, $weight = 0) {
76
77 34
        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 30
    public function set($key, $value, $ttl = null) {
88
89 30
        if ( iterator_count($this->stack) == 0 ) return $this->vacuum->set($key, $value, $ttl);
90
91 29
        if ( $this->align_cache === false && $this->pick_mode < 5 ) {
92 1
            return $this->selectProvider()->set($key, $value, $ttl);
93
        }
94
95 28
        $result = [];
96
97 28
        foreach ( $this->stack as $provider ) {
98
99 28
            $pro = $provider[0];
100
101 28
            $provider_result = $pro->set($key, $value, $ttl);
102
103 27
            $this->logger->debug("Saving item $key into provider ".
104 27
                $pro->getId().": ".($provider_result ? "OK" : "NOK - ".$pro->getStateMessage()));
105
106 27
            $result[] = $provider_result;
107
108
        }
109
110 27
        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($enable, $manager_configuration, $providers) = ConfigurationParser::parse($configuration, $logger);
189
190 1
        $manager = new Manager(...$manager_configuration);
191
192 1
        if ( $enable ) {
193 1
            foreach ( $providers as $name => $provider ) {
194 1
                $instance = $provider->instance;
195 1
                $weight = $provider->weight;
196 1
                $id = $instance->getId();
197 1
                $logger->debug("Adding provider $name ($id) to cache manager (w $weight)");
198 1
                $manager->addProvider($instance, $weight);
199
            }
200
        }
201
202 1
        return $manager;
203
204
    }
205
206 30
    protected function selectFrom($mode, $key, $default = null) {
207
208 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...
209
210 28
            $result = $this->fromSingleProvider($mode, $key, $default);
211
212 2
        } else if ( $this->pick_mode == 5 ) {
213
214 1
            $result = $this->fromAllProviders($mode, $key, $default);
215
216
        } else {
217
218 1
            $result = $this->traverse($mode, $key, $default);
219
220
        }
221
222 30
        return $result;
223
224
    }
225
226 28
    protected function fromSingleProvider($mode, $key, $default) {
227
228 28
        $provider = $this->selectProvider();
229
230
        switch ( $mode ) {
231
232 28
            case 'GET':
233 23
                $data = $provider->get($key, $default);
234 23
                break;
235
236 17
            case 'GETMULTIPLE':
237 2
                $data = $provider->getMultiple($key, $default);
238 2
                break;
239
240 16
            case 'HAS':
241 16
                $data = $provider->has($key);
242 16
                break;
243
244
        }
245
246 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...
247
248
    }
249
250 1
    protected function fromAllProviders($mode, $key, $default) {
251
252 1
        $result = [];
253
254 1
        if ( $mode == 'GET' ) {
255
256 1
            foreach ( $this->stack as $provider ) {
257
258 1
                $result[] = $provider[0]->get($key, $default);
259
260
                // selected provider has no sense in this case
261 1
                $this->selected = $provider[0];
262
263
            }
264
265 1
            if ( count(array_unique($result)) == 1 ) return $result[0];
266
267 1
            return $default;
268
269 1
        } else if ( $mode == 'GETMULTIPLE' ) {
270
271
            foreach ( $this->stack as $provider ) {
272
273
                $result[] = $provider[0]->getMultiple($key, $default);
274
275
                // selected provider has no sense in this case
276
                $this->selected = $provider[0];
277
278
            }
279
280
            if ( count(array_unique($result)) == 1 ) return $result[0];
281
282
            $this->selected = $this->vacuum;
283
284
            return $this->vacuum->getMultiple($key, $default);
285
286
            // 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...
287
288 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...
289
290 1
            foreach ( $this->stack as $provider ) {
291
292 1
                $result[] = $provider[0]->has($key);
293
294
                // selected provider has no sense in this case
295 1
                $this->selected = $provider[0];
296
297
            }
298
299 1
            return !in_array(false, $result);
300
301
        }
302
303
    }
304
305 1
    protected function traverse($mode, $key, $default) {
306
307 1
        $this->stack->rewind();
308
309 1
        if ( $mode == 'GET' ) {
310
311
            foreach ( $this->stack as $provider ) {
312
313
                $item = $provider[0]->get($key);
314
315
                if ( $item !== null ) {
316
317
                    // selected provider
318
                    $this->selected = $provider[0];
319
320
                    return $item;
321
322
                }
323
324
            }
325
326
            // selected provider has no sense in this case
327
            $this->selected = $this->vacuum;
328
329
            return $default;
330
331 1
        } else if ( $mode == 'GETMULTIPLE' ) {
332
333
            foreach ( $this->stack as $provider ) {
334
335
                $items = $provider[0]->getMultiple($key);
336
337
                $item_unique = array_unique($items);
338
339
                if ( count($item_unique) > 1 && $item_unique[0] !== null ) {
340
341
                    // selected provider
342
                    $this->selected = $provider[0];
343
344
                    return $items;
345
346
                }
347
348
            }
349
350
            // selected provider has no sense in this case
351
            $this->selected = $this->vacuum;
352
353
            return $this->vacuum->getMultiple($key, $default);
354
355 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...
356
357 1
            foreach ( $this->stack as $provider ) {
358
359 1
                $item = $provider[0]->has($key);
360
361 1
                if ( $item === true ) {
362
363
                    // selected provider has no sense in this case
364 1
                    $this->selected = $provider[0];
365
366 1
                    return true;
367
368
                }
369
370
            }
371
372
            // selected provider has no sense in this case
373
            $this->selected = $this->vacuum;
374
375
            return false;
376
377
        }
378
379
    }
380
381
}
382