1 | <?php |
||
11 | class ThrottleRequests |
||
12 | { |
||
13 | use CreatesHttpResponses; |
||
14 | |||
15 | /** |
||
16 | * The rate limiter instance. |
||
17 | * |
||
18 | * @var RateLimiter |
||
19 | */ |
||
20 | private $rateLimiter; |
||
21 | |||
22 | /** |
||
23 | * Create a new request throttler. |
||
24 | * |
||
25 | * @param RateLimiter $rateLimiter |
||
26 | */ |
||
27 | public function __construct(RateLimiter $rateLimiter) |
||
31 | |||
32 | /** |
||
33 | * Handle an incoming request. |
||
34 | * |
||
35 | * @param Request $request |
||
36 | * @param Closure $next |
||
37 | * @param int $maxAttempts |
||
38 | * @param int $decayMinutes |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | public function handle(Request $request, Closure $next, $maxAttempts = 60, $decayMinutes = 1) |
||
59 | |||
60 | /** |
||
61 | * Resolve request signature. |
||
62 | * |
||
63 | * @param Request $request |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | protected function resolveRequestSignature($request) |
||
76 | |||
77 | /** |
||
78 | * Create a 'too many attempts' response. |
||
79 | * |
||
80 | * @param string $key |
||
81 | * @param int $maxAttempts |
||
82 | * |
||
83 | * @return Response |
||
84 | */ |
||
85 | protected function buildResponse($key, $maxAttempts) |
||
97 | |||
98 | /** |
||
99 | * Add the limit header information to the given response. |
||
100 | * |
||
101 | * @param Response $response |
||
102 | * @param int $maxAttempts |
||
103 | * @param int $remainingAttempts |
||
104 | * @param int|null $retryAfter |
||
105 | * |
||
106 | * @return Response |
||
107 | */ |
||
108 | protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null) |
||
123 | |||
124 | /** |
||
125 | * Calculate the number of remaining attempts. |
||
126 | * |
||
127 | * @param string $key |
||
128 | * @param int $maxAttempts |
||
129 | * @param int|null $retryAfter |
||
130 | * |
||
131 | * @return int |
||
132 | */ |
||
133 | protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null) |
||
141 | } |
||
142 |