InvalidHeaderFilter::getValidator()   A
last analyzed

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 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DanBettles\Defence\Filter;
6
7
use Closure;
8
use DanBettles\Defence\Envelope;
9
10
use const false;
11
use const true;
12
13
/**
14
 * @phpstan-import-type IncomingFilterOptions from AbstractFilter
15
 */
16
class InvalidHeaderFilter extends AbstractFilter
17
{
18
    /**
19
     * @phpstan-param IncomingFilterOptions $options
20
     */
21 20
    public function __construct(
22
        private string $selector,
23
        private Closure $validator,
24
        array $options = []
25
    ) {
26 20
        parent::__construct($options);
27
    }
28
29 15
    public function __invoke(Envelope $envelope): bool
30
    {
31 15
        $request = $envelope->getRequest();
32 15
        $headerValue = $request->headers->get($this->getSelector());
33 15
        $headerValueIsValid = ($this->getValidator())($headerValue);
34
35 15
        if (!$headerValueIsValid) {
36 10
            $this->envelopeAddLogEntry(
37 10
                $envelope,
38 10
                "The value of header `{$this->getSelector()}`, `{$headerValue}`, is invalid"
39 10
            );
40
41 10
            return true;
42
        }
43
44 5
        return false;
45
    }
46
47 17
    public function getSelector(): string
48
    {
49 17
        return $this->selector;
50
    }
51
52 17
    public function getValidator(): Closure
53
    {
54 17
        return $this->validator;
55
    }
56
}
57