Test Failed
Pull Request — master (#19)
by Alexander
07:01
created

LimitCallback   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 7
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

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