Passed
Push — main ( f1c997...ae6b2e )
by Daniel
13:01
created

InvalidHeaderFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 16 2
A getSelector() 0 3 1
A getValidator() 0 3 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