Formatter::__invoke()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
3
namespace Dafiti\Silex\Log\Factory;
4
5
class Formatter extends AbstractFactory
6
{
7
    public function __invoke(array $args)
8
    {
9
        $this->args = $args;
10
11
        if (!isset($this->args['class']) || empty($this->args['class'])) {
12
            throw new \OutOfBoundsException('You should define key class to create a log formatter');
13
        }
14
15
        $class = new \ReflectionClass($args['class']);
16
        $params = [];
17
18
        if ($constructor = $class->getConstructor()) {
19
            $params = $this->getParams($constructor);
20
        }
21
22
        $object = $class->newInstanceArgs($params);
23
24
        if (!$object instanceof \Monolog\Formatter\FormatterInterface) {
25
            throw new \InvalidArgumentException('Class must be instance of "\Monolog\Formatter\FormatterInterface"');
26
        }
27
28
        return $object;
29
    }
30
}
31