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

FormatterFactory::validateFormatter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 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