|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2015 Krishnaprasad MG <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace Sunspikes\Ratelimit\Throttle\Throttler; |
|
27
|
|
|
|
|
28
|
|
|
use Sunspikes\Ratelimit\Time\TimeAdapterInterface; |
|
29
|
|
|
|
|
30
|
|
|
final class RetrialQueueThrottler implements ThrottlerInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var ThrottlerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $internalThrottler; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var TimeAdapterInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $timeProvider; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param RetriableThrottlerInterface $internalThrottler |
|
44
|
|
|
* @param TimeAdapterInterface $timeProvider |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public function __construct(RetriableThrottlerInterface $internalThrottler, TimeAdapterInterface $timeProvider) |
|
47
|
|
|
{ |
|
48
|
6 |
|
$this->internalThrottler = $internalThrottler; |
|
49
|
6 |
|
$this->timeProvider = $timeProvider; |
|
50
|
6 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function access() |
|
56
|
|
|
{ |
|
57
|
2 |
|
$status = $this->check(); |
|
58
|
2 |
|
$this->hit(); |
|
59
|
|
|
|
|
60
|
2 |
|
return $status; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
5 |
|
public function hit() |
|
67
|
|
|
{ |
|
68
|
5 |
|
if (0 !== $waitTime = $this->internalThrottler->getRetryTimeout()) { |
|
|
|
|
|
|
69
|
1 |
|
$this->timeProvider->usleep(self::MILLISECOND_TO_MICROSECOND_MULTIPLIER * $waitTime); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
return $this->internalThrottler->hit(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function clear() |
|
79
|
|
|
{ |
|
80
|
1 |
|
$this->internalThrottler->clear(); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritdoc |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function count() |
|
87
|
|
|
{ |
|
88
|
2 |
|
return $this->internalThrottler->count(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @inheritdoc |
|
93
|
|
|
*/ |
|
94
|
4 |
|
public function check() |
|
95
|
|
|
{ |
|
96
|
4 |
|
return $this->internalThrottler->check(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritdoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getTime() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->internalThrottler->getTime(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @inheritdoc |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getLimit() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->internalThrottler->getLimit(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: