Passed
Push — master ( a8d0c9...8a0571 )
by Aleksei
07:15 queued 22s
created

LogFactory::createInjection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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\LoggerInjector;
18
use Spiral\Logger\LogsInterface;
19
use Spiral\Monolog\Config\MonologConfig;
20
use Spiral\Monolog\Exception\ConfigException;
21
22
/**
23
 * @implements InjectorInterface<Logger>
24
 */
25
final class LogFactory implements LogsInterface, InjectorInterface, ResettableInterface
26
{
27
    private ?LoggerInterface $default = null;
28
    private readonly HandlerInterface $eventHandler;
29
30 22
    public function __construct(
31
        private readonly MonologConfig $config,
32
        ListenerRegistryInterface $listenerRegistry,
33
        private readonly FactoryInterface $factory
34
    ) {
35 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...
36
    }
37
38 22
    public function getLogger(?string $channel = null): LoggerInterface
39
    {
40 22
        $default = $this->config->getDefault();
41
42 22
        if ($channel === null || $channel === $default) {
43 7
            if ($this->default !== null) {
44
                // we should use only one default logger per system
45 4
                return $this->default;
46
            }
47
48 7
            return $this->default = new Logger(
49 7
                $default,
50 7
                $this->getHandlers($default),
51 7
                $this->getProcessors($default)
52 7
            );
53
        }
54
55 17
        return new Logger(
56 17
            $channel,
57 17
            $this->getHandlers($channel),
58 17
            $this->getProcessors($channel)
59 17
        );
60
    }
61
62
    /**
63
     * @deprecated use {@see LoggerInjector} as an injector instead.
64
     */
65
    public function createInjection(\ReflectionClass $class, ?string $context = null): LoggerInterface
66
    {
67
        return $this->getLogger();
68
    }
69
70 1
    public function reset(): void
71
    {
72 1
        if ($this->default instanceof ResettableInterface) {
73 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

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

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