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

MonologConfig   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A wire() 0 15 4
A getHandlers() 0 15 6
A getEventLevel() 0 3 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\Config;
13
14
use Monolog\Logger;
15
use Spiral\Core\Container\Autowire;
16
use Spiral\Core\InjectableConfig;
17
use Spiral\Monolog\Exception\ConfigException;
18
19
final class MonologConfig extends InjectableConfig
20
{
21
    public const CONFIG = 'monolog';
22
23
    /** @var array */
24
    protected $config = [
25
        'globalLevel' => Logger::DEBUG,
26
        'handlers'    => []
27
    ];
28
29
    /**
30
     * @return int
31
     */
32
    public function getEventLevel(): int
33
    {
34
        return $this->config['globalLevel'] ?? Logger::DEBUG;
35
    }
36
37
    /**
38
     * @param string $channel
39
     * @return \Generator|Autowire[]
40
     *
41
     * @throws ConfigException
42
     */
43
    public function getHandlers(string $channel): \Generator
44
    {
45
        if (empty($this->config['handlers'][$channel])) {
46
            return;
47
        }
48
49
        foreach ($this->config['handlers'][$channel] as $handler) {
50
            if (is_object($handler) && !$handler instanceof Autowire) {
51
                yield $handler;
52
                continue;
53
            }
54
55
            $wire = $this->wire($channel, $handler);
56
            if (!empty($wire)) {
57
                yield $wire;
58
            }
59
        }
60
    }
61
62
    /**
63
     * @param string $channel
64
     * @param mixed  $handler
65
     * @return null|Autowire
66
     *
67
     * @throws ConfigException
68
     */
69
    private function wire(string $channel, $handler): ?Autowire
70
    {
71
        if ($handler instanceof Autowire) {
72
            return $handler;
73
        }
74
75
        if (is_string($handler)) {
76
            return new Autowire($handler);
77
        }
78
79
        if (isset($handler['class'])) {
80
            return new Autowire($handler['class'], $handler['options'] ?? []);
81
        }
82
83
        throw new ConfigException("Invalid handler definition for channel `{$channel}`.");
84
    }
85
}
86