AbstractFilter::envelopeAddLogEntry()   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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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