Completed
Push — master ( 8d2b6e...df3805 )
by Andrii
13:34
created

PlanGridView::columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 0
cts 50
cp 0
rs 9.0472
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\grid;
12
13
use hipanel\grid\DataColumn;
14
use hipanel\grid\MainColumn;
15
use hipanel\grid\RefColumn;
16
use hipanel\helpers\Url;
17
use hipanel\modules\client\grid\ClientColumn;
18
use hipanel\modules\finance\menus\PlanActionsMenu;
19
use hipanel\modules\finance\models\Plan;
20
use hipanel\modules\finance\widgets\PlanAttributes;
21
use hiqdev\yii2\menus\grid\MenuColumn;
22
use Yii;
23
use yii\helpers\Html;
24
25
class PlanGridView extends \hipanel\grid\BoxedGridView
26
{
27
    public function columns(): array
28
    {
29
        return array_merge(parent::columns(), [
30
            'client' => [
31
                'class' => ClientColumn::class,
32
            ],
33
            'name' => [
34
                'attribute' => 'name',
35
                'filterAttribute' => 'name_ilike',
36
                'filterOptions' => ['class' => 'narrow-filter'],
37
                'class' => MainColumn::class,
38
                'note' => 'note',
39
                'noteOptions' => [
40
                    'url' => Url::to(['@plan/set-note']),
41
                ],
42
                'badges' => function (Plan $model): string {
43
                    return $this->prepareBadges($model);
44
                },
45
            ],
46
            'simple_name' => [
47
                'attribute' => 'name',
48
                'format' => 'html',
49
                'value' => function (Plan $model): string {
50
                    return sprintf('%s %s', $model->name, $this->prepareBadges($model));
51
                },
52
            ],
53
            'state' => [
54
                'attribute' => 'state',
55
                'class' => RefColumn::class,
56
                'filterAttribute' => 'state',
57
                'filterOptions' => ['class' => 'narrow-filter'],
58
                'format' => 'html',
59
                'gtype' => 'state,tariff',
60
            ],
61
            'type' => [
62
                'attribute' => 'type',
63
                'class' => RefColumn::class,
64
                'filterAttribute' => 'type_in',
65
                'filterOptions' => ['class' => 'narrow-filter'],
66
                'format' => 'html',
67
                'gtype' => 'type,tariff',
68
            ],
69
            'actions' => [
70
                'class' => MenuColumn::class,
71
                'menuClass' => PlanActionsMenu::class,
72
            ],
73
            'monthly' => [
74
                'attribute' => 'monthly',
75
                'contentOptions' => ['id' => 'plan-monthly-value'],
76
            ],
77
            'custom_attributes' => [
78
                'class' => DataColumn::class,
79
                'label' => Yii::t('hipanel:finance', 'Attributes'),
80
                'format' => 'raw',
81
                'contentOptions' => ['style' => 'padding: 0;'],
82
                'value' => static fn(Plan $plan): string => PlanAttributes::widget(['plan' => $plan]),
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_PAAMAYIM_NEKUDOTAYIM
Loading history...
83
            ]
84
        ]);
85
    }
86
87
    /**
88
     * @param Plan $model
89
     * @return string
90
     */
91
    protected function prepareBadges(Plan $model): string
92
    {
93
        $html = '';
94
        if ($model->your_tariff) {
95
            $html .=  Html::tag('span', Html::tag('i', null, ['class' => 'fa fa-lock']), ['class' => 'label bg-red pull-right', 'style' => 'margin-left: .1em;']);
96
        }
97
        if ($model->is_grouping) {
98
            $localization = Yii::t('hipanel.finance.plan', 'Grouping');
99
            $html .= Html::tag('span', $localization, ['class' => 'label bg-olive pull-right']);
100
        }
101
102
        return $html;
103
    }
104
}
105