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

LimitingFunction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 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 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