Completed
Push — 2.0 ( 07e083...8b0753 )
by Marco
03:10 queued 01:10
created

Manager::selectFrom()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 8

Duplication

Lines 13
Ratio 68.42 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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