Completed
Pull Request — master (#7)
by Jaap
01:42
created

ChannelChangerFactory::getConfigArray()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 9
cts 10
cp 0.9
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 10
nc 4
nop 1
crap 6.0359
1
<?php
2
3
namespace WShafer\PSR11MonoLog;
4
5
use Psr\Container\ContainerInterface;
6
use WShafer\PSR11MonoLog\Config\MainConfig;
7
use WShafer\PSR11MonoLog\Formatter\FormatterMapper;
8
use WShafer\PSR11MonoLog\Handler\HandlerMapper;
9
use WShafer\PSR11MonoLog\Processor\ProcessorMapper;
10
use WShafer\PSR11MonoLog\Service\FormatterManager;
11
use WShafer\PSR11MonoLog\Service\HandlerManager;
12
use WShafer\PSR11MonoLog\Service\ProcessorManager;
13
14
class ChannelChangerFactory
15
{
16
    protected $config = null;
17
18
    protected $handlerManager = null;
19
20
    protected $processManager = null;
21
22
    protected $formatterManager = null;
23
24 1
    public function __invoke(ContainerInterface $container)
25
    {
26 1
        $config = $this->getMainConfig($container);
27 1
        $handlerManager = $this->getHandlerManager($container);
28 1
        $processorManager = $this->getProcessorManager($container);
29
30 1
        return new ChannelChanger(
31 1
            $config,
32 1
            $handlerManager,
33 1
            $processorManager
34
        );
35
    }
36
37 7
    public function getMainConfig(ContainerInterface $container)
38
    {
39 7
        $config = $this->getConfigArray($container);
40 7
        return new MainConfig($config);
41
    }
42
43 7
    protected function getConfigArray(ContainerInterface $container)
44
    {
45
        // Symfony config is parameters. //
46 7
        if (method_exists($container, 'getParameter')
47 7
            && method_exists($container, 'hasParameter')
48 7
            && $container->hasParameter('monolog')
49
        ) {
50 1
            return ['monolog' => $container->getParameter('monolog')];
51
        }
52
53
        // Zend uses config key
54 6
        if ($container->has('config')) {
55 5
            return $container->get('config');
56
        }
57
58
        // Slim Config comes from "settings"
59 1
        if ($container->has('settings')) {
60 1
            return ['monolog' => $container->get('settings')['monolog']];
61
        }
62
63
        return [];
64
    }
65
66 2
    public function getHandlerManager(ContainerInterface $container)
67
    {
68 2
        $config = $this->getMainConfig($container);
69 2
        $this->handlerManager = new HandlerManager(
70 2
            $config,
71 2
            new HandlerMapper(),
72 2
            $container
73
        );
74
75 2
        $this->handlerManager->setFormatterManager($this->getFormatterManager($container));
76 2
        $this->handlerManager->setProcessorManager($this->getProcessorManager($container));
77 2
        return $this->handlerManager;
78
    }
79
80 3
    public function getFormatterManager(ContainerInterface $container)
81
    {
82 3
        $config = $this->getMainConfig($container);
83 3
        $this->formatterManager = new FormatterManager(
84 3
            $config,
85 3
            new FormatterMapper(),
86 3
            $container
87
        );
88
89 3
        return $this->formatterManager;
90
    }
91
92 3
    public function getProcessorManager(ContainerInterface $container)
93
    {
94 3
        $config = $this->getMainConfig($container);
95 3
        $this->processManager = new ProcessorManager(
96 3
            $config,
97 3
            new ProcessorMapper(),
98 3
            $container
99
        );
100
101 3
        return $this->processManager;
102
    }
103
}
104