GenericManagerTrait::selectProvider()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.2373

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
ccs 13
cts 16
cp 0.8125
rs 9.2222
c 0
b 0
f 0
cc 6
nc 10
nop 0
crap 6.2373
1
<?php namespace Comodojo\Cache\Traits;
2
3
/**
4
 * @package     Comodojo Cache
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @license     MIT
7
 *
8
 * LICENSE:
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16
 * THE SOFTWARE.
17
 */
18
19
trait GenericManagerTrait {
20
21 79
    public function genericAddProvider($provider, $weight = 0) {
22
23 79
        $this->stack->add($provider, $weight);
24
25 79
        return $this;
26
27
    }
28
29 2
    public function removeProvider($id) {
30
31 2
        return $this->stack->remove($id);
32
33
    }
34
35 2
    public function getProvider($id) {
36
37 2
        return $this->stack->get($id);
38
39
    }
40
41 2
    public function getProviders($enabled = false) {
42
43 2
        return $this->stack->getAll($enabled);
44
45
    }
46
47 29
    public function getSelectedProvider() {
48
49 29
        return $this->selected === null ? $this->vacuum : $this->selected;
50
51
    }
52
53 11
    public function clear() {
54
55 11
        $result = [];
56
57 11
        foreach ( $this->stack->getAll() as $provider ) {
58
59 9
            $result[] = $provider->clear();
60
61
        }
62
63 11
        return !in_array(false, $result);
64
65
    }
66
67 6
    public function setNamespace($namespace = null) {
68
69 6
        foreach ( $this->stack->getAll(false) as $provider ) {
70 4
            $provider->setNamespace($namespace);
71
        }
72
73 6
        $this->namespace = empty($namespace) ? "GLOBAL" : $namespace;
0 ignored issues
show
Bug Best Practice introduced by
The property namespace does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
75 6
        return $this;
76
77
    }
78
79 4
    public function clearNamespace() {
80
81 4
        $result = [];
82
83 4
        foreach ( $this->stack->getAll() as $provider ) {
84 2
            $result[] = $provider->clearNamespace();
85
        }
86
87 4
        return !in_array(false, $result);
88
89
    }
90
91 2
    public function getStats() {
92
93 2
        $stats = [];
94
95 2
        foreach ( $this->stack->getAll(false) as $provider ) {
96 2
            $stats[] = $provider->getStats();
97
        }
98
99 2
        return $stats;
100
101
    }
102
103 66
    protected function selectProvider() {
104
105 66
        switch ( $this->pick_mode ) {
106
107 66
            case 1:
108 51
                $provider = $this->stack->getFirstProvider();
109 51
                break;
110 15
            case 2:
111 11
                $provider = $this->stack->getLastProvider();
112 11
                break;
113 4
            case 3:
114 4
                $provider = $this->stack->getRandomProvider();
115 4
                break;
116
            case 4:
117
                $provider = $this->stack->getHeavyProvider();
118
                break;
119
120
        }
121
122 66
        $this->selected = $provider == null ? $this->vacuum : $provider;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $provider does not seem to be defined for all execution paths leading up to this point.
Loading history...
Bug Best Practice introduced by
The property selected does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
123
124 66
        return $this->selected;
125
126
    }
127
128
}
129