|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace hamburgscleanest\GuzzleAdvancedThrottle; |
|
5
|
|
|
|
|
6
|
|
|
use DateInterval; |
|
7
|
|
|
use DateTime; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class TimeKeeper |
|
12
|
|
|
* @package hamburgscleanest\GuzzleAdvancedThrottle |
|
13
|
|
|
*/ |
|
14
|
|
|
class TimeKeeper |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** @var int */ |
|
18
|
|
|
private $_expirationIntervalSeconds; |
|
19
|
|
|
|
|
20
|
|
|
/** @var DateTime */ |
|
21
|
|
|
private $_expiresAt; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* TimeKeeper constructor. |
|
25
|
|
|
* @param int $intervalInSeconds |
|
26
|
|
|
*/ |
|
27
|
11 |
|
public function __construct(int $intervalInSeconds) |
|
28
|
|
|
{ |
|
29
|
11 |
|
$this->_expirationIntervalSeconds = $intervalInSeconds; |
|
30
|
11 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param int $intervalInSeconds |
|
34
|
|
|
* @return TimeKeeper |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public static function create(int $intervalInSeconds) : self |
|
37
|
|
|
{ |
|
38
|
1 |
|
return new static($intervalInSeconds); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return DateTime |
|
43
|
|
|
*/ |
|
44
|
6 |
|
public function getExpiration() : ? DateTime |
|
45
|
|
|
{ |
|
46
|
6 |
|
return $this->_expiresAt; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param DateTime $expiresAt |
|
51
|
|
|
* @return TimeKeeper |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function setExpiration(DateTime $expiresAt) : self |
|
54
|
|
|
{ |
|
55
|
2 |
|
$this->_expiresAt = $expiresAt; |
|
56
|
|
|
|
|
57
|
2 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return int |
|
62
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\TimerNotStartedException |
|
63
|
|
|
*/ |
|
64
|
4 |
|
public function getRemainingSeconds() : ? int |
|
65
|
|
|
{ |
|
66
|
4 |
|
if ($this->isExpired()) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->reset(); |
|
69
|
|
|
|
|
70
|
|
|
return $this->_expirationIntervalSeconds; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
4 |
|
return $this->_expiresAt === null ? null : $this->_expiresAt->getTimestamp() - \time(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return bool |
|
78
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\TimerNotStartedException |
|
79
|
|
|
*/ |
|
80
|
5 |
|
public function isExpired() : bool |
|
81
|
|
|
{ |
|
82
|
5 |
|
if ($this->_expiresAt === null) |
|
83
|
|
|
{ |
|
84
|
2 |
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
5 |
|
return $this->_expiresAt <= new DateTime(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Reset the request timer. |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function reset() : void |
|
94
|
|
|
{ |
|
95
|
1 |
|
$this->_expiresAt = null; |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Initialize the expiration date for the request timer. |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
7 |
|
public function start() : void |
|
103
|
|
|
{ |
|
104
|
7 |
|
$this->_expiresAt = (new DateTime())->add(new DateInterval('PT' . $this->_expirationIntervalSeconds . 'S')); |
|
105
|
|
|
} |
|
106
|
|
|
} |