Passed
Push — master ( 8cd431...bc71f5 )
by Janko
09:12
created

LoggerUtil::log()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.944

Importance

Changes 0
Metric Value
eloc 5
nc 3
nop 1
dl 0
loc 9
c 0
b 0
f 0
cc 3
ccs 2
cts 5
cp 0.4
crap 4.944
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Logging;
6
7
use Monolog\Logger;
8
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...
9
use RuntimeException;
10
use Stu\Module\Config\StuConfigInterface;
11
12
final class LoggerUtil implements LoggerUtilInterface
13
{
14
    private ?Logger $logger = null;
15
16
    private LogLevelEnum $level;
17
18
    private bool $doLog = false;
19
20 11
    public function __construct(private StuConfigInterface $stuConfig) {}
21
22 2
    #[Override]
23
    public function init(string $channel = 'stu', LogLevelEnum $level = LogLevelEnum::INFO): void
24
    {
25 2
        $this->level = $level;
26
27 2
        if ($this->checkDoLog()) {
28 2
            $this->logger = StuLogger::getLogger(LogTypeEnum::DEFAULT);
29
        }
30
    }
31
32 2
    private function checkDoLog(): bool
33
    {
34 2
        $threshold = $this->stuConfig->getDebugSettings()->getLoglevel();
35
36 2
        $this->doLog = $threshold <= $this->level->value;
37
38 2
        return $this->doLog;
39
    }
40
41
    #[Override]
42
    public function doLog(): bool
43
    {
44
        return $this->doLog;
45
    }
46
47 8
    #[Override]
48
    public function log(string $message): void
49
    {
50 8
        if ($this->doLog) {
51
            if ($this->logger === null) {
52
                throw new RuntimeException('logger has not been initialized');
53
            }
54
55
            $this->level->log($message, $this->logger);
56
        }
57
    }
58
59
    #[Override]
60
    public function logf(string $information, ...$args): void
61
    {
62
        $this->log(vsprintf(
63
            $information,
64
            $args
65
        ));
66
    }
67
}
68