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

MonologFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 0
loc 69
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 1
A __callStatic() 0 14 3
A getChannelName() 0 4 1
A setChannelName() 0 4 1
A getChannelChanger() 0 11 2
A setChannelChanger() 0 4 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
}