Completed
Pull Request — master (#62)
by Tim
28:18
created

LoggerFactory::factory()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 37
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
ccs 0
cts 22
cp 0
rs 8.5806
cc 4
eloc 17
nc 6
nop 1
crap 20
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Configuration\LoggerFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cli\Configuration;
22
23
use TechDivision\Import\Configuration\LoggerConfigurationInterface;
24
25
/**
26
 * Logger factory implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-cli-simple
32
 * @link      http://www.techdivision.com
33
 */
34
class LoggerFactory
35
{
36
37
    /**
38
     * Creates a new logger instance based on the passed logger configuration.
39
     *
40
     * @param \TechDivision\Import\Configuration\LoggerConfigurationInterface $loggerConfiguration The logger configuration
41
     *
42
     * @return \Psr\Log\LoggerInterface The logger instance
43
     */
44
    public static function factory(LoggerConfigurationInterface $loggerConfiguration)
45
    {
46
47
        // initialize the processors
48
        $processors = array();
49
        /** @var \TechDivision\Import\Configuration\Logger\ProcessorConfigurationInterface $processorConfiguration */
50
        foreach ($loggerConfiguration->getProcessors() as $processorConfiguration) {
51
            $reflectionClass = new \ReflectionClass($processorConfiguration->getType());
52
            $processors[] = $reflectionClass->newInstanceArgs($processorConfiguration->getParams());
53
        }
54
55
        // initialize the handlers
56
        $handlers = array();
57
        /** @var \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration */
58
        foreach ($loggerConfiguration->getHandlers() as $handlerConfiguration) {
59
            // initialize the handler node
60
            $reflectionClass = new \ReflectionClass($handlerConfiguration->getType());
61
            $handler = $reflectionClass->newInstanceArgs($handlerConfiguration->getParams());
62
63
            // if we've a formatter, initialize the formatter also
64
            if ($formatterConfiguration = $handlerConfiguration->getFormatter()) {
65
                $reflectionClass = new \ReflectionClass($formatterConfiguration->getType());
66
                $handler->setFormatter($reflectionClass->newInstanceArgs($formatterConfiguration->getParams()));
67
            }
68
69
            // add the handler
70
            $handlers[] = $handler;
71
        }
72
73
        // prepare the logger params
74
        $loggerParams = array($loggerConfiguration->getChannelName(), $handlers, $processors);
75
        $loggerParams = array_merge($loggerParams, $loggerConfiguration->getParams());
76
77
        // initialize the logger instance itself
78
        $reflectionClass = new \ReflectionClass($loggerConfiguration->getType());
79
        return $reflectionClass->newInstanceArgs($loggerParams);
80
    }
81
}
82