ErrorLogTarget::collect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * @copyright Copyright(c) 2018 Thamtech, LLC
4
 * @link https://github.com/thamtech/yii2-scheduler
5
 * @license https://opensource.org/licenses/MIT
6
**/
7
8
namespace thamtech\scheduler;
9
10
use yii\log\Target;
11
12
/**
13
 * ErrorLogTarget is a custom log target meant to capture a fatal scheduler
14
 * error and pass it to the current task runner.
15
 *
16
 * This target is typically not enabled. It is enabled by a
17
 * Task::EVENT_BEFORE_RUN error handler set in [[Module::bootstrap()]], and
18
 * disabled by a Task::EVENT_AFTER_RUN error handler set in the same place.
19
 *
20
 * The primary purpose of this log target is to pass a fatal error on to the
21
 * [[taskRunner]].
22
 *
23
 * @author Tyler Ham <[email protected]>
24
 */
25
class ErrorLogTarget extends Target
26
{
27
    /**
28
     * @var TaskRunner
29
     */
30
    public $taskRunner;
31
32
    /**
33
     * @var bool whether [[collect()]] has been called with $final=true yet.
34
     */
35
    public $final = false;
36
37
    /**
38
     * @var array exportable messages
39
     */
40
    protected $exportableMessages = [];
41
42
    /**
43
     * {{@inheritdoc}}
44
     */
45
    public function export()
46
    {
47
        // we only care about the most recent error
48
        $lastMessage = array_pop($this->messages);
49
        $this->exportableMessages = [$lastMessage];
50
51
        if (!$this->final) {
52
            // We will wait until final to handle the error in case more errors
53
            // are logged.
54
            return;
55
        }
56
57
        $error = $lastMessage[0];
58
        if ($this->taskRunner) {
59
            $this->taskRunner->handleError(
60
                $error->getCode(),
61
                $error->getMessage(),
62
                $error->getFile(),
63
                $error->getLine());
64
        }
65
    }
66
67
    /**
68
     * {{@inheritdoc}}
69
     */
70
    public function collect($messages, $final)
71
    {
72
        $this->final = $this->final || $final;
73
        $messages = array_merge($this->exportableMessages, $messages);
74
        parent::collect($messages, $final);
75
    }
76
77
    /**
78
     * {{@inheritdoc}}
79
     */
80
    protected function getContextMessage()
81
    {
82
        // we don't want a context message to be appended, so we just return
83
        // an empty string
84
        return '';
85
    }
86
87
    /**
88
     * {{@inheritdoc}}
89
     *
90
     * Only include ErrorExceptions.
91
     */
92
    public static function filterMessages($messages, $levels = 0, $categories = [], $except = [])
93
    {
94
        $messages = parent::filterMessages($messages, $levels, $categories, $except);
95
        foreach ($messages as $i => $message) {
96
            if (!($message[0] instanceof \yii\base\ErrorException)) {
97
                unset($messages[$i]);
98
            }
99
        }
100
        return $messages;
101
    }
102
}
103