Completed
Pull Request — master (#4)
by Westin
08:56
created

MainConfig::setDefaults()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11MonoLog\Config;
5
6
use Monolog\Logger;
7
8
class MainConfig
9
{
10
    /** @var HandlerConfig[] */
11
    protected $handlers = [];
12
13
    /** @var FormatterConfig[] */
14
    protected $formatters = [];
15
16
    /** @var ChannelConfig[] */
17
    protected $channels = [];
18
19
    /** @var ProcessorConfig[] */
20
    protected $processors = [];
21
22 18
    public function __construct(array $config)
23
    {
24 18
        $this->setDefaults($config);
25 18
        $this->buildFormatters($config);
26 18
        $this->buildHandlers($config);
27 18
        $this->buildChannels($config);
28 18
        $this->buildProcessors($config);
29 18
    }
30
31
    /**
32
     * @return HandlerConfig[]
33
     */
34 1
    public function getHandlers()
35
    {
36 1
        return $this->handlers;
37
    }
38
39 1
    public function hasHandlerConfig($handler) : bool
40
    {
41 1
        return key_exists($handler, $this->handlers);
42
    }
43
44
    /**
45
     * @param $handler
46
     * @return null|HandlerConfig
47
     */
48 1
    public function getHandlerConfig($handler)
49
    {
50 1
        return $this->handlers[$handler] ?? null;
51
    }
52
53
    /**
54
     * @return FormatterConfig[]
55
     */
56 2
    public function getFormatters() : array
57
    {
58 2
        return $this->formatters;
59
    }
60
61 1
    public function hasFormatterConfig($formatter) : bool
62
    {
63 1
        return key_exists($formatter, $this->formatters);
64
    }
65
66
    /**
67
     * @param $formatter
68
     * @return null|FormatterConfig
69
     */
70 1
    public function getFormatterConfig($formatter)
71
    {
72 1
        return $this->formatters[$formatter] ?? null;
73
    }
74
75
    /**
76
     * @return FormatterConfig[]
77
     */
78 2
    public function getProcessors() : array
79
    {
80 2
        return $this->processors;
81
    }
82
83 1
    public function hasProcessorConfig($processor) : bool
84
    {
85 1
        return key_exists($processor, $this->processors);
86
    }
87
88
    /**
89
     * @param $processor
90
     * @return null|ProcessorConfig
91
     */
92 1
    public function getProcessorConfig($processor)
93
    {
94 1
        return $this->processors[$processor] ?? null;
95
    }
96
97
    /**
98
     * @return ChannelConfig[]
99
     */
100 1
    public function getChannels() : array
101
    {
102 1
        return $this->channels;
103
    }
104
105
    /**
106
     * @param $channel
107
     * @return bool
108
     */
109 1
    public function hasChannelConfig($channel) : bool
110
    {
111 1
        return key_exists($channel, $this->channels);
112
    }
113
114 1
    public function getChannelConfig($channel)
115
    {
116 1
        return $this->channels[$channel] ?? null;
117
    }
118
119 18
    protected function setDefaults(array &$config)
120
    {
121 18
        if (empty($config['monolog']['handlers']['default'])) {
122 18
            $config['monolog']['handlers']['default'] = [
123
                'type' => 'noop',
124 1
                'options' => [
125 1
                    'level' => Logger::DEBUG
126
                ]
127
            ];
128
        }
129 18
130 1
        if (empty($config['monolog']['channels']['default'])) {
131 1
            $config['monolog']['channels']['default']['handlers'][] = 'default';
132
        }
133
    }
134
135 18
    protected function buildHandlers($config)
136 1
    {
137 1
        foreach ($config['monolog']['handlers'] as $name => $handlerConfig) {
138
            $this->handlers[$name] = new HandlerConfig($handlerConfig);
139
        }
140 18
    }
141
142 18
    protected function buildChannels($config)
143
    {
144 18
        foreach ($config['monolog']['channels'] as $name => $channelConfig) {
145 18
            $this->channels[$name] = new ChannelConfig($channelConfig);
146
        }
147 18
    }
148
149 18
    protected function buildFormatters($config)
150
    {
151 18
        if (empty($config['monolog']['formatters'])) {
152 18
            return;
153
        }
154 18
155
        foreach ($config['monolog']['formatters'] as $name => $formatterConfig) {
156 18
            $this->formatters[$name] = new FormatterConfig($formatterConfig);
157
        }
158 18
    }
159 1
160
    protected function buildProcessors($config)
161
    {
162 18
        if (empty($config['monolog']['processors'])) {
163 18
            return;
164
        }
165 18
166
        foreach ($config['monolog']['processors'] as $name => $processorConfig) {
167 18
            $this->processors[$name] = new ProcessorConfig($processorConfig);
168
        }
169 18
    }
170
}
171