AbstractStackManager::getRandomProvider()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php namespace Comodojo\Cache\Components;
2
3
use \Comodojo\Cache\Traits\FlapIntervalTrait;
4
use \Exception;
5
use \FilterIterator;
6
7
/**
8
 * @package     Comodojo Cache
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     MIT
11
 *
12
 * LICENSE:
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
23
class AbstractStackManager extends FilterIterator {
24
25
    use FlapIntervalTrait;
26
27 74
    public function accept() {
28
29 74
        $provider = $this->getInnerIterator()->current();
30 74
        $provider = $provider[0];
31
32 74
        $status = $provider->getState();
33
34
        if (
35 74
            $status === $provider::CACHE_ERROR &&
36 74
            date_create('now')->diff($provider->getStateTime())->format('%s') > $this->getFlapInterval()
37
        ) {
38
39 2
            return $provider->test();
40
41
        }
42
43 74
        return $status == $provider::CACHE_SUCCESS ? true : false;
44
45
    }
46
47 79
    public function genericAdd($provider, $weight) {
48
49 79
        $pools = $this->getInnerIterator();
50
51 79
        $id = $provider->getId();
52
53 79
        $pools[$id] = [$provider, $weight];
54
55 79
    }
56
57 2
    public function remove($id) {
58
59 2
        $pools = $this->getInnerIterator();
60
61 2
        if ( isset($pools[$id]) ) {
62 2
            unset($pools[$id]);
63 2
            return true;
64
        }
65
66
        throw new Exception("Provider $id not registered into the stack");
67
68
    }
69
70 2
    public function get($id) {
71
72 2
        $pools = $this->getInnerIterator();
73
74 2
        if ( isset($pools[$id]) ) return $pools[$id][0];
75
76
        throw new Exception("Provider $id not registered into the stack");
77
78
    }
79
80 34
    public function getAll($enabled = false) {
81
82 34
        $result = [];
83
84 34
        if ( $enabled === true ) {
85
86 15
            foreach ( $this as $id => $provider ) $result[$id] = $provider[0];
87
88
        } else {
89
90 19
            foreach ( $this->getInnerIterator() as $id => $provider ) $result[$id] = $provider[0];
91
92
        }
93
94 34
        return $result;
95
96
    }
97
98
    public function getCurrent() {
99
100
        $current = $this->current();
101
        return $current[0];
102
103
    }
104
105
    public function has($id) {
106
107
        $pools = $this->getInnerIterator();
108
109
        return isset($pools[$id]);
110
111
    }
112
113 4
    public function getRandomProvider() {
114
115 4
        $stack = $this->getAll(true);
116
117 4
        $rand = array_rand($stack);
118
119 4
        return $rand === null ? null : $stack[$rand];
120
121
    }
122
123 51
    public function getFirstProvider() {
124
125 51
        $this->rewind();
126
127 51
        $current = $this->current();
128
129 51
        return $current === null ? null : $current[0];
130
131
        // $providers = $this->getAll();
132
133
        // return current($providers);
134
135
    }
136
137 11
    public function getLastProvider() {
138
139 11
        $this->rewind();
140
141 11
        $providers = $this->getAll(true);
142
143 11
        $provider = end($providers);
144
145 11
        return $provider;
146
147
    }
148
149
    public function getHeavyProvider() {
150
151
        $providers = $this->getAll(true);
152
153
        if ( count($providers) === 0 ) return null;
154
155
        $weights = $this->getWeights();
156
157
        asort($weights);
158
159
        end($weights);
160
161
        return $providers[key($weights)];
162
163
    }
164
165
    private function getWeights() {
166
167
        $result = [];
168
169
        foreach ( $this as $id => $provider ) $result[$id] = $provider[1];
170
171
        return $result;
172
173
    }
174
175
}
176