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

HandlerConfig   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 64
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A fromArray() 0 15 3
A defaults() 0 7 1
A validate() 0 7 1
A getProcessors() 0 4 1
A getFormatter() 0 4 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