Passed
Pull Request — master (#993)
by Maxim
20:44 queued 10:42
created

LogFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 91.11%

Importance

Changes 0
Metric Value
wmc 17
eloc 42
dl 0
loc 105
ccs 41
cts 45
cp 0.9111
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getHandlers() 0 21 4
A getProcessors() 0 21 5
A getLogger() 0 21 4
A reset() 0 4 2
A createInjection() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Monolog;
6
7
use Monolog\Handler\HandlerInterface;
8
use Monolog\Logger;
9
use Monolog\Processor\PsrLogMessageProcessor;
10
use Monolog\ResettableInterface;
11
use Psr\Container\ContainerExceptionInterface;
12
use Psr\Log\LoggerInterface;
13
use Spiral\Core\Container\Autowire;
14
use Spiral\Core\Container\InjectorInterface;
15
use Spiral\Core\FactoryInterface;
16
use Spiral\Logger\ListenerRegistryInterface;
17
use Spiral\Logger\LogsInterface;
18
use Spiral\Monolog\Config\MonologConfig;
19
use Spiral\Monolog\Exception\ConfigException;
20
21
/**
22
 * @implements InjectorInterface<Logger>
23
 */
24
final class LogFactory implements LogsInterface, InjectorInterface, ResettableInterface
25
{
26
    private ?LoggerInterface $default = null;
27
    private readonly HandlerInterface $eventHandler;
28
29 21
    public function __construct(
30
        private readonly MonologConfig $config,
31
        ListenerRegistryInterface $listenerRegistry,
32
        private readonly FactoryInterface $factory
33
    ) {
34 21
        $this->eventHandler = new EventHandler($listenerRegistry, $config->getEventLevel());
0 ignored issues
show
Bug introduced by
The property eventHandler is declared read-only in Spiral\Monolog\LogFactory.
Loading history...
35
    }
36
37 21
    public function getLogger(string $channel = null): LoggerInterface
38
    {
39 21
        $default = $this->config->getDefault();
40
41 21
        if ($channel === null || $channel === $default) {
42 7
            if ($this->default !== null) {
43
                // we should use only one default logger per system
44 4
                return $this->default;
45
            }
46
47 7
            return $this->default = new Logger(
48 7
                $default,
49 7
                $this->getHandlers($default),
50 7
                $this->getProcessors($default)
51 7
            );
52
        }
53
54 16
        return new Logger(
55 16
            $channel,
56 16
            $this->getHandlers($channel),
57 16
            $this->getProcessors($channel)
58 16
        );
59
    }
60
61 2
    public function createInjection(\ReflectionClass $class, string $context = null): LoggerInterface
62
    {
63
        // always return default logger as injection
64 2
        return $this->getLogger();
65
    }
66
67 1
    public function reset(): void
68
    {
69 1
        if ($this->default instanceof ResettableInterface) {
70 1
            $this->default->reset();
0 ignored issues
show
Bug introduced by
The method reset() does not exist on Psr\Log\LoggerInterface. It seems like you code against a sub-type of Psr\Log\LoggerInterface such as Monolog\Logger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            $this->default->/** @scrutinizer ignore-call */ 
71
                            reset();
Loading history...
Bug introduced by
The method reset() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            $this->default->/** @scrutinizer ignore-call */ 
71
                            reset();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
        }
72
    }
73
74
    /**
75
     * Get list of channel specific handlers.
76
     *
77
     *
78
     * @throws ConfigException
79
     */
80 21
    protected function getHandlers(string $channel): array
81
    {
82
        // always include default handler
83 21
        $handlers = [];
84
85 21
        foreach ($this->config->getHandlers($channel) as $handler) {
86 6
            if (!$handler instanceof Autowire) {
87 2
                $handlers[] = $handler;
88 2
                continue;
89
            }
90
91
            try {
92 4
                $handlers[] = $handler->resolve($this->factory);
93
            } catch (ContainerExceptionInterface $e) {
94
                throw new ConfigException($e->getMessage(), $e->getCode(), $e);
95
            }
96
        }
97
98 20
        $handlers[] = $this->eventHandler;
99
100 20
        return $handlers;
101
    }
102
103
    /**
104
     * Get list of channel specific log processors.
105
     *
106
     * @return callable[]
107
     */
108 20
    protected function getProcessors(string $channel): array
109
    {
110 20
        $processors = [];
111 20
        foreach ($this->config->getProcessors($channel) as $processor) {
112 6
            if (!$processor instanceof Autowire) {
113 3
                $processors[] = $processor;
114 3
                continue;
115
            }
116
117
            try {
118 3
                $processors[] = $processor->resolve($this->factory);
119
            } catch (ContainerExceptionInterface $e) {
120
                throw new ConfigException($e->getMessage(), $e->getCode(), $e);
121
            }
122
        }
123
124 19
        if ($processors === []) {
125 13
            $processors[] = new PsrLogMessageProcessor();
126
        }
127
128 19
        return $processors;
129
    }
130
}
131