Passed
Pull Request — master (#19)
by Vadim
01:52
created

LimitingFunction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
dl 0
loc 25
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fingerprint() 0 10 3
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 LimitingFunction implements LimitingPolicy
10
{
11
    /**
12
     * @psalm-var callable(ServerRequestInterface): string
13
     */
14
    private $receiver;
15
16
    /**
17
     * @psalm-param callable(ServerRequestInterface): string $receiver
18
     */
19 2
    public function __construct(callable $receiver)
20
    {
21 2
        $this->receiver = $receiver;
22 2
    }
23
24 2
    public function fingerprint(ServerRequestInterface $request): string
25
    {
26 2
        $id = ($this->receiver)($request);
27
28
        /** @psalm-suppress DocblockTypeContradiction */
29 2
        if (!is_string($id) || '' === $id) {
30 1
            throw new \InvalidArgumentException('The id must be a non-empty-string.');
31
        }
32
33 1
        return $id;
34
    }
35
}
36