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

LoggerUtil::log()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 5.667

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 2
cts 6
cp 0.3333
crap 5.667
rs 10
c 0
b 0
f 0
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