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
|
13 |
|
public function __construct(int $intervalInSeconds) |
28
|
|
|
{ |
29
|
13 |
|
$this->_expirationIntervalSeconds = $intervalInSeconds; |
30
|
13 |
|
} |
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
|
7 |
|
public function getExpiration() : ? DateTime |
45
|
|
|
{ |
46
|
7 |
|
return $this->_expiresAt; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param DateTime $expiresAt |
51
|
|
|
* @return TimeKeeper |
52
|
|
|
*/ |
53
|
4 |
|
public function setExpiration(DateTime $expiresAt) : self |
54
|
|
|
{ |
55
|
4 |
|
$this->_expiresAt = $expiresAt; |
56
|
|
|
|
57
|
4 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return int |
62
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\TimerNotStartedException |
63
|
|
|
*/ |
64
|
6 |
|
public function getRemainingSeconds() : ? int |
65
|
|
|
{ |
66
|
6 |
|
if ($this->isExpired()) |
67
|
|
|
{ |
68
|
1 |
|
$this->reset(); |
69
|
|
|
|
70
|
1 |
|
return $this->_expirationIntervalSeconds; |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
return $this->_expiresAt === null ? null : $this->_expiresAt->getTimestamp() - \time(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
* @throws \hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\TimerNotStartedException |
79
|
|
|
*/ |
80
|
7 |
|
public function isExpired() : bool |
81
|
|
|
{ |
82
|
7 |
|
if ($this->_expiresAt === null) |
83
|
|
|
{ |
84
|
2 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
return $this->_expiresAt <= new DateTime(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Reset the request timer. |
92
|
|
|
*/ |
93
|
2 |
|
public function reset() : void |
94
|
|
|
{ |
95
|
2 |
|
$this->_expiresAt = null; |
96
|
2 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Initialize the expiration date for the request timer. |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
9 |
|
public function start() : void |
103
|
|
|
{ |
104
|
9 |
|
$this->_expiresAt = (new DateTime())->add(new DateInterval('PT' . $this->_expirationIntervalSeconds . 'S')); |
105
|
|
|
} |
106
|
|
|
} |