Completed
Push — 2.0 ( 453d0a...62378f )
by Marco
05:46
created

AbstractStackManager::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2.0185
1
<?php namespace Comodojo\Cache\Components;
2
3
use \Comodojo\Cache\Traits\FlapIntervalTrait;
4
use \Exception;
5
use \FilterIterator;
6
7
/**
8
 *
9
 * @package     Comodojo Spare Parts
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
class AbstractStackManager extends FilterIterator {
25
26
    use FlapIntervalTrait;
27
28 70
    public function accept() {
29
30 70
        $provider = $this->getInnerIterator()->current();
31 70
        $provider = $provider[0];
32
33 70
        $status = $provider->getState();
34
35
        if (
36 70
            $status === $provider::CACHE_ERROR &&
37 70
            date_create('now')->diff($provider->getStateTime())->format('%s') > $this->getFlapInterval()
38
        ) {
39
40 2
            return $provider->test();
41
42
        }
43
44 70
        return $status == $provider::CACHE_SUCCESS ? true : false;
45
46
    }
47
48 75
    public function genericAdd($provider, $weight) {
49
50 75
        $pools = $this->getInnerIterator();
51
52 75
        $id = $provider->getId();
53
54 75
        $pools[$id] = [$provider, $weight];
55
56 75
    }
57
58 2 View Code Duplication
    public function remove($id) {
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...
59
60 2
        $pools = $this->getInnerIterator();
61
62 2
        if ( isset($pools[$id]) ) {
63 2
            unset($pools[$id]);
64 2
            return true;
65
        }
66
67
        throw new Exception("Provider $id not registered into the stack");
68
69
    }
70
71 2 View Code Duplication
    public function get($id) {
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...
72
73 2
        $pools = $this->getInnerIterator();
74
75 2
        if ( isset($pools[$id]) ) return $pools[$id][0];
76
77
        throw new Exception("Provider $id not registered into the stack");
78
79
    }
80
81 25
    public function getAll($enabled = false) {
82
83 25
        $result = [];
84
85 25
        if ( $enabled === true ) {
86
87 15
            foreach($this as $id => $provider) $result[$id] = $provider[0];
88
89
        } else {
90
91 10
            foreach($this->getInnerIterator() as $id => $provider) $result[$id] = $provider[0];
92
93
        }
94
95 25
        return $result;
96
97
    }
98
99
    public function getCurrent() {
100
101
        $current = $this->current();
102
        return $current[0];
103
104
    }
105
106
    public function has($id) {
107
108
        $pools = $this->getInnerIterator();
109
110
        return isset($pools[$id]);
111
112
    }
113
114 4
    public function getRandomProvider() {
115
116 4
        $stack = $this->getAll(true);
117
118 4
        $rand = array_rand($stack);
119
120 4
        return $rand === null ? null : $stack[$rand];
121
122
    }
123
124 48
    public function getFirstProvider() {
125
126 48
        $this->rewind();
127
128 48
        $current = $this->current();
129
130 48
        return $current === null ? null : $current[0];
131
132
        // $providers = $this->getAll();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133
134
        // return current($providers);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
135
136
    }
137
138 11
    public function getLastProvider() {
139
140 11
        $this->rewind();
141
142 11
        $providers = $this->getAll(true);
143
144 11
        $provider = end($providers);
145
146 11
        return $provider;
147
148
    }
149
150
    public function getHeavyProvider() {
151
152
        $providers = $this->getAll(true);
153
154
        if ( count($providers) === 0 ) return null;
155
156
        $weights = $this->getWeights();
157
158
        asort($weights);
159
160
        end($weights);
161
162
        return $providers[key($weights)];
163
164
    }
165
166
    private function getWeights() {
167
168
        $result = [];
169
170
        foreach($this as $id => $provider) $result[$id] = $provider[1];
171
172
        return $result;
173
174
    }
175
176
}
177