Completed
Pull Request — master (#8)
by Grzegorz
06:13
created

Yii2LogMessage::getException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
     * @var \Throwable|null
45
     */
46
    private $exception;
47
48
    /**
49
     * Initializes a new Yii2LogMessage.
50
     *
51
     * @param array $message
52
     * @param bool $exceptionInContext
53
     */
54 13
    public function __construct(array $message, bool $exceptionInContext = false)
55
    {
56 13
        if (true === $exceptionInContext && $message[0] instanceof \Throwable) {
57 2
            $this->exception = $message[0];
58 2
            $this->setMessage(\get_class($message[0]) . ': ' . $message[0]->getMessage());
59
        } else {
60 12
            $this->setMessage($message[0]);
61
        }
62
63 13
        $this->yiiLogLevel = $message[1];
64
65 13
        if (isset($message[2])) {
66 13
            $this->category = $message[2];
67
        }
68
69 13
        $this->timestamp = $message[3];
70
71 13
        if (isset($message[4])) {
72 12
            $this->trace = $message[4];
73
        }
74
75 13
        if (isset($message[5])) {
76 13
            $this->memory = $message[5];
77
        }
78 13
    }
79
80
    /**
81
     * Returns the message string.
82
     *
83
     * @return string
84
     */
85 6
    public function getMessage()
86
    {
87 6
        return $this->message;
88
    }
89
90
    /**
91
     * Returns the timestamp for the Yii2LogMessage.
92
     *
93
     * @return int
94
     */
95 1
    public function getTimestamp()
96
    {
97 1
        return $this->timestamp;
98
    }
99
100
    /**
101
     * @return null|\Throwable
102
     */
103 1
    public function getException()
104
    {
105 1
        return $this->exception;
106
    }
107
108
    /**
109
     * Returns the context for the Yii2LogMessage.
110
     *
111
     * @return array
112
     */
113 5
    public function getContext(): array
114
    {
115 5
        $context = [];
116
117 5
        if ($this->category !== null) {
118 5
            $context['category'] = $this->category;
119
        }
120
121 5
        if ($this->trace !== null) {
122 4
            $context['trace'] = $this->trace;
123
        }
124
125 5
        if ($this->memory !== null) {
126 5
            $context['memory'] = $this->memory;
127
        }
128
129 5
        if ($this->exception !== null) {
130 2
            $context['exception'] = $this->exception;
131
        }
132
133 5
        return $context;
134
    }
135
136
    /**
137
     * Returns the PSR-3 compliant log level.
138
     *
139
     * @return string
140
     */
141 10
    public function getPsr3LogLevel(): string
142
    {
143
        $psrLevels = [
144 10
            Logger::LEVEL_ERROR => LogLevel::ERROR,
145
            Logger::LEVEL_WARNING => LogLevel::WARNING,
146
            Logger::LEVEL_INFO => LogLevel::INFO,
147
            Logger::LEVEL_TRACE => LogLevel::DEBUG,
148
            Logger::LEVEL_PROFILE => LogLevel::DEBUG,
149
            Logger::LEVEL_PROFILE_BEGIN => LogLevel::DEBUG,
150
            Logger::LEVEL_PROFILE_END => LogLevel::DEBUG,
151
        ];
152
153 10
        return $psrLevels[$this->yiiLogLevel];
154
    }
155
156
    /**
157
     * @param array|\Throwable|string $message
158
     */
159 13
    private function setMessage($message)
160
    {
161 13
        if (! \is_string($message)) {
162 1
            $message = $this->convertYiisMessageToString($message);
163
        }
164 13
        $this->message = $message;
165 13
    }
166
167
    /**
168
     * Converts Yii's message to string format.
169
     *
170
     * @param array|\Throwable $message
171
     *
172
     * @return string
173
     */
174 1
    private function convertYiisMessageToString($message): string
175
    {
176 1
        if ($message instanceof \Throwable || $message instanceof \Exception) {
177 1
            return (string) $message;
178
        }
179
180 1
        return VarDumper::export($message);
181
    }
182
}
183