Completed
Push — master ( a1d3b0...f47b8c )
by Nikola
02:43
created

HandlerConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MonologFactory\Config;
6
7
use Monolog\Formatter\FormatterInterface;
8
9
class HandlerConfig extends ParametrizedObjectConfig
10
{
11
    public const PROCESSORS = 'processors';
12
    public const FORMATTER = 'formatter';
13
14
    /** @var callable[]|ProcessorConfig[] */
15
    protected $processors;
16
17
    /** @var FormatterInterface|FormatterConfig|null */
18
    protected $formatter;
19
20 9
    protected function __construct(
21
        string $name,
22
        array $parameters,
23
        array $processors,
24
        $formatter = null
25
    ) {
26 9
        parent::__construct($name, $parameters);
27 9
        $this->processors = $processors;
28 9
        $this->formatter = $formatter;
29 9
    }
30
31 10
    public static function fromArray(array $config)
32
    {
33 10
        $config = self::filter($config);
34
35 9
        return new static(
36 9
            $config[self::NAME],
37 9
            $config[self::PARAMETERS],
38
            array_map(function ($processor) {
39
                return is_array($processor) ? ProcessorConfig::fromArray($processor) : $processor;
40 9
            }, $config[self::PROCESSORS]),
41 9
            is_array($config[self::FORMATTER])
42 2
                ? FormatterConfig::fromArray($config[self::FORMATTER])
43 9
                : $config[self::FORMATTER]
44
        );
45
    }
46
47 10
    protected static function defaults(): array
48
    {
49 10
        return array_merge(parent::defaults(), [
50 10
            self::PROCESSORS => [],
51 10
            self::FORMATTER => null,
52
        ]);
53
    }
54
55 10
    protected static function validate(array $config): void
56
    {
57 10
        parent::validate($config);
58 10
        ConfigAssertion::isArray($config[self::PROCESSORS], "'" . self::PROCESSORS . "' must be an array");
59 10
        ConfigAssertion::allIsArrayOrCallable($config[self::PROCESSORS], "'" . self::PROCESSORS . "' must be an array of callables or configuration arrays");
60 10
        ConfigAssertion::nullOrIsArrayOrInstanceOf($config[self::FORMATTER], FormatterInterface::class, "'" . self::FORMATTER . "' must be Formatter instance or configuration array");
61 9
    }
62
63 9
    public function getProcessors(): array
64
    {
65 9
        return $this->processors;
66
    }
67
68 9
    public function getFormatter()
69
    {
70 9
        return $this->formatter;
71
    }
72
}
73