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

FileLogger   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 27
c 2
b 0
f 0
dl 0
loc 74
ccs 40
cts 40
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A error() 0 3 1
A notice() 0 3 1
A createLogMessage() 0 8 1
A info() 0 3 1
A emergency() 0 3 1
A log() 0 17 2
A alert() 0 3 1
A debug() 0 3 1
A critical() 0 3 1
A warning() 0 3 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