1 | <?php |
||
13 | class RateLimiter |
||
14 | { |
||
15 | /** |
||
16 | * @var RateLimitProvider |
||
17 | */ |
||
18 | protected $provider; |
||
19 | |||
20 | /** |
||
21 | * @var LoggerInterface |
||
22 | */ |
||
23 | protected $logger; |
||
24 | |||
25 | /** |
||
26 | * @var string|callable Constant or callable that accepts a Response. |
||
27 | */ |
||
28 | protected $logLevel; |
||
29 | |||
30 | /** |
||
31 | * Creates a callable middleware rate limiter. |
||
32 | * |
||
33 | * @param RateLimitProvider $provider A rate data provider. |
||
34 | * @param LoggerInterface $logger |
||
35 | */ |
||
36 | 7 | public function __construct( |
|
43 | |||
44 | /** |
||
45 | * Delays and logs the request then sets the allowance for the next request. |
||
46 | * |
||
47 | * @param callable $handler |
||
48 | * @return \Closure |
||
49 | */ |
||
50 | 3 | public function __invoke(callable $handler) |
|
70 | |||
71 | /** |
||
72 | * Logs a request which is being delayed by a specified amount of time. |
||
73 | * |
||
74 | * @param RequestInterface $request The request being delayed. |
||
75 | * @param float $delay The amount of time that the request is delayed for. |
||
|
|||
76 | */ |
||
77 | 3 | protected function log(RequestInterface $request, $delay) |
|
87 | |||
88 | /** |
||
89 | * Formats a request and delay time as a log message. |
||
90 | * |
||
91 | * @param RequestInterface $request The request being logged. |
||
92 | * @param float $delay The amount of time that the request is delayed for. |
||
93 | * |
||
94 | * @return string Log message |
||
95 | */ |
||
96 | 3 | protected function getLogMessage(RequestInterface $request, $delay) |
|
104 | |||
105 | /** |
||
106 | * Returns the default log level. |
||
107 | * |
||
108 | * @return string LogLevel |
||
109 | */ |
||
110 | 1 | protected function getDefaultLogLevel() |
|
114 | |||
115 | /** |
||
116 | * Sets the log level to use, which can be either a string or a callable |
||
117 | * that accepts a response (which could be null). A log level could also |
||
118 | * be null, which indicates that the default log level should be used. |
||
119 | * |
||
120 | * @param string|callable|null |
||
121 | */ |
||
122 | 2 | public function setLogLevel($logLevel) |
|
126 | |||
127 | /** |
||
128 | * Returns a log level for a given request. |
||
129 | * |
||
130 | * @param RequestInterface $request The request being logged. |
||
131 | * @return string LogLevel |
||
132 | */ |
||
133 | 3 | protected function getLogLevel(RequestInterface $request) |
|
145 | |||
146 | /** |
||
147 | * Returns the delay duration for the given request (in seconds). |
||
148 | * |
||
149 | * @param RequestInterface $request Request to get the delay duration for. |
||
150 | * |
||
151 | * @return float The delay duration (in seconds). |
||
152 | */ |
||
153 | 4 | protected function getDelay(RequestInterface $request) |
|
162 | |||
163 | /** |
||
164 | * Delays the given request by an amount of seconds. This method supports microsecond |
||
165 | * precision as well as integer seconds, ie. both microtime(true) or time() |
||
166 | * |
||
167 | * @param float $seconds The amount of time (in seconds) to delay by. |
||
168 | * |
||
169 | * @codeCoverageIgnore |
||
170 | */ |
||
171 | protected function delay($seconds) |
||
179 | |||
180 | /** |
||
181 | * Returns a callable handler which allows the provider to set the request |
||
182 | * allowance for the next request, using the current response. |
||
183 | * |
||
184 | * @return \Closure Handler to set request allowance on the rate provider. |
||
185 | */ |
||
186 | protected function setAllowance() |
||
193 | } |
||
194 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.