FileLogger   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A error() 0 3 1
A notice() 0 3 1
A createLogMessage() 0 8 1
A info() 0 3 1
A debug() 0 3 1
A warning() 0 3 1
A __construct() 0 2 1
A emergency() 0 3 1
A log() 0 17 2
A alert() 0 3 1
A critical() 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
    /**
13
     * @param string $filePath
14
     */
15 10
    public function __construct(private readonly string $filePath)
16
    {
17 10
    }
18
19
    /**
20
     * @param mixed $level
21
     * @param Stringable|string $message
22
     * @param array $context
23
     * @return void
24
     */
25 10
    public function log(mixed $level, Stringable|string $message, array $context = []): void
26
    {
27 10
        if (!in_array($level, [
28 10
            LogLevel::EMERGENCY,
29 10
            LogLevel::ALERT,
30 10
            LogLevel::CRITICAL,
31 10
            LogLevel::ERROR,
32 10
            LogLevel::WARNING,
33 10
            LogLevel::NOTICE,
34 10
            LogLevel::INFO,
35 10
            LogLevel::DEBUG
36 10
        ])) {
37 1
            throw new InvalidArgumentException('Invalid log level.');
38
        }
39
40 9
        $logMessage = $this->createLogMessage($level, $message, $context);
41 9
        file_put_contents($this->filePath, $logMessage, FILE_APPEND);
42
    }
43
44
    /**
45
     * @param mixed $level
46
     * @param Stringable|string $message
47
     * @param array $context
48
     * @return string
49
     */
50 9
    private function createLogMessage(mixed $level, Stringable|string $message, array $context): string
51
    {
52 9
        return sprintf(
53 9
            '[%s] %s %s %s' . PHP_EOL,
54 9
            date('Y-m-d H:i:s'),
55 9
            strtoupper($level),
56 9
            $message,
57 9
            json_encode($context)
58 9
        );
59
    }
60
61
    /**
62
     * @param Stringable|string $message
63
     * @param array $context
64
     * @return void
65
     */
66 1
    public function emergency(Stringable|string $message, array $context = []): void
67
    {
68 1
        $this->log(LogLevel::EMERGENCY, $message, $context);
69
    }
70
71
    /**
72
     * @param Stringable|string $message
73
     * @param array $context
74
     * @return void
75
     */
76 1
    public function alert(Stringable|string $message, array $context = []): void
77
    {
78 1
        $this->log(LogLevel::ALERT, $message, $context);
79
    }
80
81
    /**
82
     * @param Stringable|string $message
83
     * @param array $context
84
     * @return void
85
     */
86 1
    public function critical(Stringable|string $message, array $context = []): void
87
    {
88 1
        $this->log(LogLevel::CRITICAL, $message, $context);
89
    }
90
91
    /**
92
     * @param Stringable|string $message
93
     * @param array $context
94
     * @return void
95
     */
96 1
    public function error(Stringable|string $message, array $context = []): void
97
    {
98 1
        $this->log(LogLevel::ERROR, $message, $context);
99
    }
100
101
    /**
102
     * @param Stringable|string $message
103
     * @param array $context
104
     * @return void
105
     */
106 1
    public function warning(Stringable|string $message, array $context = []): void
107
    {
108 1
        $this->log(LogLevel::WARNING, $message, $context);
109
    }
110
111
    /**
112
     * @param Stringable|string $message
113
     * @param array $context
114
     * @return void
115
     */
116 1
    public function notice(Stringable|string $message, array $context = []): void
117
    {
118 1
        $this->log(LogLevel::NOTICE, $message, $context);
119
    }
120
121
    /**
122
     * @param Stringable|string $message
123
     * @param array $context
124
     * @return void
125
     */
126 1
    public function info(Stringable|string $message, array $context = []): void
127
    {
128 1
        $this->log(LogLevel::INFO, $message, $context);
129
    }
130
131
    /**
132
     * @param Stringable|string $message
133
     * @param array $context
134
     * @return void
135
     */
136 1
    public function debug(Stringable|string $message, array $context = []): void
137
    {
138 1
        $this->log(LogLevel::DEBUG, $message, $context);
139
    }
140
}
141