ThrottleMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 40
ccs 7
cts 7
cp 1
rs 10
wmc 3

2 Methods

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