Passed
Push — dev ( aca482...6fd7cc )
by Nico
08:12
created

LoggerUtil   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 45.45%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 49
ccs 10
cts 22
cp 0.4545
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A doLog() 0 3 1
A __construct() 0 4 1
A init() 0 9 2
A checkDoLog() 0 7 1
A log() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Logging;
6
7
use Monolog\Handler\StreamHandler;
8
use Monolog\Logger;
9
use Noodlehaus\ConfigInterface;
10
11
final class LoggerUtil implements LoggerUtilInterface
12
{
13
    private ConfigInterface $config;
14
15
    private $logger;
16
17
    private int $level;
18
19
    private bool $doLog;
20
21 1
    public function __construct(ConfigInterface $config)
22
    {
23 1
        $this->config = $config;
24 1
        $this->doLog = false;
25
    }
26
27 1
    public function init(string $channel = 'stu', int $level = LoggerEnum::LEVEL_INFO): void
28
    {
29 1
        $this->level = $level;
30
31 1
        if ($this->checkDoLog()) {
32
            $this->logger = new Logger($channel);
33
            $this->logger->pushHandler(
34
                new StreamHandler(
35
                    $this->config->get('debug.logfile_path')
36
                ),
37
            );
38
        }
39
    }
40
41 1
    private function checkDoLog(): bool
42
    {
43 1
        $threshold = (int) $this->config->get('debug.loglevel');
44
45 1
        $this->doLog = $threshold <= $this->level;
46
47 1
        return $this->doLog;
48
    }
49
50
    public function doLog(): bool
51
    {
52
        return $this->doLog;
53
    }
54
55
    public function log(string $message): void
56
    {
57
        if ($this->doLog) {
58
            $method = LoggerEnum::LEVEL_METHODS[$this->level];
59
            $this->logger->$method($message);
60
        }
61
    }
62
}
63