Test Setup Failed
Push — master ( 8c5f4b...dcf915 )
by Alexpts
03:11
created

RateLimitMiddleware   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 66
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\RateLimiter;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
class RateLimitMiddleware implements MiddlewareInterface
12
{
13
    protected Limiter $limiter;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
14
    protected int $ttl = 60; // sec
15
    protected int $max = 60; // sec
16
    protected string $keyPrefix = 'limiter';
17
    protected ResponseInterface $responseTooManyRequest;
18
    protected string $keyAttr = 'client-ip';
19
20
    public function __construct(Limiter $limiter, ResponseInterface $response)
21
    {
22
        $this->limiter = $limiter;
23
        $this->responseTooManyRequest = $response->withStatus(429, 'Too Many Requests');
24
    }
25
26 4
    public function setKeyAttr(string $attr): void
27
    {
28 4
        $this->keyAttr = $attr;
29 4
    }
30 4
31
    public function setKeyPrefix(string $keyPrefix): void
32 3
    {
33
        $this->keyPrefix = $keyPrefix;
34 3
    }
35 3
36
    public function setMax(int $max): void
37 1
    {
38
        $this->max = $max;
39 1
    }
40 1
41
    public function setTtl(int $ttl): void
42 1
    {
43
        $this->ttl = $ttl;
44 1
    }
45 1
46
    public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
47 1
    {
48
        $limiterKey = $this->getKey($request);
49 1
50 1
        if ($this->limiter->isExceeded($limiterKey, $this->max)) {
51
            return $this->responseTooManyRequest;
52 4
        }
53
54 4
        $response = $next->handle($request);
55
        $this->limiter->inc($limiterKey, $this->ttl);
56 4
57 1
        return $response;
58
    }
59
60 4
    protected function getKey(ServerRequestInterface $request): string
61 4
    {
62
        $key = $request->getAttribute($this->keyAttr);
63 4
        return sprintf('%s.%s', $this->keyPrefix, $key);
64
    }
65
}
66