Passed
Push — master ( 9cac2c...9221f6 )
by K
01:48
created

FileLogger::createLogMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace PerfectApp\Logger;
4
5
use InvalidArgumentException;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LogLevel;
8
use Stringable;
9
10
class FileLogger implements LoggerInterface
11
{
12 10
    public function __construct(private readonly string $filePath)
13
    {
14 10
    }
15
16 10
    public function log(mixed $level, Stringable|string $message, array $context = []): void
17
    {
18 10
        if (!in_array($level, [
19 10
            LogLevel::EMERGENCY,
20 10
            LogLevel::ALERT,
21 10
            LogLevel::CRITICAL,
22 10
            LogLevel::ERROR,
23 10
            LogLevel::WARNING,
24 10
            LogLevel::NOTICE,
25 10
            LogLevel::INFO,
26 10
            LogLevel::DEBUG
27 10
        ])) {
28 1
            throw new InvalidArgumentException('Invalid log level.');
29
        }
30
31 9
        $logMessage = $this->createLogMessage($level, $message, $context);
32 9
        file_put_contents($this->filePath, $logMessage, FILE_APPEND);
33
    }
34
35 9
    private function createLogMessage(mixed $level, Stringable|string $message, array $context): string
36
    {
37 9
        return sprintf(
38 9
            '[%s] %s %s %s' . PHP_EOL,
39 9
            date('Y-m-d H:i:s'),
40 9
            strtoupper($level),
41 9
            $message,
42 9
            json_encode($context)
43 9
        );
44
    }
45
46 1
    public function emergency(Stringable|string $message, array $context = []): void
47
    {
48 1
        $this->log(LogLevel::EMERGENCY, $message, $context);
49
    }
50
51 1
    public function alert(Stringable|string $message, array $context = []): void
52
    {
53 1
        $this->log(LogLevel::ALERT, $message, $context);
54
    }
55
56 1
    public function critical(Stringable|string $message, array $context = []): void
57
    {
58 1
        $this->log(LogLevel::CRITICAL, $message, $context);
59
    }
60
61 1
    public function error(Stringable|string $message, array $context = []): void
62
    {
63 1
        $this->log(LogLevel::ERROR, $message, $context);
64
    }
65
66 1
    public function warning(Stringable|string $message, array $context = []): void
67
    {
68 1
        $this->log(LogLevel::WARNING, $message, $context);
69
    }
70
71 1
    public function notice(Stringable|string $message, array $context = []): void
72
    {
73 1
        $this->log(LogLevel::NOTICE, $message, $context);
74
    }
75
76 1
    public function info(Stringable|string $message, array $context = []): void
77
    {
78 1
        $this->log(LogLevel::INFO, $message, $context);
79
    }
80
81 1
    public function debug(Stringable|string $message, array $context = []): void
82
    {
83 1
        $this->log(LogLevel::DEBUG, $message, $context);
84
    }
85
}
86