GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 871d34...713d9f )
by Steve
04:40
created

ErrorPanel::getChartModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace bedezign\yii2\audit\panels;
4
5
use bedezign\yii2\audit\components\Helper;
6
use bedezign\yii2\audit\components\panels\Panel;
7
use bedezign\yii2\audit\components\panels\RendersSummaryChartTrait;
8
use bedezign\yii2\audit\models\AuditError;
9
use bedezign\yii2\audit\models\AuditErrorSearch;
10
use Yii;
11
use yii\grid\GridViewAsset;
12
13
/**
14
 * ErrorPanel
15
 * @package bedezign\yii2\audit\panels
16
 */
17
class ErrorPanel extends Panel
18
{
19
    use RendersSummaryChartTrait;
20
21
    private $_exceptions = [];
22
23 78
    /**
24
     * @inheritdoc
25 78
     */
26
    public function init()
27 3
    {
28 3
        parent::init();
29 78
        $this->module->registerFunction('exception', function($e) {
30
            $entry = $this->module->getEntry(true);
31 78
            return $entry ? $this->log($entry->id, $e) : null;
32 3
        });
33 3
34 78
        $this->module->registerFunction('errorMessage', function ($message, $code = 0, $file = '', $line = 0, $trace = []) {
35 78
            $entry = $this->module->getEntry(true);
36
            return $entry ? $this->logMessage($entry->id, $message, $code, $file, $line, $trace) : null;
37
        });
38
    }
39
40
41
    /**
42
     * Log an exception
43
     *
44
     * @param int        $entry_id      Entry to associate the error with
45 30
     * @param \Exception|\Throwable  $exception
46
     * @return null|static
47
     */
48 27
    public function log($entry_id, $exception)
49 27
    {
50 27
        // Only log each exception once
51
        $exceptionId = spl_object_hash($exception);
52
        if (in_array($exceptionId, $this->_exceptions))
53 27
            return true;
54 27
55
        // If this is a follow up exception, make sure to log the base exception first
56 27
        if ($exception->getPrevious())
57 27
            $this->log($entry_id, $exception->getPrevious());
58 30
59 30
        $error = new AuditError();
60 30
        $error->entry_id    = $entry_id;
61 30
        $error->message     = $exception->getMessage();
62 30
        $error->code        = $exception->getCode();
63 30
        $error->file        = $exception->getFile();
64
        $error->line        = $exception->getLine();
65 30
        $error->trace       = Helper::cleanupTrace($exception->getTrace());
66
        $error->hash        = Helper::hash($error->message . $error->file . $error->line);
67 27
68
        $this->_exceptions[] = $exceptionId;
69
70
        return $error->save(false) ? $error : null;
71
    }
72
73
    /**
74
     * Log a regular error message
75
     *
76
     * @param int        $entry_id      Entry to associate the error with
77
     * @param string     $message
78
     * @param int        $code
79
     * @param string     $file
80
     * @param int        $line
81 6
     * @param array      $trace         Stack trace to include. Use `Helper::generateTrace()` to create it.
82
     * @return null|static
83 6
     */
84 6
    public function logMessage($entry_id, $message, $code = 0, $file = '', $line = 0, $trace = [])
85 6
    {
86 6
        $error = new AuditError();
87 6
        $error->entry_id    = $entry_id;
88 6
        $error->message     = $message;
89 6
        $error->code        = $code;
90 6
        $error->file        = $file;
91 6
        $error->line        = $line;
92
        $error->trace       = Helper::cleanupTrace($trace);
93
        $error->hash        = Helper::hash($error->message . $error->file . $error->line);
94
        return $error->save(false) ? $error : null;
95
    }
96
97 3
    /**
98
     * @inheritdoc
99 3
     */
100
    public function hasEntryData($entry)
101
    {
102
        return count($entry->linkedErrors) > 0;
103
    }
104
105 33
    /**
106
     * @inheritdoc
107 33
     */
108
    public function getName()
109
    {
110
        return \Yii::t('audit', 'Errors');
111
    }
112
113 3
    /**
114
     * @inheritdoc
115 3
     */
116
    public function getLabel()
117
    {
118
        return $this->getName() . ' <small>(' . count($this->_model->linkedErrors) . ')</small>';
119
    }
120
121 3
    /**
122
     * @inheritdoc
123 3
     */
124 3
    public function getDetail()
125 3
    {
126 3
        $searchModel = new AuditErrorSearch();
127
        $params = \Yii::$app->request->getQueryParams();
128 3
        $params['AuditErrorSearch']['entry_id'] = $params['id'];
129 3
        $dataProvider = $searchModel->search($params);
130 3
131 3
        return \Yii::$app->view->render('panels/error/detail', [
132 3
            'panel'        => $this,
133
            'dataProvider' => $dataProvider,
134
            'searchModel'  => $searchModel,
135
        ]);
136
    }
137
138 33
    /**
139
     * @inheritdoc
140 33
     */
141
    public function getIndexUrl()
142
    {
143
        return ['error/index'];
144
    }
145
146 3
    /**
147
     * @inheritdoc
148 3
     */
149 3
    protected function getChartModel()
150 3
    {
151
        return AuditError::className();
152
    }
153
154
    /**
155
     * @inheritdoc
156 3
     */
157
    public function getChart()
158 3
    {
159 3
        return \Yii::$app->view->render('panels/error/chart', [
160
            'panel' => $this,
161
            'chartData' => $this->getChartData()
162
        ]);
163
    }
164 3
165
    /**
166 3
     * @inheritdoc
167 3
     */
168 3
    public function registerAssets($view)
169 3
    {
170 3
        GridViewAsset::register($view);
171 3
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    public function cleanup($maxAge = null)
177
    {
178
        $maxAge = $maxAge !== null ? $maxAge : $this->maxAge;
179
        if ($maxAge === null)
180
            return false;
181
        return AuditError::deleteAll([
182
            '<=', 'created', date('Y-m-d 23:59:59', strtotime("-$maxAge days"))
183
        ]);
184
    }
185
186
}