Reactor::getVerifiableParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ionut\Sylar;
4
5
6
use Ionut\Sylar\Filters\FilterReportInterface;
7
use Ionut\Sylar\Normalizers\NormalizerInterface;
8
use Ionut\Sylar\Normalizers\PHPIDSConverter;
9
use Ionut\Sylar\Reactions\PsrLogger;
10
use Ionut\Sylar\Reactions\ReactionInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Log\LoggerInterface;
13
14
class Reactor
15
{
16
    /**
17
     * @var Filters
18
     */
19
    protected $filters;
20
21
    /**
22
     * @var ReactionInterface[]
23
     */
24
    protected $reactions;
25
26
    /**
27
     * @var NormalizerInterface[]
28
     */
29
    protected $normalizers;
30
31
    /**
32
     * @param  Filters                $filters
33
     * @param  ReactionInterface[]    $reactions
34
     * @param  NormalizerInterface[]  $normalizers
35
     */
36
    public function __construct(Filters $filters, array $reactions = [], array $normalizers)
37
    {
38
        $this->filters = $filters;
39
        $this->reactions = $reactions;
40
        $this->normalizers = $normalizers;
41
    }
42
43
    /**
44
     * Build an instance of the Handler by using the most common defaults.
45
     *
46
     * @param  LoggerInterface  $logger
47
     * @return static
48
     */
49
    static public function factory(LoggerInterface $logger, $threshold = 0)
50
    {
51
        return new static(
52
            new Filters([
53
                new \Ionut\Sylar\Filters\PHPIDS\Filter
54
            ]),
55
            [
56
                new PsrLogger($logger, $threshold)
57
            ],
58
            [
59
                new PHPIDSConverter()
60
            ]
61
        );
62
    }
63
64
    /**
65
     * Check a given request against the defined filters.
66
     *
67
     * @param ServerRequestInterface $request
68
     */
69
    public function digest(ServerRequestInterface $request)
70
    {
71
        $parameters = $this->normalize($this->getVerifiableParameters($request));
72
73
        array_walk_recursive($parameters, function (NormalizedValue $value) use($request) {
74
            if ($filterReports = $this->filters->matches($value)) {
75
                $this->broadcast(new Report(iterator_to_array($filterReports), $request));
76
            }
77
        });
78
    }
79
80
    /**
81
     * @param   array  $parameters
82
     * @return  array
83
     */
84
    public function normalize(array $parameters)
85
    {
86
        foreach ($this->normalizers as $normalizer) {
87
            $parameters = $normalizer->normalize($parameters);
88
        }
89
90
        return $parameters;
91
    }
92
93
    /**
94
     * @param Report $report
95
     */
96
    public function broadcast(Report $report)
97
    {
98
        foreach ($this->reactions as $reaction) {
99
            if ($reaction->shouldReact($report)) {
100
                $reaction->reactTo($report);
101
            }
102
        }
103
    }
104
105
    /**
106
     * Get the values that should be checked for security intrusions.
107
     *
108
     * @param  ServerRequestInterface  $request
109
     * @return array
110
     */
111
    protected function getVerifiableParameters(ServerRequestInterface $request)
112
    {
113
        return [$request->getServerParams(), $request->getBody()->__toString()];
114
    }
115
}