Passed
Pull Request — master (#1)
by Alex
08:35
created

LoggerFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 72.97%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
eloc 43
c 2
b 1
f 0
dl 0
loc 110
ccs 27
cts 37
cp 0.7297
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getHandlers() 0 33 7
A __invoke() 0 11 1
A getProcessors() 0 32 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasMonolog\Factory;
6
7
use Arp\LaminasFactory\AbstractFactory;
8
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
9
use Laminas\ServiceManager\ServiceLocatorInterface;
10
use Monolog\Handler\HandlerInterface;
11
use Monolog\Logger;
12
use Monolog\Processor\ProcessorInterface;
13
use Psr\Container\ContainerInterface;
14
15
/**
16
 * Factory class used to create a new instance of a Monolog\Logger based on configuration options
17
 *
18
 * @author  Alex Patterson <[email protected]>
19
 * @package Arp\LaminasMonolog\Factory
20
 */
21
class LoggerFactory extends AbstractFactory
22
{
23
    /**
24
     * @param ServiceLocatorInterface&ContainerInterface $container
25
     * @param string                                     $requestedName
26
     * @param array<mixed>|null                          $options
27
     *
28
     * @return Logger
29
     *
30
     * @throws ServiceNotCreatedException
31
     */
32 4
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): Logger
33
    {
34 4
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'loggers');
35
36 4
        $name = $options['name'] ?? $requestedName;
37
38 4
        return new Logger(
39
            $name,
40 4
            $this->getHandlers($container, $options['handlers'] ?? [], $requestedName),
41 3
            $this->getProcessors($container, $options['processors'] ?? [], $requestedName),
42 3
            $options['timezone'] ?? null
43
        );
44
    }
45
46
    /**
47
     * @param ServiceLocatorInterface&ContainerInterface $container
48
     * @param array<mixed>                               $handlerConfigs
49
     * @param string                                     $serviceName
50
     *
51
     * @return array<HandlerInterface>
52
     *
53
     * @throws ServiceNotCreatedException
54
     */
55 4
    private function getHandlers(
56
        ServiceLocatorInterface $container,
57
        array $handlerConfigs,
58
        string $serviceName
59
    ): array {
60 4
        $handlers = [];
61 4
        foreach ($handlerConfigs as $handlerName => $handler) {
62 4
            if (is_string($handler)) {
63 3
                $handlerName = $handler;
64 3
                $handler = $this->getService($container, $handlerName, $serviceName);
65
            }
66
67 4
            if (is_array($handler)) {
68 1
                $handler = $this->buildService($container, $handlerName, $handler, $serviceName);
69
            }
70
71 4
            if (!$handler instanceof HandlerInterface) {
72 1
                throw new ServiceNotCreatedException(
73 1
                    sprintf(
74 1
                        'The log handler \'%s\' must be an object of type \'%s\'; '
75
                        . '\'%s\' provided for service \'%s\'',
76 1
                        is_string($handlerName) ? $handlerName : gettype($handler),
77
                        HandlerInterface::class,
78 1
                        is_object($handler) ? get_class($handler) : gettype($handler),
79
                        $serviceName,
80
                    )
81
                );
82
            }
83
84 3
            $handlers[] = $handler;
85
        }
86
87 3
        return $handlers;
88
    }
89
90
    /**
91
     * @param ServiceLocatorInterface&ContainerInterface $container
92
     * @param array<mixed>                               $processorConfigs
93
     * @param string                                     $serviceName
94
     *
95
     * @return array<callable>
96
     *
97
     * @throws ServiceNotCreatedException
98
     */
99 3
    private function getProcessors(
100
        ServiceLocatorInterface $container,
101
        array $processorConfigs,
102
        string $serviceName
103
    ): array {
104 3
        $processors = [];
105 3
        foreach ($processorConfigs as $processorName => $processor) {
106
            if (is_string($processor)) {
107
                $processor = $this->getService($container, $processor, $serviceName);
108
            }
109
110
            if (is_array($processor)) {
111
                $processor = $this->buildService($container, $processorName, $processor, $serviceName);
112
            }
113
114
            if (!$processor instanceof ProcessorInterface) {
115
                throw new ServiceNotCreatedException(
116
                    sprintf(
117
                        'The log processor \'%s\' must be an object of type \'%s\'; '
118
                        . '\'%s\' provided for service \'%s\'',
119
                        $processorName,
120
                        ProcessorInterface::class,
121
                        is_object($processor) ? get_class($processor) : gettype($processor),
122
                        $serviceName,
123
                    )
124
                );
125
            }
126
127
            $processors[] = $processor;
128
        }
129
130 3
        return $processors;
131
    }
132
}
133