Passed
Push — master ( 854c8f...2c927b )
by Juuso
02:52
created

Yii2LogMessage::convertYiisMessageToString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leinonen\Yii2Monolog;
6
7
use yii\log\Logger;
8
use Psr\Log\LogLevel;
9
use yii\helpers\VarDumper;
10
11
class Yii2LogMessage
12
{
13
    /**
14
     * @var string
15
     */
16
    private $category;
17
18
    /**
19
     * @var array
20
     */
21
    private $trace;
22
23
    /**
24
     * @var int
25
     */
26
    private $memory;
27
28
    /**
29
     * @var string
30
     */
31
    private $message;
32
33
    /**
34
     * @var int
35
     */
36
    private $timestamp;
37
38
    /**
39
     * @var int|mixed
40
     */
41
    private $yiiLogLevel;
42
43
    /**
44
     * Initializes a new Yii2LogMessage.
45
     *
46
     * @param array $message
47
     */
48 11
    public function __construct(array $message)
49
    {
50 11
        $this->setMessage($message[0]);
51 11
        $this->yiiLogLevel = $message[1];
52
53 11
        if (isset($message[2])) {
54 11
            $this->category = $message[2];
55
        }
56
57 11
        $this->timestamp = $message[3];
58
59 11
        if (isset($message[4])) {
60 10
            $this->trace = $message[4];
61
        }
62
63 11
        if (isset($message[5])) {
64 11
            $this->memory = $message[5];
65
        }
66 11
    }
67
68
    /**
69
     * Returns the message string.
70
     *
71
     * @return string
72
     */
73 4
    public function getMessage()
74
    {
75 4
        return $this->message;
76
    }
77
78
    /**
79
     * Returns the timestamp for the Yii2LogMessage.
80
     *
81
     * @return int
82
     */
83 1
    public function getTimestamp()
84
    {
85 1
        return $this->timestamp;
86
    }
87
88
    /**
89
     * Returns the context for the Yii2LogMessage.
90
     *
91
     * @return array
92
     */
93 3
    public function getContext(): array
94
    {
95 3
        $context = [];
96
97 3
        if ($this->category !== null) {
98 3
            $context['category'] = $this->category;
99
        }
100
101 3
        if ($this->trace !== null) {
102 2
            $context['trace'] = $this->trace;
103
        }
104
105 3
        if ($this->memory !== null) {
106 3
            $context['memory'] = $this->memory;
107
        }
108
109 3
        return $context;
110
    }
111
112
    /**
113
     * Returns the PSR-3 compliant log level.
114
     *
115
     * @return string
116
     */
117 9
    public function getPsr3LogLevel(): string
118
    {
119
        $psrLevels = [
120 9
            Logger::LEVEL_ERROR => LogLevel::ERROR,
121
            Logger::LEVEL_WARNING => LogLevel::WARNING,
122
            Logger::LEVEL_INFO => LogLevel::INFO,
123
            Logger::LEVEL_TRACE => LogLevel::DEBUG,
124
            Logger::LEVEL_PROFILE => LogLevel::DEBUG,
125
            Logger::LEVEL_PROFILE_BEGIN => LogLevel::DEBUG,
126
            Logger::LEVEL_PROFILE_END => LogLevel::DEBUG,
127
        ];
128
129 9
        return $psrLevels[$this->yiiLogLevel];
130
    }
131
132
    /**
133
     * @param array|\Throwable|string $message
134
     */
135 11
    private function setMessage($message)
136
    {
137 11
        if (! \is_string($message)) {
138 1
            $message = $this->convertYiisMessageToString($message);
139
        }
140 11
        $this->message = $message;
141 11
    }
142
143
    /**
144
     * Converts Yii's message to string format.
145
     *
146
     * @param array|\Throwable $message
147
     *
148
     * @return string
149
     */
150 1
    private function convertYiisMessageToString($message): string
151
    {
152 1
        if ($message instanceof \Throwable || $message instanceof \Exception) {
153 1
            return (string) $message;
154
        }
155
156 1
        return VarDumper::export($message);
157
    }
158
}
159