Completed
Push — master ( efa1dd...4acd9a )
by Nikola
01:12
created

LoggerFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 0
loc 79
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 12 1
A createProcessor() 0 9 2
A createFormatter() 0 9 2
B createHandler() 0 23 6
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
use MonologFactory\Helper\GenericObjectFactory;
18
use MonologFactory\Helper\ObjectFactory;
19
20
class LoggerFactory
21
{
22
    /** @var ObjectFactory */
23
    private $objectFactory;
24
    
25 15
    public function __construct(ObjectFactory $objectFactory = null)
26
    {
27 15
        $this->objectFactory = $objectFactory ?? new GenericObjectFactory(new Cascader());
28 15
    }
29
30 21
    public function create(string $name, array $config = []): Logger
31
    {
32 21
        $config[LoggerConfig::NAME] = $name;
33 21
        $config = LoggerConfig::fromArray($config);
34
35 16
        return new Logger(
36 16
            $config->getName(),
37 16
            array_map([$this, 'createHandler'], $config->getHandlers()),
38 16
            array_map([$this, 'createProcessor'], $config->getProcessors()),
39 16
            $config->getTimezone()
40
        );
41
    }
42
43
    /**
44
     * @param HandlerInterface|HandlerConfig $handler
45
     * @return HandlerInterface
46
     */
47 14
    protected function createHandler($handler): HandlerInterface
48
    {
49 14
        if ($handler instanceof HandlerInterface) {
50 4
            return $handler;
51
        }
52
53 13
        $handlerConfig = $handler;
54
55 13
        $handler = $this->objectFactory->create($handlerConfig->getName(), $handlerConfig->getParameters());
56
57 13
        if ($handler instanceof ProcessableHandlerInterface) {
58 13
            foreach (array_reverse($handlerConfig->getProcessors()) as $processorConfig) {
59 1
                $handler->pushProcessor($this->createProcessor($processorConfig));
60
            }
61
        }
62
63 13
        if ($handler instanceof FormattableHandlerInterface && null !== ($formatterConfig = $handlerConfig->getFormatter())) {
64 6
            $handler->setFormatter($this->createFormatter($formatterConfig));
65
        }
66
67
        /** @noinspection PhpIncompatibleReturnTypeInspection */
68 13
        return $handler;
69
    }
70
71
    /**
72
     * @param callable|ProcessorConfig $processor
73
     * @return callable
74
     */
75 10
    protected function createProcessor($processor): callable
76
    {
77 10
        if (is_callable($processor)) {
78 5
            return $processor;
79
        }
80
81
        /** @noinspection PhpIncompatibleReturnTypeInspection */
82 6
        return $this->objectFactory->create($processor->getName(), $processor->getParameters());
83
    }
84
85
    /**
86
     * @param FormatterInterface|FormatterConfig $formatter
87
     * @return FormatterInterface
88
     */
89 6
    protected function createFormatter($formatter): FormatterInterface
90
    {
91 6
        if ($formatter instanceof FormatterInterface) {
92 4
            return $formatter;
93
        }
94
95
        /** @noinspection PhpIncompatibleReturnTypeInspection */
96 2
        return $this->objectFactory->create($formatter->getName(), $formatter->getParameters());
97
    }
98
}
99