Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

ErrorLogHandlerFactory::factory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Loggers\ErrorLogHandlerFactory
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 2019 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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Loggers;
22
23
use Monolog\Handler\ErrorLogHandler;
24
use TechDivision\Import\Utils\ConfigurationUtil;
25
use TechDivision\Import\Utils\ConfigurationKeys;
26
use TechDivision\Import\ConfigurationInterface;
27
use TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface;
28
29
/**
30
 * Error Log Handler factory implementation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2019 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
36
 * @link      http://www.techdivision.com
37
 */
38
class ErrorLogHandlerFactory implements HandlerFactoryInterface
39
{
40
41
    /**
42
     * The log level to use.
43
     *
44
     * @var string
45
     */
46
    protected $defaultLogLevel;
47
48
    /**
49
     * Initialize the processor with the actual configuration instance
50
     *
51
     * @param \TechDivision\Import\ConfigurationInterface $configuration The actual configuration instance
52
     */
53
    public function __construct(ConfigurationInterface $configuration)
54
    {
55
        $this->defaultLogLevel = $configuration->getLogLevel();
56
    }
57
58
    /**
59
     * Creates a new formatter instance based on the passed configuration.
60
     *
61
     * @param \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration The handler configuration
62
     *
63
     * @return \Monolog\Handler\HandlerInterface The handler instance
64
     */
65
    public function factory(HandlerConfigurationInterface $handlerConfiguration)
66
    {
67
68
        // load the params
69
        $params = $handlerConfiguration->getParams();
70
71
        // set the default log level, if not already set explicitly
72
        if (!isset($params[ConfigurationKeys::LEVEL])) {
73
            $params[ConfigurationKeys::LEVEL] = $this->defaultLogLevel;
74
        }
75
76
        // create and return the handler instance
77
        $reflectionClass = new \ReflectionClass(ErrorLogHandler::class);
78
        return $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $params));
79
    }
80
}
81