TestingGridView::init()   A
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
c 4
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.2222
1
<?php
2
3
4
namespace carono\exchange1c\widgets;
5
6
use carono\exchange1c\models\TestingClass;
7
use yii\grid\ActionColumn;
8
use yii\grid\GridView;
9
use yii\helpers\Html;
10
11
class TestingGridView extends GridView
12
{
13
    public function initColumns()
14
    {
15
        $this->columns = [
16
            'name',
17
            'expect',
18
            [
19
                'attribute' => 'comment',
20
                'value' => function ($data) {
21
                    /**
22
                     * @var TestingClass $data
23
                     */
24
                    if ($data->hasErrors()) {
25
                        return Html::errorSummary($data);
26
                    }
27
                    return $data->comment;
28
                }
29
            ],
30
            [
31
                'class' => ActionColumn::class,
32
                'header' => 'Result',
33
                'template' => '{view}',
34
                'buttons' => [
35
                    'view' => function ($url, $data) {
36
                        /**
37
                         * @var TestingClass $data
38
                         */
39
                        if ($data->method && !$data->isAutoTest()) {
40
                            $span = Html::tag('i', '', [
41
                                'class' => 'glyphicon glyphicon-eye-open',
42
                                'title' => "Выполнить метод {$data->method}"
43
                            ]);
44
                            return Html::a($span, [
45
                                'testing/index',
46
                                'class' => (new \ReflectionClass($data))->getShortName(),
47
                                'result' => $data->method
48
                            ]);
49
                        } else {
50
                            return $data->result;
51
                        }
52
                    }
53
                ]
54
            ]
55
        ];
56
        parent::initColumns();
57
    }
58
59
    public function init()
60
    {
61
        $this->rowOptions = function ($data) {
62
            /**
63
             * @var TestingClass $data
64
             */
65
            $result = $data->testing();
66
            if (($result === true || $data->hasResult()) && !$data->hasErrors()) {
67
                return ['class' => 'success'];
68
            } elseif ($result === false || $data->hasErrors()) {
69
                return ['class' => 'danger'];
70
            } else {
71
                return ['class' => 'warning'];
72
            }
73
        };
74
75
76
        parent::init();
77
    }
78
}