AbstractFilter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 44
rs 10
c 0
b 0
f 0
ccs 12
cts 12
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A envelopeAddLogEntry() 0 3 1
A getOptions() 0 3 1
A __construct() 0 8 1
A setOptions() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DanBettles\Defence\Filter;
6
7
use Psr\Log\LogLevel;
8
use DanBettles\Defence\Envelope;
9
10
use function array_replace;
11
12
/**
13
 * The default log-level of a filter extending this class is `LogLevel::WARNING`.  That value can be overridden by
14
 * setting the `log_level` option.  The log-level is used by the add-log-entry convenience method.
15
 *
16
 * @phpstan-type IncomingFilterOptions array<string,string|null>
17
 * @phpstan-type BasicFilterOptions array{log_level:string}
18
 */
19
abstract class AbstractFilter implements FilterInterface
20
{
21
    /**
22
     * @phpstan-var BasicFilterOptions
23
     */
24
    private $options;
25
26
    /**
27
     * @phpstan-param IncomingFilterOptions $options
28
     */
29 692
    public function __construct(array $options = [])
30
    {
31
        /** @phpstan-var BasicFilterOptions */
32 692
        $completeOptions = array_replace([
33 692
            'log_level' => LogLevel::WARNING,
34 692
        ], $options);
35
36 692
        $this->setOptions($completeOptions);
37
    }
38
39
    /**
40
     * @phpstan-param BasicFilterOptions $options
41
     */
42 692
    private function setOptions(array $options): self
43
    {
44 692
        $this->options = $options;
45
46 692
        return $this;
47
    }
48
49
    /**
50
     * @phpstan-return BasicFilterOptions
51
     */
52 329
    public function getOptions(): array
53
    {
54 329
        return $this->options;
55
    }
56
57
    /**
58
     * Adds a log entry to the log accessed via the envelope.
59
     */
60 219
    protected function envelopeAddLogEntry(Envelope $envelope, string $message): void
61
    {
62 219
        $envelope->addLogEntry($this->getOptions()['log_level'], $message);
63
    }
64
}
65