Target   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 50
ccs 20
cts 20
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getContextMessage() 0 3 1
A init() 0 5 1
A export() 0 19 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sulao\YiiHoneybadger;
6
7
use Throwable;
8
use Yii;
9
use yii\log\Target as YiiTarget;
10
11
class Target extends YiiTarget
12
{
13
    protected ?Component $component = null;
14
15 2
    public function init()
16
    {
17 2
        parent::init();
18
19 2
        $this->component = Yii::$app->honeybadger;
20
    }
21
22
    /**
23
     * Exports the log messages to Honeybadger
24
     *
25
     * This method iterates through the collected log messages and sends them to Honeybadger.
26
     * It handles both exceptions and error messages, sending them with appropriate context.
27
     */
28 1
    public function export()
29
    {
30 1
        foreach ($this->messages as $message) {
31 1
            [$context, , $category , , $traces] = $message;
32
33 1
            if ($context instanceof Throwable) {
34 1
                $this->component?->notify($context);
35 1
            } elseif (is_string($context)) {
36 1
                $data = [
37 1
                    'title'   => $category,
38 1
                    'message' => $context,
39 1
                ];
40 1
                if (isset($traces[0]['file'])) {
41 1
                    $data['file'] = $traces[0]['file'];
42
                }
43 1
                if (isset($traces[0]['line'])) {
44 1
                    $data['line'] = $traces[0]['line'];
45
                }
46 1
                $this->component?->customNotification($data);
47
            }
48
        }
49
    }
50
51
    /**
52
     * Generates the context information to be logged.
53
     * The default implementation will dump user information, system variables, etc.
54
     * Here returns an empty string.
55
     *
56
     * @return string the context information. If an empty string, it means no context information.
57
     */
58 1
    protected function getContextMessage()
59
    {
60 1
        return '';
61
    }
62
}
63