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

Manager   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 223
Duplicated Lines 40.36 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 10
dl 90
loc 223
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 23 23 1
A addProvider() 0 5 1
A getItem() 0 5 1
A hasItem() 0 5 1
A deleteItem() 17 17 4
B save() 0 24 5
A selectFrom() 13 19 3
A fromSingleProvider() 0 9 2
B fromAllProviders() 14 35 5
B traverse() 23 51 6

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;
2
3
use \Comodojo\Cache\Item;
4
use \Comodojo\Cache\Providers\AbstractProvider;
5
use \Comodojo\Cache\Providers\Void;
6
use \Comodojo\Cache\Interfaces\CacheItemPoolManagerInterface;
7
use \Comodojo\Cache\Interfaces\EnhancedCacheItemPoolInterface;
8
use \Comodojo\Cache\Traits\NamespaceTrait;
9
use \Comodojo\Cache\Traits\BasicCacheItemPoolTrait;
10
use \Comodojo\Cache\Traits\GenericManagerTrait;
11
use \Comodojo\Cache\Components\StackManager;
12
use \Comodojo\Foundation\Validation\DataFilter;
13
use \Psr\Cache\CacheItemInterface;
14
use \Psr\Log\LoggerInterface;
15
use \ArrayObject;
16
17
/**
18
 *
19
 * @package     Comodojo Spare Parts
20
 * @author      Marco Giovinazzi <[email protected]>
21
 * @license     MIT
22
 *
23
 * LICENSE:
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31
 * THE SOFTWARE.
32
 */
33
34
class Manager extends AbstractProvider implements CacheItemPoolManagerInterface {
35
36
    use NamespaceTrait, GenericManagerTrait {
37
        GenericManagerTrait::setNamespace insteadof NamespaceTrait;
38
    }
39
    use BasicCacheItemPoolTrait;
40
41
    const DEFAULT_PICK_MODE = 1;
42
43
    protected $pick_mode;
44
45
    protected $stack;
46
47
    protected $align_cache;
48
49
    protected $void;
50
51
    protected $selected;
52
53 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...
54
        $pick_mode = null,
55
        LoggerInterface $logger = null,
56
        $align_cache = true,
57
        $flap_interval = null
58
    ) {
59
60
        $this->pick_mode = DataFilter::filterInteger($pick_mode, 1, 6, self::DEFAULT_PICK_MODE);
61
62
        $this->align_cache = DataFilter::filterBoolean($align_cache, true);
63
64
        $stack = new ArrayObject([]);
65
66
        $this->stack = new StackManager($stack->getIterator());
67
        $this->stack->setFlapInterval($flap_interval);
68
69
        parent::__construct($logger);
70
71
        $this->void = new Void($this->logger);
72
73
        $this->logger->info("Cache manager online; pick mode ".$this->pick_mode);
74
75
    }
76
77
    public function addProvider(EnhancedCacheItemPoolInterface $provider, $weight = 0) {
78
79
        return $this->genericAddProvider($provider, $weight);
80
81
    }
82
83
    public function getItem($key) {
84
85
        return $this->selectFrom('GET', $key);
86
87
    }
88
89
    public function hasItem($key) {
90
91
        return $this->selectFrom('HAS', $key);
92
93
    }
94
95 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...
96
97
        if ( $this->align_cache === false && $this->pick_mode < 5) {
98
            return $this->selectProvider()->deleteItem($key);
99
        }
100
101
        $result = [];
102
103
        foreach ($this->stack as $provider) {
104
105
            $result[] = $provider[0]->deleteItem($key);
106
107
        }
108
109
        return !in_array(false, $result);
110
111
    }
112
113
    public function save(CacheItemInterface $item) {
114
115
        if ( $this->align_cache === false && $this->pick_mode < 5) {
116
            return $this->selectProvider()->save($item);
117
        }
118
119
        $result = [];
120
121
        foreach ($this->stack as $provider) {
122
123
            $pro = $provider[0];
124
125
            $provider_result = $pro->save($item);
126
127
            $this->logger->debug("Saving item ".$item->getKey()." into provider ".
128
                $pro->getId().": ".($provider_result ? "OK" : "NOK - ".$pro->getStateMessage()));
129
130
            $result[] = $provider_result;
131
132
        }
133
134
        return !in_array(false, $result);
135
136
    }
137
138
    protected function selectFrom($mode, $key) {
139
140 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...
141
142
            $result = $this->fromSingleProvider($mode, $key);
143
144
        } else if ( $this->pick_mode == 5 ) {
145
146
            $result = $this->fromAllProviders($mode, $key);
147
148
        } else {
149
150
            $result = $this->traverse($mode, $key);
151
152
        }
153
154
        return $result;
155
156
    }
157
158
    protected function fromSingleProvider($mode, $key) {
159
160
        $provider = $this->selectProvider();
161
162
        if ( $mode == 'HAS' ) return $provider->hasItem($key);
163
164
        return $provider->getItem($key);
165
166
    }
167
168
    protected function fromAllProviders($mode, $key) {
169
170
        $result = [];
171
172
        if ( $mode == 'GET' ) {
173
174
            foreach ($this->stack as $provider) {
175
176
                $result[] = $provider[0]->getItem($key);
177
178
                // selected provider has no sense in this case
179
                $this->selected = $provider[0];
180
181
            }
182
183
            if ( count(array_unique($result)) == 1 ) return $result[0];
184
185
            return new Item($key);
186
187 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...
188
189
            foreach ($this->stack as $provider) {
190
191
                $result[] = $provider[0]->hasItem($key);
192
193
                // selected provider has no sense in this case
194
                $this->selected = $provider[0];
195
196
            }
197
198
            return !in_array(false, $result);
199
200
        }
201
202
    }
203
204
    protected function traverse($mode, $key) {
205
206
        $this->stack->rewind();
207
208
        if ( $mode == 'GET' ) {
209
210
            foreach ($this->stack as $provider) {
211
212
                $item = $provider[0]->getItem($key);
213
214
                if ( $item->isHit() ) {
215
216
                    // selected provider has no sense in this case
217
                    $this->selected = $provider[0];
218
219
                    return $item;
220
221
                }
222
223
            }
224
225
            // selected provider has no sense in this case
226
            $this->selected = $this->void;
227
228
            return new Item($key);
229
230 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...
231
232
            foreach ($this->stack as $provider) {
233
234
                $item = $provider[0]->hasItem($key);
235
236
                if ( $item === true ) {
237
238
                    // selected provider has no sense in this case
239
                    $this->selected = $provider[0];
240
241
                    return true;
242
243
                }
244
245
            }
246
247
            // selected provider has no sense in this case
248
            $this->selected = $this->void;
249
250
            return false;
251
252
        }
253
254
    }
255
256
}
257