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
|
12 |
|
public function __construct(RetriableThrottlerInterface $internalThrottler, TimeAdapterInterface $timeProvider) |
47
|
|
|
{ |
48
|
12 |
|
$this->internalThrottler = $internalThrottler; |
49
|
12 |
|
$this->timeProvider = $timeProvider; |
50
|
12 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
3 |
|
public function access() |
56
|
|
|
{ |
57
|
3 |
|
$status = $this->check(); |
58
|
3 |
|
$this->hit(); |
59
|
|
|
|
60
|
3 |
|
return $status; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
8 |
|
public function hit() |
67
|
|
|
{ |
68
|
8 |
|
if (0 !== $waitTime = $this->internalThrottler->getRetryTimeout()) { |
|
|
|
|
69
|
2 |
|
$this->timeProvider->usleep(1e3 * $waitTime); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
8 |
|
return $this->internalThrottler->hit(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
2 |
|
public function clear() |
79
|
|
|
{ |
80
|
2 |
|
$this->internalThrottler->clear(); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
3 |
|
public function count() |
87
|
|
|
{ |
88
|
3 |
|
return $this->internalThrottler->count(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
6 |
|
public function check() |
95
|
|
|
{ |
96
|
6 |
|
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: