Passed
Pull Request — master (#25)
by Wilmer
15:03
created

LimitPerIp   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 5
c 1
b 0
f 0
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getIp() 0 6 1
A fingerprint() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\RateLimiter\Policy;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
9
final class LimitPerIp implements LimitPolicyInterface
10
{
11 3
    public function fingerprint(ServerRequestInterface $request): string
12
    {
13 3
        return sha1(
14 3
            mb_strtolower($request->getMethod() . '-' . $request->getUri()->getPath() . '-' . $this->getIp($request))
15
        );
16
    }
17
18 3
    private function getIp(ServerRequestInterface $request): string
19
    {
20
        /** @psalm-var array{REMOTE_ADDR?: string} $server */
21 3
        $server = $request->getServerParams();
22
23 3
        return trim($server['REMOTE_ADDR'] ?? '', '[]');
24
    }
25
}
26