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 TechDivision\Import\Utils\ConfigurationUtil; |
24
|
|
|
use TechDivision\Import\Utils\ConfigurationKeys; |
25
|
|
|
use TechDivision\Import\Configuration\ConfigurationInterface; |
26
|
|
|
use TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Error Log Handler factory implementation. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class GenericLogHandlerFactory implements HandlerFactoryInterface |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The log level to use. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $defaultLogLevel; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The log level to use. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $handlerClassName; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialize the processor with the actual configuration instance |
56
|
|
|
* |
57
|
|
|
* @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The actual configuration instance |
58
|
|
|
* @param string $handlerClassName |
59
|
|
|
*/ |
60
|
|
|
public function __construct(ConfigurationInterface $configuration, $handlerClassName) |
61
|
|
|
{ |
62
|
|
|
$this->defaultLogLevel = $configuration->getLogLevel(); |
63
|
|
|
$this->handlerClassName = $handlerClassName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates a new formatter instance based on the passed configuration. |
68
|
|
|
* |
69
|
|
|
* @param \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration The handler configuration |
70
|
|
|
* |
71
|
|
|
* @return \Monolog\Handler\HandlerInterface The handler instance |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function factory(HandlerConfigurationInterface $handlerConfiguration) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
// load the params |
77
|
|
|
$params = $handlerConfiguration->getParams(); |
78
|
|
|
|
79
|
|
|
// set the default log level, if not already set explicitly |
80
|
|
|
if (!isset($params[ConfigurationKeys::LEVEL])) { |
81
|
|
|
$params[ConfigurationKeys::LEVEL] = $this->defaultLogLevel; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// create and return the handler instance |
85
|
|
|
$reflectionClass = new \ReflectionClass($this->handlerClassName); |
86
|
|
|
return $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $params)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.