Completed
Pull Request — master (#4)
by Westin
08:56
created

MonologFactory::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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