Completed
Push — master ( 3181c3...80f4ac )
by Juuso
10:53
created

Yii2Monolog   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 117
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getLogger() 0 8 2
A bootstrap() 0 5 1
A setChannels() 0 4 1
A setMainChannel() 0 4 1
A registerLoggers() 0 12 2
A registerPsrLogger() 0 6 1
A getMainChannel() 0 8 2
1
<?php
2
3
4
namespace leinonen\Yii2Monolog;
5
6
7
use leinonen\Yii2Monolog\Factories\MonologFactory;
8
use Monolog\Logger;
9
use Psr\Log\LoggerInterface;
10
use Yii;
11
use yii\base\Application;
12
use yii\base\BootstrapInterface;
13
use yii\base\Component;
14
15
class Yii2Monolog extends Component implements BootstrapInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    private $channels;
21
22
    /**
23
     * @var MonologFactory
24
     */
25
    private $monologFactory;
26
27
    /**
28
     * @var string
29
     */
30
    private $mainChannel;
31
32
    /**
33
     * @var LoggerRegistry
34
     */
35
    private $loggerRegistry;
36
37
    /**
38
     * Initiates a new Yii2Monolog.
39
     *
40
     * @param MonologFactory $monologFactory
41
     * @param LoggerRegistry $loggerRegistry
42
     * @param array $config
43
     */
44 8
    public function __construct(MonologFactory $monologFactory, LoggerRegistry $loggerRegistry, array $config = [])
45
    {
46 8
        $this->monologFactory = $monologFactory;
47 8
        $this->loggerRegistry = $loggerRegistry;
48 8
        parent::__construct($config);
49 8
    }
50
51
    /**
52
     * Returns the given logger channel.
53
     *
54
     * @param string $channel
0 ignored issues
show
Documentation introduced by
Should the type for parameter $channel not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
55
     *
56
     * @return Logger
57
     */
58 6
    public function getLogger(string $channel = null): Logger
59
    {
60 6
        if ($channel === null) {
61 1
            $channel = $this->getMainChannel();
62
        }
63
64 6
        return $this->loggerRegistry->getLogger($channel);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 8
    public function bootstrap($app)
71
    {
72 8
        $this->registerLoggers();
73 8
        $this->registerPsrLogger();
74 8
    }
75
76
    /**
77
     * @param array $channelConfiguration
78
     */
79 8
    public function setChannels(array $channelConfiguration)
80
    {
81 8
        $this->channels = $channelConfiguration;
82 8
    }
83
84
    /**
85
     * @param string $channelName
86
     */
87 2
    public function setMainChannel(string $channelName)
88
    {
89 2
        $this->mainChannel = $channelName;
90 2
    }
91
92
    /**
93
     * Registers loggers into Yii's DI container.
94
     */
95 8
    private function registerLoggers()
96
    {
97 8
        foreach ($this->channels as $configuredChannelName => $channelConfiguration) {
98 8
            $channelName = $configuredChannelName;
99 8
            $handlers = $channelConfiguration['handlers'] ?? [];
100 8
            $processors = $channelConfiguration['processors'] ?? [];
101
102
            $this->loggerRegistry->registerLogChannel($channelName, function () use ($channelName, $handlers, $processors) {
103 8
                return $this->monologFactory->make($channelName, $handlers, $processors);
104 8
            });
105
        }
106 8
    }
107
108
    /**
109
     * Registers the main channel to be used for Psr LoggerInterface.
110
     */
111
    private function registerPsrLogger()
112
    {
113 8
        $this->loggerRegistry->registerPsrLogger(function () {
114 3
            return $this->getLogger($this->getMainChannel());
115 8
        });
116 8
    }
117
118
    /**
119
     * Returns the main channel to be used for Yii2Monolog component.
120
     *
121
     * @return string
122
     */
123 4
    private function getMainChannel(): string
124
    {
125 4
        if ($this->mainChannel === null) {
126 2
            return array_keys($this->channels)[0];
127
        }
128
129 2
        return $this->mainChannel;
130
    }
131
}
132