Test Setup Failed
Pull Request — master (#15)
by
unknown
08:18
created

FormatterFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 6 1
A validateFormatter() 0 7 2
1
<?php
2
3
namespace bessonov87\Yii2Monolog\Factories;
4
5
use Monolog\Formatter\FormatterInterface;
6
7
class FormatterFactory
8
{
9
    /**
10
     * @var GenericStrategyBasedFactory
11
     */
12
    private $genericFactory;
13
14
    /**
15
     * Initializes a new FormatterFactory.
16
     *
17
     * @param GenericStrategyBasedFactory $genericFactory
18
     */
19 11
    public function __construct(GenericStrategyBasedFactory $genericFactory)
20
    {
21 11
        $this->genericFactory = $genericFactory;
22 11
    }
23
24
    /**
25
     * Makes a new Formatter with given formatter class and config.
26
     *
27
     * @param string $formatterClass
28
     * @param array $config
29
     *
30
     * @return FormatterInterface
31
     * @throws \InvalidArgumentException
32
     *
33
     * @throws \yii\base\InvalidConfigException
34
     */
35 8
    public function make(string $formatterClass, array $config = []): FormatterInterface
36
    {
37 8
        $this->validateFormatter($formatterClass);
38
39 7
        return $this->genericFactory->makeWithStrategy($formatterClass, $config);
40
    }
41
42
    /**
43
     * Validates the given handler class.
44
     *
45
     * @param string $formatterClass
46
     *
47
     * @throws \InvalidArgumentException
48
     */
49 8
    private function validateFormatter(string $formatterClass)
50
    {
51 8
        $formatterInterface = FormatterInterface::class;
52 8
        if (! (new \ReflectionClass($formatterClass))->implementsInterface($formatterInterface)) {
53 1
            throw new \InvalidArgumentException("{$formatterClass} doesn't implement {$formatterInterface}");
54
        }
55 7
    }
56
}
57