Passed
Pull Request — master (#1102)
by Aleksei
10:04
created

LogFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 17
eloc 42
dl 0
loc 104
ccs 39
cts 45
cp 0.8667
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getLogger() 0 21 4
A getHandlers() 0 21 4
A getProcessors() 0 21 5
A reset() 0 4 2
A createInjection() 0 3 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 22
    public function __construct(
30
        private readonly MonologConfig $config,
31
        ListenerRegistryInterface $listenerRegistry,
32
        private readonly FactoryInterface $factory
33
    ) {
34 22
        $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 22
    public function getLogger(string $channel = null): LoggerInterface
38
    {
39 22
        $default = $this->config->getDefault();
40
41 22
        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 17
        return new Logger(
55 17
            $channel,
56 17
            $this->getHandlers($channel),
57 17
            $this->getProcessors($channel)
58 17
        );
59
    }
60
61
    public function createInjection(\ReflectionClass $class, ?string $context = null): LoggerInterface
62
    {
63
        return $this->getLogger();
64
    }
65
66 1
    public function reset(): void
67
    {
68 1
        if ($this->default instanceof ResettableInterface) {
69 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

69
            $this->default->/** @scrutinizer ignore-call */ 
70
                            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

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