Passed
Push — master ( 1719b9...7be248 )
by Tim
03:09
created

LoggerFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 66
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguration() 0 3 1
A createLoggers() 0 31 2
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Logger\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\Logger;
22
23
use Monolog\Logger;
24
use Monolog\Handler\ErrorLogHandler;
25
use Doctrine\Common\Collections\ArrayCollection;
26
use TechDivision\Import\Utils\LoggerKeys;
27
use TechDivision\Import\ConfigurationInterface;
28
29
/**
30
 * The logger factory implementation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-cli-simple
36
 * @link      http://www.techdivision.com
37
 */
38
class LoggerFactory implements LoggerFactoryInterface
39
{
40
41
    /**
42
     * The actual configuration instance.
43
     *
44
     * @var \TechDivision\Import\ConfigurationInterface
45
     */
46
    protected $configuration;
47
48
    /**
49
     * Initialize the factory with the actual configuration instance.
50
     *
51
     * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration with the data to create the loggers with
52
     */
53
    public function __construct(ConfigurationInterface $configuration)
54
    {
55
        $this->configuration = $configuration;
56
    }
57
58
    /**
59
     * Returns the actual configuration instance.
60
     *
61
     * @return \TechDivision\Import\ConfigurationInterface The configuration instance
62
     */
63
    protected function getConfiguration()
64
    {
65
        return $this->configuration;
66
    }
67
68
    /**
69
     * Create's and return's the loggers to use.
70
     *
71
     * @return \Doctrine\Common\Collections\ArrayCollection The array with the initialized loggers
72
     */
73
    public function createLoggers()
74
    {
75
76
        // load the configuration instance
77
        $configuration = $this->getConfiguration();
78
79
        // initialize the collection for the loggers
80
        $loggers = new ArrayCollection();
81
82
        // initialize the default system logger
83
        $systemLogger = new Logger('techdivision/import');
84
        $systemLogger->pushHandler(
85
            new ErrorLogHandler(
86
                ErrorLogHandler::OPERATING_SYSTEM,
87
                $configuration->getLogLevel()
0 ignored issues
show
Bug introduced by
$configuration->getLogLevel() of type string is incompatible with the type integer expected by parameter $level of Monolog\Handler\ErrorLogHandler::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
                /** @scrutinizer ignore-type */ $configuration->getLogLevel()
Loading history...
88
            )
89
        );
90
91
        // add it to the array
92
        $loggers->set(LoggerKeys::SYSTEM, $systemLogger);
93
94
        // append the configured loggers or override the default one
95
        foreach ($configuration->getLoggers() as $loggerConfiguration) {
96
            // load the factory class that creates the logger instance
97
            $loggerFactory = $loggerConfiguration->getFactory();
98
            // create the logger instance and add it to the available loggers
99
            $loggers->set($loggerConfiguration->getName(), $loggerFactory::factory($configuration, $loggerConfiguration));
100
        }
101
102
        // return the collection with the initialized loggers
103
        return $loggers;
104
    }
105
}
106