Passed
Push — dev ( 6a73c6...5c2643 )
by Janko
07:00
created

StuLogger::setMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Logging;
4
5
use Monolog\Handler\RotatingFileHandler;
6
use Monolog\Handler\StreamHandler;
7
use Monolog\Level;
8
use Monolog\Logger;
9
use Stu\Config\Init;
10
use Stu\Module\Config\StuConfigInterface;
11
12
class StuLogger
13
{
14
    /** @var array<string, Logger> */
15
    private static array $loggers = [];
16
17
    private static ?Logger $loggerMock = null;
18
19 4
    public static function log(string $message, LogTypeEnum $type = LogTypeEnum::DEFAULT): void
20
    {
21 4
        LogLevelEnum::INFO->log($message, self::getLogger($type));
22
    }
23
24
    /** @param string|int|float $args */
25
    public static function logf(string $information, ...$args): void
26
    {
27
        self::log(vsprintf(
28
            $information,
29
            $args
30
        ));
31
    }
32
33 6
    public static function getLogger(LogTypeEnum $type): Logger
34
    {
35 6
        if (self::$loggerMock !== null) {
36 4
            return self::$loggerMock;
37
        }
38
39 2
        if (!array_key_exists($type->value, self::$loggers)) {
40
41 1
            $logPath = $type->getLogfilePath(Init::getContainer()->get(StuConfigInterface::class)
42 1
                ->getDebugSettings()->getLoggingSettings()->getLogDirectory());
43
44 1
            $logger = new Logger($type->value);
45
46 1
            if ($type->isRotating()) {
47 1
                $logger->pushHandler(
48 1
                    new RotatingFileHandler($logPath, 10, Level::Info)
49 1
                );
50
            } else {
51 1
                $logger->pushHandler(
52 1
                    new StreamHandler(
53 1
                        $logPath
54 1
                    ),
55 1
                );
56
            }
57
58 1
            self::$loggers[$type->value] = $logger;
59
        }
60
61 2
        return self::$loggers[$type->value];
62
    }
63
64 13
    public static function setMock(?Logger $mock): void
65
    {
66 13
        self::$loggerMock = $mock;
67
    }
68
}
69