Completed
Push — master ( 4d8e99...b81835 )
by Maxim
03:07
created

LoggerService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 5 1
A get() 0 7 2
1
<?php
2
3
namespace WebComplete\mvc\logger;
4
5
use Monolog\Logger;
6
use Psr\Log\LoggerInterface;
7
use WebComplete\core\utils\container\ContainerInterface;
8
use WebComplete\mvc\ApplicationConfig;
9
10
class LoggerService
11
{
12
13
    /**
14
     * @var ContainerInterface
15
     */
16
    protected $container;
17
    /**
18
     * @var ApplicationConfig
19
     */
20
    protected $config;
21
    protected $scope = [];
22
23
    /**
24
     * @param ApplicationConfig $config
25
     * @param ContainerInterface $container
26
     */
27
    public function __construct(ApplicationConfig $config, ContainerInterface $container)
28
    {
29
        $this->config = $config;
30
        $this->container = $container;
31
    }
32
33
    /**
34
     * @param string $name
35
     *
36
     * @return LoggerInterface
37
     */
38
    public function get(string $name): LoggerInterface
39
    {
40
        if (!isset($this->scope[$name])) {
41
            $this->scope[$name] = $this->create($name);
42
        }
43
44
        return $this->scope[$name];
45
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @return LoggerInterface
51
     */
52
    protected function create(string $name): LoggerInterface
53
    {
54
        $configLogger = $this->config['logger'];
55
        $handlers = (array)($configLogger[$name] ?? []);
56
        return new Logger($name, $handlers);
57
    }
58
}
59