MonologFactory::__callStatic()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WShafer\PSR11MonoLog;
6
7
use Psr\Container\ContainerInterface;
8
use WShafer\PSR11MonoLog\Exception\InvalidContainerException;
9
10
class MonologFactory
11
{
12
    protected static $channelChanger;
13
14
    protected static $channelName = 'default';
15
16 2
    public function __invoke(ContainerInterface $container)
17
    {
18 2
        $channelChanger = static::getChannelChanger($container);
19 2
        $configKey = static::getChannelName();
20 2
        return $channelChanger->get($configKey);
21
    }
22
23 2
    public static function __callStatic($name, $arguments)
24
    {
25
        if (
26 2
            empty($arguments[0])
27 2
            || !$arguments[0] instanceof ContainerInterface
28
        ) {
29 1
            throw new InvalidContainerException(
30 1
                'Argument 0 must be an instance of a PSR-11 container'
31
            );
32
        }
33
34 1
        $factory = new static();
35 1
        $factory->setChannelName($name);
36 1
        return $factory($arguments[0]);
37
    }
38
39
    /**
40
     * @return string
41
     */
42 2
    public static function getChannelName(): string
43
    {
44 2
        return self::$channelName;
45
    }
46
47
    /**
48
     * @param string $channelName
49
     */
50 1
    public static function setChannelName(string $channelName)
51
    {
52 1
        self::$channelName = $channelName;
53 1
    }
54
55
    /**
56
     * @param ContainerInterface $container
57
     *
58
     * @return mixed
59
     */
60 3
    public static function getChannelChanger(ContainerInterface $container): ChannelChanger
61
    {
62
        // @codeCoverageIgnoreStart
63
        if (!static::$channelChanger) {
64
            $factory = new ChannelChangerFactory();
65
            static::setChannelChanger($factory($container));
66
        }
67
        // @codeCoverageIgnoreEnd
68
69 3
        return static::$channelChanger;
70
    }
71
72
    /**
73
     * @param ChannelChanger $channelChanger
74
     */
75 4
    public static function setChannelChanger(ChannelChanger $channelChanger)
76
    {
77 4
        static::$channelChanger = $channelChanger;
78 4
    }
79
}
80