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

LoggerConfig   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 83
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fromArray() 0 15 3
A defaults() 0 9 1
A validate() 0 9 1
A getName() 0 4 1
A getHandlers() 0 4 1
A getProcessors() 0 4 1
A getTimezone() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MonologFactory\Config;
6
7
use DateTimeZone;
8
use Monolog\Handler\HandlerInterface;
9
10
class LoggerConfig extends BaseConfig
11
{
12
    public const NAME = 'name';
13
    public const HANDLERS = 'handlers';
14
    public const PROCESSORS = 'processors';
15
    public const TIMEZONE = 'timezone';
16
17
    /** @var string */
18
    protected $name;
19
20
    /** @var HandlerInterface[]|HandlerConfig[] */
21
    protected $handlers;
22
23
    /** @var callable[]|ProcessorConfig[] */
24
    protected $processors;
25
26
    /** @var DateTimeZone */
27
    protected $timezone;
28
29 11
    protected function __construct(string $name, array $handlers, array $processors, DateTimeZone $timezone)
30
    {
31 11
        $this->name = $name;
32 11
        $this->handlers = $handlers;
33 11
        $this->processors = $processors;
34 11
        $this->timezone = $timezone;
35 11
    }
36
37 16
    public static function fromArray(array $config)
38
    {
39 16
        $config = self::filter($config);
40
41 12
        return new static(
42 12
            $config[self::NAME],
43
            array_map(function ($handler) {
44 10
                return is_array($handler) ? HandlerConfig::fromArray($handler) : $handler;
45 12
            }, $config[self::HANDLERS]),
46
            array_map(function ($processor) {
47 8
                return is_array($processor) ? ProcessorConfig::fromArray($processor) : $processor;
48 11
            }, $config[self::PROCESSORS]),
49 11
            new DateTimeZone($config[self::TIMEZONE])
50
        );
51
    }
52
53 16
    protected static function defaults(): array
54
    {
55
        return [
56 16
            self::NAME => '',
57 16
            self::HANDLERS => [],
58 16
            self::PROCESSORS => [],
59 16
            self::TIMEZONE => date_default_timezone_get(),
60
        ];
61
    }
62
63 16
    protected static function validate(array $config): void
64
    {
65 16
        ConfigAssertion::notEmptyString($config[self::NAME], "'" . self::NAME . "' is required and cannot be empty");
66 16
        ConfigAssertion::isArray($config[self::HANDLERS], "'" . self::HANDLERS . "' must be an array");
67 15
        ConfigAssertion::allIsArrayOrInstanceOf($config[self::HANDLERS], HandlerInterface::class, "'" . self::HANDLERS . "' must be an array of Handler instances or configuration arrays");
68 14
        ConfigAssertion::isArray($config[self::PROCESSORS], "'" . self::PROCESSORS . "' must be an array");
69 13
        ConfigAssertion::allIsArrayOrCallable($config[self::PROCESSORS], "'" . self::PROCESSORS . "' must be an array of callables or configuration arrays");
70 12
        ConfigAssertion::string($config[self::TIMEZONE], "'" . self::TIMEZONE . "' must be a string");
71 12
    }
72
73 11
    public function getName(): string
74
    {
75 11
        return $this->name;
76
    }
77
78 11
    public function getHandlers(): array
79
    {
80 11
        return $this->handlers;
81
    }
82
83 11
    public function getProcessors(): array
84
    {
85 11
        return $this->processors;
86
    }
87
88 11
    public function getTimezone(): DateTimeZone
89
    {
90 11
        return $this->timezone;
91
    }
92
}
93