Passed
Pull Request — master (#993)
by Maxim
20:44 queued 10:42
created

MonologConfig::getDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Monolog\Config;
6
7
use Monolog\Logger;
8
use Spiral\Core\Container\Autowire;
9
use Spiral\Core\InjectableConfig;
10
use Spiral\Monolog\Exception\ConfigException;
11
12
final class MonologConfig extends InjectableConfig
13
{
14
    public const CONFIG = 'monolog';
15
    public const DEFAULT_CHANNEL = 'default';
16
17
    protected array $config = [
18
        'default'     => self::DEFAULT_CHANNEL,
19
        'globalLevel' => Logger::DEBUG,
20
        'handlers'    => [],
21
    ];
22
23 21
    public function getDefault(): string
24
    {
25 21
        return $this->config['default'] ?? self::DEFAULT_CHANNEL;
26
    }
27
28 21
    public function getEventLevel(): int
29
    {
30 21
        return $this->config['globalLevel'] ?? Logger::DEBUG;
31
    }
32
33
    /**
34
     * @return \Generator<int, Autowire>
35
     */
36 21
    public function getHandlers(string $channel): \Generator
37
    {
38 21
        if (empty($this->config['handlers'][$channel])) {
39 14
            return;
40
        }
41
42 7
        foreach ($this->config['handlers'][$channel] as $handler) {
43 7
            if (\is_object($handler) && !$handler instanceof Autowire) {
44 2
                yield $handler;
45 2
                continue;
46
            }
47
48 5
            $wire = $this->wire($handler);
49 5
            if (\is_null($wire)) {
50 1
                throw new ConfigException(\sprintf('Invalid handler definition for channel `%s`.', $channel));
51
            }
52
53 4
            yield $wire;
54
        }
55
    }
56
57 20
    public function getProcessors(string $channel): \Generator
58
    {
59 20
        if (empty($this->config['processors'][$channel])) {
60 13
            return;
61
        }
62
63 7
        foreach ($this->config['processors'][$channel] as $processor) {
64 7
            if (\is_object($processor) && !$processor instanceof Autowire) {
65 3
                yield $processor;
66 3
                continue;
67
            }
68
69 4
            $wire = $this->wire($processor);
70 4
            if (\is_null($wire)) {
71 1
                throw new ConfigException(\sprintf('Invalid processor definition for channel `%s`.', $channel));
72
            }
73
74 3
            yield $wire;
75
        }
76
    }
77
78 9
    private function wire(Autowire|string|array $definition): ?Autowire
79
    {
80 9
        if ($definition instanceof Autowire) {
0 ignored issues
show
introduced by
$definition is never a sub-type of Spiral\Core\Container\Autowire.
Loading history...
81 1
            return $definition;
82
        }
83
84 8
        if (\is_string($definition)) {
0 ignored issues
show
introduced by
The condition is_string($definition) is always false.
Loading history...
85 2
            return new Autowire($definition);
86
        }
87
88 6
        if (isset($definition['class'])) {
89 4
            return new Autowire($definition['class'], $definition['options'] ?? []);
90
        }
91
92 2
        return null;
93
    }
94
}
95