Completed
Push — master ( e0507b...08360b )
by Timo
02:21
created

RequestLimitGroup::getRequestLimiterCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle;
4
5
use SplObjectStorage;
6
7
/**
8
 * Class RequestLimitGroup
9
 * @package hamburgscleanest\GuzzleAdvancedThrottle
10
 */
11
class RequestLimitGroup
12
{
13
14
    /** @var SplObjectStorage */
15
    private $_requestLimiters;
16
17
    /** @var int */
18
    private $_retryAfter = 0;
19
20
    /**
21
     * RequestLimitGroup constructor.
22
     */
23 6
    public function __construct()
24
    {
25 6
        $this->_requestLimiters = new SplObjectStorage();
26 6
    }
27
28
    /**
29
     * @return RequestLimitGroup
30
     */
31 5
    public static function create() : self
32
    {
33 5
        return new static();
34
    }
35
36
    /**
37
     * @return int
38
     */
39 2
    public function getRetryAfter() : int
40
    {
41 2
        return $this->_retryAfter;
42
    }
43
44
    /**
45
     * We have to cycle through all the limiters (no early return).
46
     * The timers of each limiter have to be updated despite of another limiter already preventing the request.
47
     *
48
     * @return bool
49
     * @throws \Exception
50
     */
51 3
    public function canRequest() : bool
52
    {
53 3
        $groupCanRequest = true;
54 3
        $this->_requestLimiters->rewind();
55 3
        while ($this->_requestLimiters->valid())
56
        {
57
            /** @var RequestLimiter $requestLimiter */
58 3
            $requestLimiter = $this->_requestLimiters->current();
59
60 3
            $canRequest = $requestLimiter->canRequest();
61 3
            if ($groupCanRequest && !$canRequest)
62
            {
63 3
                $groupCanRequest = false;
64 3
                $this->_retryAfter = $requestLimiter->getRemainingSeconds();
65
            }
66
67 3
            $this->_requestLimiters->next();
68
        }
69
70 3
        return $groupCanRequest;
71
    }
72
73
    /**
74
     * @param RequestLimiter $requestLimiter
75
     * @return RequestLimitGroup
76
     */
77 5
    public function addRequestLimiter(RequestLimiter $requestLimiter) : self
78
    {
79 5
        $this->_requestLimiters->attach($requestLimiter);
80
81 5
        return $this;
82
    }
83
84
    /**
85
     * @param RequestLimiter $requestLimiter
86
     * @return RequestLimitGroup
87
     */
88 1
    public function removeRequestLimiter(RequestLimiter $requestLimiter) : self
89
    {
90 1
        $this->_requestLimiters->detach($requestLimiter);
91
92 1
        return $this;
93
    }
94
95
    /**
96
     * @return int
97
     */
98 2
    public function getRequestLimiterCount() : int
99
    {
100 2
        return $this->_requestLimiters->count();
101
    }
102
}