Defence::setFilterChain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DanBettles\Defence;
6
7
use DanBettles\Gestalt\SimpleFilterChain;
8
use DanBettles\Defence\Handler\HandlerInterface;
9
10
use const true;
11
12
class Defence
13
{
14
    /** @var SimpleFilterChain */
15
    private $filterChain;
16
17
    /** @var HandlerInterface */
18
    private $handler;
19
20 6
    public function __construct(SimpleFilterChain $filterChain, HandlerInterface $handler)
21
    {
22 6
        $this
23 6
            ->setFilterChain($filterChain)
24 6
            ->setHandler($handler)
25 6
        ;
26
    }
27
28 6
    private function setFilterChain(SimpleFilterChain $filterChain): self
29
    {
30 6
        $this->filterChain = $filterChain;
31
32 6
        return $this;
33
    }
34
35 6
    public function getFilterChain(): SimpleFilterChain
36
    {
37 6
        return $this->filterChain;
38
    }
39
40 6
    private function setHandler(HandlerInterface $handler): self
41
    {
42 6
        $this->handler = $handler;
43
44 6
        return $this;
45
    }
46
47 5
    public function getHandler(): HandlerInterface
48
    {
49 5
        return $this->handler;
50
    }
51
52
    /**
53
     * Executes the filter chain and, if the request was deemed suspicious, triggers the handler.
54
     */
55 3
    public function execute(Envelope $envelope): void
56
    {
57 3
        $requestIsSuspicious = $this->getFilterChain()->execute($envelope, true);
58
59 3
        if ($requestIsSuspicious) {
60 2
            $this->getHandler()($envelope);
61
        }
62
    }
63
}
64