Passed
Push — master ( 9ec1b2...596585 )
by Juuso
03:58
created

MonologFactory::getFormatterFromHandlerConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace leinonen\Yii2Monolog\Factories;
4
5
use Monolog\Logger;
6
use Monolog\Handler\HandlerInterface;
7
use Monolog\Formatter\FormatterInterface;
8
9
class MonologFactory
10
{
11
    /**
12
     * @var HandlerFactory
13
     */
14
    private $handlerFactory;
15
16
    /**
17
     * @var ProcessorFactory
18
     */
19
    private $processorFactory;
20
    /**
21
     * @var FormatterFactory
22
     */
23
    private $formatterFactory;
24
25
    /**
26
     * Initializes a new MonologFactory.
27
     *
28
     * @param HandlerFactory $handlerFactory
29
     * @param ProcessorFactory $processorFactory
30
     * @param FormatterFactory $formatterFactory
31
     */
32 3
    public function __construct(
33
        HandlerFactory $handlerFactory,
34
        ProcessorFactory $processorFactory,
35
        FormatterFactory $formatterFactory
36
    ) {
37 3
        $this->handlerFactory = $handlerFactory;
38 3
        $this->processorFactory = $processorFactory;
39 3
        $this->formatterFactory = $formatterFactory;
40 3
    }
41
42
    /**
43
     * @param string $name
44
     * @param array $handlers
45
     * An array of handlers in format of:
46
     *  [
47
     *      HandlerClass::class => [
48
     *          'configKey' => 'value',
49
     *      ],
50
     *  ]
51
     *
52
     * @param array $processors
53
     * An array of processors in format of:
54
     *  [
55
     *      function ($record) {
56
     *          return $record;
57
     *      },
58
     *      MyProcessor::class => [
59
     *          'configKey' => 'value',
60
     *      ],
61
     * ]
62
     *
63
     * You can supply both callables and invokable classes.
64
     *
65
     * @return Logger
66
     *
67
     * @throws \yii\base\InvalidConfigException
68
     * @throws \InvalidArgumentException
69
     */
70 3
    public function make(string $name, array $handlers = [], array $processors = []): Logger
71
    {
72 3
        $handlers = $this->makeHandlers($handlers);
73 3
        $processors = $this->makeProcessors($processors);
74
75 3
        return new Logger($name, $handlers, $processors);
76
    }
77
78
    /**
79
     * Returns an array of handlers based on the given configuration.
80
     *
81
     * @param array $handlerConfigs
82
     *
83
     * @see MonologFactory::make() for the array format.
84
     *
85
     * @return HandlerInterface[]
86
     *
87
     * @throws \yii\base\InvalidConfigException
88
     * @throws \InvalidArgumentException
89
     */
90 3
    private function makeHandlers(array $handlerConfigs)
91
    {
92 3
        if (empty($handlerConfigs)) {
93 2
            return [];
94
        }
95
96 1
        return $this->mapConfigurations(
97
            $handlerConfigs,
98
            function ($handlerClass, $handlerConfig) {
99 1
                $formatter = $this->getFormatterFromHandlerConfig($handlerConfig);
100 1
                $processors = $this->getProcessorsFromHandlerConfig($handlerConfig);
101
102 1
                unset($handlerConfig['formatter'], $handlerConfig['processors']);
103
104 1
                return $this->handlerFactory->make($handlerClass, $handlerConfig, $formatter, $processors);
105 1
            }
106
        );
107
    }
108
109
    /**
110
     * Returns an array of processors based on the given configuration.
111
     *
112
     * @param array $processorConfigs
113
     *
114
     * @see MonologFactory::make() for the array format.
115
     *
116
     * @return callable[]
117
     *
118
     * @throws \yii\base\InvalidConfigException
119
     * @throws \InvalidArgumentException
120
     */
121 3
    private function makeProcessors(array $processorConfigs): array
122
    {
123 3
        if (empty($processorConfigs)) {
124 2
            return [];
125
        }
126
127 2
        return $this->mapConfigurations(
128
            $processorConfigs,
129
            function ($processor, $config) {
130 2
                if (\is_callable($processor)) {
131 1
                    return $processor;
132
                }
133
134 2
                return $this->processorFactory->make($processor, $config);
135 2
            }
136
        );
137
    }
138
139
    /**
140
     * Maps the given configurations with the given callable.
141
     *
142
     * @param array $config
143
     * @param callable $mapFunction
144
     *
145
     * @return array
146
     */
147 2
    private function mapConfigurations(array $config, callable $mapFunction): array
148
    {
149 2
        $values = [];
150
151 2
        foreach ($config as $configKey => $configValue) {
152
            // In case the key is int assume that just the configurable class name has been given
153
            // and supply empty configurations.
154 2
            if (is_int($configKey)) {
155 2
                $values[] = $mapFunction($configValue, []);
156
            } else {
157 2
                $values[] = $mapFunction($configKey, $configValue);
158
            }
159
        }
160
161 2
        return $values;
162
    }
163
164
    /**
165
     * Returns the formatter from given handler config array.
166
     *
167
     * @param array $handlerConfig
168
     *
169
     * @return FormatterInterface|null
170
     *
171
     * @throws \yii\base\InvalidConfigException
172
     * @throws \InvalidArgumentException
173
     */
174 1
    private function getFormatterFromHandlerConfig(array $handlerConfig)
175
    {
176 1
        if (isset($handlerConfig['formatter'])) {
177 1
            return $this->mapConfigurations(
178 1
                $handlerConfig['formatter'],
179 1
                function ($formatterClass, $formatterConfig) {
180 1
                    return $this->formatterFactory->make($formatterClass, $formatterConfig);
181 1
                }
182 1
            )[0];
183
        }
184 1
    }
185
186
    /**
187
     * Returns processors from the given handler config array.
188
     *
189
     * @param array $handlerConfig
190
     *
191
     * @return callable[]|array
192
     *
193
     * @throws \yii\base\InvalidConfigException
194
     * @throws \InvalidArgumentException
195
     */
196 1
    private function getProcessorsFromHandlerConfig(array $handlerConfig): array
197
    {
198 1
        if (isset($handlerConfig['processors'])) {
199 1
            return $this->makeProcessors($handlerConfig['processors']);
200
        }
201
202 1
        return [];
203
    }
204
}
205