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

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