Completed
Push — add-native-mailer-handler ( c1b631 )
by
unknown
07:21
created

GenericLogHandlerFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 28.85 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 15
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A factory() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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