Completed
Push — master ( a1d3b0...f47b8c )
by Nikola
02:43
created

LoggerFactory::createHandler()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 11
cp 0.9091
rs 8.9457
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 6.027
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MonologFactory;
6
7
use Cascader\Cascader;
8
use Monolog\Handler\FormattableHandlerInterface;
9
use Monolog\Handler\ProcessableHandlerInterface;
10
use Monolog\Logger;
11
use Monolog\Handler\HandlerInterface;
12
use Monolog\Formatter\FormatterInterface;
13
use MonologFactory\Config\FormatterConfig;
14
use MonologFactory\Config\HandlerConfig;
15
use MonologFactory\Config\LoggerConfig;
16
use MonologFactory\Config\ProcessorConfig;
17
18
class LoggerFactory
19
{
20
    /** @var Cascader */
21
    private $cascader;
22
23 16
    public function create(string $name, array $config = []): Logger
24
    {
25 16
        $config[LoggerConfig::NAME] = $name;
26 16
        $config = LoggerConfig::fromArray($config);
27
28 11
        return new Logger(
29 11
            $config->getName(),
30 11
            array_map([$this, 'createHandler'], $config->getHandlers()),
31 11
            array_map([$this, 'createProcessor'], $config->getProcessors()),
32 11
            $config->getTimezone()
33
        );
34
    }
35
36
    /**
37
     * @param HandlerInterface|HandlerConfig $handler
38
     * @return HandlerInterface
39
     */
40 9
    protected function createHandler($handler): HandlerInterface
41
    {
42 9
        if ($handler instanceof HandlerInterface) {
43 3
            return $handler;
44
        }
45
46 9
        $handlerConfig = $handler;
47
48 9
        $handler = $this->createObject($handlerConfig->getName(), $handlerConfig->getParameters());
49
50 9
        if ($handler instanceof ProcessableHandlerInterface) {
51 9
            foreach (array_reverse($handlerConfig->getProcessors()) as $processorConfig) {
52
                $handler->pushProcessor($this->createProcessor($processorConfig));
53
            }
54
        }
55
56 9
        if ($handler instanceof FormattableHandlerInterface && null !== ($formatterConfig = $handlerConfig->getFormatter())) {
57 6
            $handler->setFormatter($this->createFormatter($formatterConfig));
58
        }
59
60 9
        return $handler;
61
    }
62
63
    /**
64
     * @param callable|ProcessorConfig $processor
65
     * @return callable
66
     */
67 8
    protected function createProcessor($processor): callable
68
    {
69 8
        if (is_callable($processor)) {
70 3
            return $processor;
71
        }
72
73 5
        return $this->createObject($processor->getName(), $processor->getParameters());
74
    }
75
76
    /**
77
     * @param FormatterInterface|FormatterConfig $formatter
78
     * @return FormatterInterface
79
     */
80 6
    protected function createFormatter($formatter): FormatterInterface
81
    {
82 6
        if ($formatter instanceof FormatterInterface) {
83 4
            return $formatter;
84
        }
85
86 2
        return $this->createObject($formatter->getName(), $formatter->getParameters());
87
    }
88
89
    /**
90
     * @param string $className
91
     * @param array $creationOptions
92
     * @return mixed
93
     */
94 9
    protected function createObject(string $className, array $creationOptions)
95
    {
96 9
        return $this->getCascader()->create($className, $creationOptions);
97
    }
98
99 9
    final protected function getCascader(): Cascader
100
    {
101 9
        if (null === $this->cascader) {
102 9
            $this->cascader = new Cascader();
103
        }
104
105 9
        return $this->cascader;
106
    }
107
}
108