Test Setup Failed
Pull Request — master (#15)
by
unknown
08:18
created

MonologFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 192
c 0
b 0
f 0
wmc 13
lcom 1
cbo 5
ccs 46
cts 46
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A make() 0 7 1
A makeHandlers() 0 18 2
A makeProcessors() 0 17 3
A mapConfigurations() 0 12 2
A getFormatterFromHandlerConfig() 0 11 2
A getProcessorsFromHandlerConfig() 0 8 2
1
<?php
2
3
namespace bessonov87\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 12
    public function __construct(
33
        HandlerFactory $handlerFactory,
34
        ProcessorFactory $processorFactory,
35
        FormatterFactory $formatterFactory
36
    ) {
37 12
        $this->handlerFactory = $handlerFactory;
38 12
        $this->processorFactory = $processorFactory;
39 12
        $this->formatterFactory = $formatterFactory;
40 12
    }
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 12
    public function make(string $name, array $handlers = [], array $processors = []): Logger
71
    {
72 12
        $handlers = $this->makeHandlers($handlers);
73 12
        $processors = $this->makeProcessors($processors);
74
75 12
        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 12
    private function makeHandlers(array $handlerConfigs)
91
    {
92 12
        if (empty($handlerConfigs)) {
93 5
            return [];
94
        }
95
96 7
        return $this->mapConfigurations(
97
            $handlerConfigs,
98 7
            function ($handlerClass, $handlerConfig) {
99 7
                $formatter = $this->getFormatterFromHandlerConfig($handlerConfig);
100 7
                $processors = $this->getProcessorsFromHandlerConfig($handlerConfig);
101
102 7
                unset($handlerConfig['formatter'], $handlerConfig['processors']);
103
104 7
                return $this->handlerFactory->make($handlerClass, $handlerConfig, $formatter, $processors);
105 7
            }
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 12
    private function makeProcessors(array $processorConfigs): array
122
    {
123 12
        if (empty($processorConfigs)) {
124 5
            return [];
125
        }
126
127 8
        return $this->mapConfigurations(
128
            $processorConfigs,
129 8
            function ($processor, $config) {
130 8
                if (\is_callable($processor)) {
131 7
                    return $processor;
132
                }
133
134 8
                return $this->processorFactory->make($processor, $config);
135 8
            }
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
    private function mapConfigurations(array $config, callable $mapFunction): array
148
    {
149 8
        return collect($config)->map(function ($configValue, $configKey) use ($mapFunction) {
150
            // In case the key is int assume that just the configurable class name has been given
151
            // and supply empty configurations.
152 8
            if (\is_int($configKey)) {
153 8
                return $mapFunction($configValue, []);
154
            }
155
156 8
            return $mapFunction($configKey, $configValue);
157 8
        })->values()->all();
158
    }
159
160
    /**
161
     * Returns the formatter from given handler config array.
162
     *
163
     * @param array $handlerConfig
164
     *
165
     * @return FormatterInterface|null
166
     *
167
     * @throws \yii\base\InvalidConfigException
168
     * @throws \InvalidArgumentException
169
     */
170 7
    private function getFormatterFromHandlerConfig(array $handlerConfig)
171
    {
172 7
        if (isset($handlerConfig['formatter'])) {
173 7
            return $this->mapConfigurations(
174 7
                $handlerConfig['formatter'],
175 7
                function ($formatterClass, $formatterConfig) {
176 7
                    return $this->formatterFactory->make($formatterClass, $formatterConfig);
177 7
                }
178 7
            )[0];
179
        }
180 1
    }
181
182
    /**
183
     * Returns processors from the given handler config array.
184
     *
185
     * @param array $handlerConfig
186
     *
187
     * @return callable[]|array
188
     *
189
     * @throws \yii\base\InvalidConfigException
190
     * @throws \InvalidArgumentException
191
     */
192 7
    private function getProcessorsFromHandlerConfig(array $handlerConfig): array
193
    {
194 7
        if (isset($handlerConfig['processors'])) {
195 7
            return $this->makeProcessors($handlerConfig['processors']);
196
        }
197
198 1
        return [];
199
    }
200
}
201