Instances   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
c 1
b 0
f 1
dl 0
loc 46
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A reload() 0 10 3
A configure() 0 6 3
1
<?php
2
/**
3
 * Instances manager
4
 * User: moyo
5
 * Date: 22/11/2017
6
 * Time: 10:56 AM
7
 */
8
9
namespace Carno\Log;
10
11
use function Carno\Config\conf;
12
use Carno\Container\DI;
13
use Psr\Log\LoggerInterface;
14
15
class Instances
16
{
17
    /**
18
     * @var Logger[]
19
     */
20
    private static $loggers = [];
21
22
    /**
23
     * @var Configure
24
     */
25
    private static $configured = null;
26
27
    /**
28
     * @param string $scene
29
     * @return LoggerInterface
30
     */
31
    public static function get(string $scene) : LoggerInterface
32
    {
33
        return self::$loggers[$scene] ?? self::$loggers[$scene] = new Logger($scene, self::configure());
34
    }
35
36
    /**
37
     * @param Configure $configure
38
     */
39
    public static function reload(Configure $configure) : void
40
    {
41
        if (self::$configured) {
42
            self::$configured->unload();
43
        }
44
45
        self::$configured = $configure;
46
47
        foreach (self::$loggers as $logger) {
48
            $logger->reconfigure($configure);
49
        }
50
    }
51
52
    /**
53
     * @return Configure
54
     */
55
    private static function configure() : Configure
56
    {
57
        return self::$configured ?? self::$configured = new Configure(
58
            conf(),
59
            DI::has(Environment::class) ? DI::get(Environment::class) : new Environment(),
60
            DI::has(Connections::class) ? DI::get(Connections::class) : null
61
        );
62
    }
63
}
64