Completed
Push — master ( 14088c...41674b )
by Dmitry
03:58
created

BillGridView::billQuantity()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 0
cts 19
cp 0
rs 8.8571
cc 5
eloc 16
nc 5
nop 1
crap 30
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\grid;
12
13
use hipanel\grid\CurrencyColumn;
14
use hipanel\grid\MainColumn;
15
use hipanel\helpers\Url;
16
use hipanel\modules\finance\widgets\BillTypeFilter;
17
use hipanel\widgets\ArraySpoiler;
18
use Yii;
19
use yii\helpers\Html;
20
21
class BillGridView extends \hipanel\grid\BoxedGridView
22
{
23
    public static function defaultColumns()
24
    {
25
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('bill' => a...tribute' => 'tariff')); (array<string,array>) is incompatible with the return type of the parent method hipanel\grid\GridView::defaultColumns of type array<string,array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
26
            'bill' => [
27
                'class' => MainColumn::class,
28
                'attribute' => 'bill',
29
                'filterAttribute' => 'bill_like',
30
            ],
31
            'time' => [
32
                'format' => 'html',
33
                'filter' => false,
34
                'contentOptions' => ['class' => 'text-nowrap'],
35
                'value' => function ($model) {
36
                    list($date, $time) = explode(' ', $model->time, 2);
37
38
                    if (in_array($model->gtype, [
39
                        'discount', 'domain', 'monthly', 'overuse', 'premium_package',
40
                        'feature', 'intercept', 'periodic'
41
                    ], true)) {
42
                        return Yii::$app->formatter->asDate($date, 'LLLL y');
43
                    }
44
45
                    return $time === '00:00:00' ? Yii::$app->formatter->asDate($date) : Yii::$app->formatter->asDateTime($model->time);
46
                },
47
            ],
48
            'sum' => [
49
                'class' => CurrencyColumn::class,
50
                'attribute' => 'sum',
51
                'colors' => ['danger' => 'warning'],
52
                'headerOptions' => ['class' => 'text-right'],
53
                'contentOptions' => function ($model) {
54
                    return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')];
55
                },
56
            ],
57
            'sum_editable' => [
58
                'class' => CurrencyColumn::class,
59
                'attribute' => 'sum',
60
                'colors' => ['danger' => 'warning'],
61
                'headerOptions' => ['class' => 'text-right'],
62
                'urlCallback' => function ($model, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
                    return Url::to(['bill/update', 'id' => $model->id]);
64
                },
65
                'contentOptions' => function ($model) {
66
                    return ['class' => 'text-right' . ($model->sum > 0 ? ' text-bold' : '')];
67
                },
68
            ],
69
            'balance' => [
70
                'class' => CurrencyColumn::class,
71
                'headerOptions' => ['class' => 'text-right'],
72
                'contentOptions' => function ($model, $key, $index) {
73
                    return ['class' => 'text-right' . ($index ? '' : ' text-bold')];
74
                },
75
            ],
76
            'gtype' => [
77
                'attribute' => 'gtype',
78
            ],
79
            'type_label' => [
80
                'class' => \hipanel\grid\DataColumn::class,
81
                'filter' => function ($column, $filterModel) {
82
                    return BillTypeFilter::widget([
83
                        'options' => ['class' => 'form-control text-right'],
84
                        'attribute' => 'ftype',
85
                        'model' => $filterModel,
86
                    ]);
87
                },
88
                'format' => 'raw',
89
                'headerOptions' => ['class' => 'text-right'],
90
                'contentOptions' => function ($model) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
                    return ['class' => 'text-right'];
92
                },
93
                'value' => function ($model) {
94
                    static $colors = [
95
                        'correction' => 'normal',
96
                        'exchange' => 'warning',
97
                        'deposit' => 'success',
98
                    ];
99
                    $color = $colors[$model->gtype] ?: 'muted';
100
101
                    return Html::tag('b', Yii::t('hipanel:finance', $model->type_label), ['class' => "text-$color"]);
102
                },
103
            ],
104
            'description' => [
105
                'attribute' => 'descr',
106
                'format' => 'raw',
107
                'value' => function ($model) {
108
                    $descr = $model->descr ?: $model->label;
109
                    $text = mb_strlen($descr) > 70 ? ArraySpoiler::widget(['data' => $descr]) : $descr;
110
                    $tariff = $model->tariff ? Html::tag('span',
111
                        Yii::t('hipanel', 'Tariff') . ': ' . Html::a($model->tariff,
112
                            ['@tariff/view', 'id' => $model->tariff_id]), ['class' => 'pull-right']) : '';
113
                    $amount = static::billQuantity($model);
114
                    $object = $model->object ? implode(':&nbsp;',
115
                        [$model->class_label, static::objectLink($model)]) : '';
116
117
                    return $tariff . $amount . ' ' . implode('<br>', array_filter([$object, $text]));
118
                },
119
            ],
120
            'tariff' => [
121
                'attribute' => 'tariff',
122
            ],
123
        ];
124
    }
125
126
    public static function objectLink($model)
127
    {
128
        return $model->class === 'device'
129
            ? Html::a($model->object, ['@server/view', 'id' => $model->object_id])
130
            : Html::tag('b', $model->object);
131
    }
132
133
    /**
134
     * @param $model
135
     * @return null|string
136
     */
137
    public static function billQuantity($model)
138
    {
139
        switch ($model->type) {
140
            case 'support_time':
141
                $text = Yii::t('hipanel:finance', '{quantity, time, HH:mm} hour(s)',
142
                    ['quantity' => ceil($model->quantity * 3600)]);
143
                break;
144
            case 'server_traf_max':
145
            case 'backup_du':
146
                $text = Yii::$app->formatter->asShortSize($model->quantity * 1024 * 1024 * 1024);
147
                break;
148
            case 'ip_num':
149
                $text = Yii::t('hipanel:finance', '{quantity} IP', ['quantity' => $model->quantity]);
150
                break;
151
            default:
152
                return null;
153
        }
154
155
        return Html::tag('nobr', Html::tag('b', $text));
156
    }
157
}
158