Passed
Push — master ( 5e2b25...073a92 )
by Carlos
02:18 queued 28s
created

Config::getDefaultChannel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/cuttle.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Cuttle;
13
14
use Closure;
15
use InvalidArgumentException;
16
use Monolog\Formatter\LineFormatter;
17
use Monolog\Handler\StreamHandler;
18
19
/**
20
 * Class Config.
21
 *
22
 * @author overtrue <[email protected]>
23
 */
24
class Config
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $formatters = [];
30
31
    /**
32
     * @var array
33
     */
34
    protected $handlers = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $processors = [];
40
41
    /**
42
     * @var array
43
     */
44
    protected $channels = [];
45
46
    /**
47
     * @var string
48
     */
49
    protected $defaultChannel = '';
50
51
    /**
52
     * Config constructor.
53
     *
54
     * @param array $config
55
     */
56
    public function __construct(array $config)
57
    {
58
        $this->parse($config);
59
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @return array
65
     */
66
    public function getChannel(string $name)
67
    {
68
        if (empty($this->channels[$name])) {
69
            throw new InvalidArgumentException("No channel named '{$name}' found.");
70
        }
71
72
        $this->channels[$name]['handlers'] = $this->getHandlers($this->channels[$name]['handlers']);
73
        $this->channels[$name]['processors'] = $this->getProcessors($this->channels[$name]['processors']);
74
75
        return $this->channels[$name];
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getDefaultChannel()
82
    {
83
        if (!$this->defaultChannel) {
84
            throw new \LogicException('No default channel configured.');
85
        }
86
87
        return $this->defaultChannel;
88
    }
89
90
    /**
91
     * @param array $names
92
     *
93
     * @return array
94
     */
95
    protected function getFormatters(array $names)
96
    {
97
        return array_map(function ($name) {
98
            return $this->getFormatter($name);
99
        }, $names);
100
    }
101
102
    /**
103
     * @param array $names
104
     *
105
     * @return array
106
     */
107
    protected function getHandlers(array $names)
108
    {
109
        return array_map(function ($name) {
110
            return $this->getHandler($name);
111
        }, $names);
112
    }
113
114
    /**
115
     * @param array $names
116
     *
117
     * @return array
118
     */
119
    protected function getProcessors(array $names)
120
    {
121
        return array_map(function ($name) {
122
            return $this->getProcessor($name);
123
        }, $names);
124
    }
125
126
    /**
127
     * @param string $formatterId
128
     *
129
     * @return \Monolog\Formatter\FormatterInterface
130
     */
131
    protected function getFormatter(string $formatterId)
132
    {
133
        if ($this->formatters[$formatterId] instanceof Closure) {
134
            $this->formatters[$formatterId] = $this->formatters[$formatterId]();
135
        }
136
137
        return $this->formatters[$formatterId];
138
    }
139
140
    /**
141
     * @param string $handlerId
142
     *
143
     * @return \Monolog\Handler\HandlerInterface
144
     */
145
    protected function getHandler(string $handlerId)
146
    {
147
        if ($this->handlers[$handlerId] instanceof Closure) {
148
            $this->handlers[$handlerId] = $this->handlers[$handlerId]();
149
        }
150
151
        return $this->handlers[$handlerId];
152
    }
153
154
    /**
155
     * @param string $processorId
156
     *
157
     * @return \Monolog\Handler\HandlerInterface
158
     */
159
    protected function getProcessor(string $processorId)
160
    {
161
        if ($this->processors[$processorId] instanceof Closure) {
162
            $this->processors[$processorId] = $this->processors[$processorId]();
163
        }
164
165
        return $this->processors[$processorId];
166
    }
167
168
    /**
169
     * @param array $config
170
     */
171
    protected function parse(array $config)
172
    {
173
        $this->formatters = $this->formatFormatters($config['formatters'] ?? []);
174
        $this->handlers = $this->formatHandlers($config['handlers'] ?? []);
175
        $this->processors = $this->formatProcessors($config['processors'] ?? []);
176
        $this->channels = $this->formatChannels($config['channels'] ?? []);
177
        $this->defaultChannel = $config['default'];
178
    }
179
180
    /**
181
     * @param array $formatters
182
     *
183
     * @return array
184
     */
185 View Code Duplication
    protected function formatFormatters(array $formatters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187
        foreach ($formatters as $id => $option) {
188
            $class = $option['formatter'] ?? LineFormatter::class;
189
            unset($option['formatter']);
190
191
            $formatters[$id] = function () use ($class, $option) {
192
                return (new ClassResolver($class))->resolve($option);
193
            };
194
        }
195
196
        return $formatters;
197
    }
198
199
    /**
200
     * @param array $handlers
201
     *
202
     * @return array
203
     */
204
    protected function formatHandlers(array $handlers = [])
205
    {
206
        foreach ($handlers as $id => $option) {
207
            if (isset($option['formatter']) && !isset($this->formatters[$option['formatter']])) {
208
                throw new InvalidArgumentException(sprintf('Formatter %s not configured.', $option['formatter']));
209
            }
210
211 View Code Duplication
            foreach ($option['processors'] ?? [] as $processorId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
                if (!isset($this->processors[$processorId])) {
213
                    throw new InvalidArgumentException(sprintf('Processor %s not configured.', $processorId));
214
                }
215
            }
216
217
            $class = $option['handler'] ?? StreamHandler::class;
218
            unset($option['handler']);
219
220
            $handlers[$id] = function () use ($class, $option) {
221
                $handler = (new ClassResolver($class))->resolve($option);
222
223
                if (!empty($option['formatter'])) {
224
                    $handler->setFormatter($this->getFormatter($option['formatter']));
225
                }
226
227
                if (!empty($option['processors'])) {
228
                    $handler->pushProcessor($this->getProcessors($option['processors']));
229
                }
230
231
                return $handler;
232
            };
233
        }
234
235
        return $handlers;
236
    }
237
238
    /**
239
     * @param array $processors
240
     *
241
     * @return array
242
     */
243 View Code Duplication
    protected function formatProcessors(array $processors = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
    {
245
        foreach ($processors as $id => $option) {
246
            if (empty($option['processor'])) {
247
                continue;
248
            }
249
250
            $class = $option['processor'];
251
            unset($option['processor']);
252
253
            $processors[$id] = function () use ($class, $option) {
254
                return (new ClassResolver($class))->resolve($option);
255
            };
256
        }
257
258
        return $processors;
259
    }
260
261
    /**
262
     * @param array $channels
263
     *
264
     * @return array
265
     */
266
    protected function formatChannels(array $channels = [])
267
    {
268
        foreach ($channels as $id => $option) {
269 View Code Duplication
            foreach ($option['processors'] ?? [] as $processorId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
                if (!isset($this->processors[$processorId])) {
271
                    throw new InvalidArgumentException(sprintf('Processor %s not configured.', $processorId));
272
                }
273
            }
274
275 View Code Duplication
            foreach ($option['handlers'] ?? [] as $handlerId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
276
                if (!isset($this->handlers[$handlerId])) {
277
                    throw new InvalidArgumentException(sprintf('Handler %s not configured.', $handlerId));
278
                }
279
            }
280
            $channels[$id] = array_merge(['handlers' => [], 'processors' => []], $option);
281
        }
282
283
        return $channels;
284
    }
285
}
286