-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
ThrottleMiddleware.php
67 lines (59 loc) · 1.63 KB
/
ThrottleMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
/*
* This file is part of Laravel Throttle.
*
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace GrahamCampbell\Throttle\Http\Middleware;
use Closure;
use GrahamCampbell\Throttle\Throttle;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
/**
* This is the throttle middleware class.
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*/
class ThrottleMiddleware
{
/**
* The throttle instance.
*
* @var \GrahamCampbell\Throttle\Throttle
*/
protected Throttle $throttle;
/**
* Create a new throttle middleware instance.
*
* @param \GrahamCampbell\Throttle\Throttle $throttle
*
* @return void
*/
public function __construct(Throttle $throttle)
{
$this->throttle = $throttle;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param int|string $limit
* @param int|string $time
*
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
*
* @return mixed
*/
public function handle(Request $request, Closure $next, $limit = 10, $time = 60)
{
if (!$this->throttle->attempt($request, (int) $limit, (int) $time)) {
throw new TooManyRequestsHttpException($time * 60, 'Rate limit exceeded.');
}
return $next($request);
}
}