Passed
Push — master ( 3312bd...c25b5e )
by Kirill
04:35
created

LogFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 106
rs 10
c 1
b 0
f 0
eloc 33
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getHandlers() 0 21 4
A getProcessors() 0 3 1
A getLogger() 0 19 4
A createInjection() 0 4 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Monolog;
13
14
use Monolog\Handler\HandlerInterface;
15
use Monolog\Logger;
16
use Monolog\Processor\PsrLogMessageProcessor;
17
use Psr\Container\ContainerExceptionInterface;
18
use Psr\Log\LoggerInterface;
19
use Spiral\Core\Container\Autowire;
20
use Spiral\Core\Container\InjectorInterface;
21
use Spiral\Core\FactoryInterface;
22
use Spiral\Logger\ListenerRegistryInterface;
23
use Spiral\Logger\LogsInterface;
24
use Spiral\Monolog\Config\MonologConfig;
25
use Spiral\Monolog\Exception\ConfigException;
26
27
final class LogFactory implements LogsInterface, InjectorInterface
28
{
29
    // Default logger channel (supplied via injection)
30
    public const DEFAULT = 'default';
31
32
    /** @var MonologConfig */
33
    private $config;
34
35
    /** @var LoggerInterface */
36
    private $default;
37
38
    /** @var FactoryInterface */
39
    private $factory;
40
41
    /** @var HandlerInterface|null */
42
    private $eventHandler;
43
44
    /**
45
     * @param MonologConfig             $config
46
     * @param ListenerRegistryInterface $listenerRegistry
47
     * @param FactoryInterface          $factory
48
     */
49
    public function __construct(
50
        MonologConfig $config,
51
        ListenerRegistryInterface $listenerRegistry,
52
        FactoryInterface $factory
53
    ) {
54
        $this->config = $config;
55
        $this->factory = $factory;
56
        $this->eventHandler = new EventHandler($listenerRegistry, $config->getEventLevel());
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getLogger(string $channel = null): LoggerInterface
63
    {
64
        if ($channel === null || $channel == self::DEFAULT) {
65
            if ($this->default !== null) {
66
                // we should use only one default logger per system
67
                return $this->default;
68
            }
69
70
            return $this->default = new Logger(
71
                self::DEFAULT,
72
                $this->getHandlers(self::DEFAULT),
73
                $this->getProcessors(self::DEFAULT)
74
            );
75
        }
76
77
        return new Logger(
78
            $channel,
79
            $this->getHandlers($channel),
80
            $this->getProcessors($channel)
81
        );
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function createInjection(\ReflectionClass $class, string $context = null)
88
    {
89
        // always return default logger as injection
90
        return $this->getLogger();
91
    }
92
93
    /**
94
     * Get list of channel specific handlers.
95
     *
96
     * @param string $channel
97
     * @return array
98
     *
99
     * @throws ConfigException
100
     */
101
    protected function getHandlers(string $channel): array
102
    {
103
        // always include default handler
104
        $handlers = [];
105
106
        foreach ($this->config->getHandlers($channel) as $handler) {
107
            if (!$handler instanceof Autowire) {
108
                $handlers[] = $handler;
109
                continue;
110
            }
111
112
            try {
113
                $handlers[] = $handler->resolve($this->factory);
114
            } catch (ContainerExceptionInterface $e) {
115
                throw new ConfigException($e->getMessage(), $e->getCode(), $e);
0 ignored issues
show
Bug introduced by
The method getCode() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

115
                throw new ConfigException($e->getMessage(), $e->/** @scrutinizer ignore-call */ getCode(), $e);
Loading history...
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

115
                throw new ConfigException($e->/** @scrutinizer ignore-call */ getMessage(), $e->getCode(), $e);
Loading history...
116
            }
117
        }
118
119
        $handlers[] = $this->eventHandler;
120
121
        return $handlers;
122
    }
123
124
    /**
125
     * Get list of channel specific log processors. Falls back to PsrLogMessageProcessor for now.
126
     *
127
     * @param string $channel
128
     * @return callable[]
129
     */
130
    protected function getProcessors(string $channel): array
0 ignored issues
show
Unused Code introduced by
The parameter $channel is not used and could be removed. ( Ignorable by Annotation )

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

130
    protected function getProcessors(/** @scrutinizer ignore-unused */ string $channel): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        return [new PsrLogMessageProcessor()];
133
    }
134
}
135