Passed
Push — master ( c9be78...243230 )
by Nico
22:39
created

LoggerUtil   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 61.53%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 59
ccs 16
cts 26
cp 0.6153
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A doLog() 0 4 1
A __construct() 0 1 1
A init() 0 10 2
A log() 0 10 3
A checkDoLog() 0 7 1
A logf() 0 6 1
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 Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use RuntimeException;
11
use Stu\Module\Config\StuConfigInterface;
12
13
final class LoggerUtil implements LoggerUtilInterface
14
{
15
    private ?Logger $logger = null;
16
17
    private int $level;
18
19
    private bool $doLog = false;
20
21 11
    public function __construct(private StuConfigInterface $stuConfig) {}
22
23 2
    #[Override]
24
    public function init(string $channel = 'stu', int $level = LoggerEnum::LEVEL_INFO): void
0 ignored issues
show
Bug introduced by
The type Stu\Module\Logging\LoggerEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
    {
26 2
        $this->level = $level;
27
28 2
        if ($this->checkDoLog()) {
29 2
            $this->logger = new Logger($channel);
30 2
            $this->logger->pushHandler(
31 2
                new StreamHandler(
32 2
                    $this->stuConfig->getDebugSettings()->getLogfilePath()
33 2
                ),
34 2
            );
35
        }
36
    }
37
38 2
    private function checkDoLog(): bool
39
    {
40 2
        $threshold = $this->stuConfig->getDebugSettings()->getLoglevel();
41
42 2
        $this->doLog = $threshold <= $this->level;
43
44 2
        return $this->doLog;
45
    }
46
47
    #[Override]
48
    public function doLog(): bool
49
    {
50
        return $this->doLog;
51
    }
52
53 188
    #[Override]
54
    public function log(string $message): void
55
    {
56 188
        if ($this->doLog) {
57
            if ($this->logger === null) {
58
                throw new RuntimeException('logger has not been initialized');
59
            }
60
61
            $method = LoggerEnum::LEVEL_METHODS[$this->level];
62
            $this->logger->$method($message);
63
        }
64
    }
65
66
    #[Override]
67
    public function logf(string $information, ...$args): void
68
    {
69
        $this->log(vsprintf(
70
            $information,
71
            $args
72
        ));
73
    }
74
}
75