Completed
Push — master ( 596585...dae15c )
by Juuso
04:50
created

HandlerFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 10.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 7
loc 67
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 21 3
A validateHandler() 7 7 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
declare(strict_types=1);
4
5
namespace leinonen\Yii2Monolog\Factories;
6
7
use Monolog\Handler\HandlerInterface;
8
use Monolog\Formatter\FormatterInterface;
9
10
class HandlerFactory
11
{
12
    /**
13
     * @var GenericStrategyBasedFactory
14
     */
15
    private $genericFactory;
16
17
    /**
18
     * Initializes a new HandlerFactory.
19
     *
20
     * @param GenericStrategyBasedFactory $genericFactory
21
     */
22 4
    public function __construct(GenericStrategyBasedFactory $genericFactory)
23
    {
24 4
        $this->genericFactory = $genericFactory;
25 4
    }
26
27
    /**
28
     * Returns an instance of the given handler class with the given configuration.
29
     *
30
     * @param string $handlerClass
31
     * @param array $config
32
     * @param FormatterInterface|null $formatter
33
     * @param array|callable[] $processors
34
     *
35
     * @return HandlerInterface
36
     *
37
     * @throws \yii\base\InvalidConfigException
38
     * @throws \InvalidArgumentException
39
     */
40 4
    public function make(
41
        string $handlerClass,
42
        array $config = [],
43
        FormatterInterface $formatter = null,
44
        array $processors = []
45
    ): HandlerInterface {
46 4
        $this->validateHandler($handlerClass);
47
48
        /** @var HandlerInterface $handler */
49 3
        $handler = $this->genericFactory->makeWithStrategy($handlerClass, $config);
50
51 3
        if ($formatter !== null) {
52 1
            $handler->setFormatter($formatter);
53
        }
54
55 3
        foreach ($processors as $processor) {
56 1
            $handler->pushProcessor($processor);
57
        }
58
59 3
        return $handler;
60
    }
61
62
    /**
63
     * Validates the given handler class.
64
     *
65
     * @param string $handlerClass
66
     *
67
     * @throws \InvalidArgumentException
68
     */
69 4 View Code Duplication
    private function validateHandler(string $handlerClass): void
1 ignored issue
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...
70
    {
71 4
        $handlerInterface = HandlerInterface::class;
72 4
        if (! (new \ReflectionClass($handlerClass))->implementsInterface($handlerInterface)) {
73 1
            throw new \InvalidArgumentException("{$handlerClass} doesn't implement {$handlerInterface}");
74
        }
75 3
    }
76
}
77