Passed
Push — master ( 7ba57e...634515 )
by Paweł
03:13
created

StatsColumn::getDataCellValue()   B

Complexity

Conditions 9
Paths 27

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 27
nop 3
dl 0
loc 32
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 19.01.2018
6
 */
7
8
namespace app\modules\admin\components\grid;
9
10
11
use yii\grid\DataColumn;
12
use yii\helpers\ArrayHelper;
13
14
class StatsColumn extends DataColumn
15
{
16
    public $format = 'html';
17
    public $numberFormat = 'integer';
18
    public $statsAttribute;
19
    public $headerOptions = ['class' => 'sort-numerical'];
20
    public $visibleData = true;
21
22
    /**
23
     * @var \app\components\stats\diffs\MultiAccountsDiff
24
     */
25
    public $dailyDiff;
26
27
    /**
28
     * @var \app\components\stats\diffs\MultiAccountsDiff
29
     */
30
    public $monthlyDiff;
31
32
    public function init()
33
    {
34
        parent::init();
35
        if (!$this->statsAttribute) {
36
            $this->statsAttribute = $this->attribute;
37
        }
38
    }
39
40
    /**
41
     * @param \app\modules\admin\models\Account|\app\modules\admin\models\Tag $model
42
     * @param mixed $key
43
     * @param int $index
44
     * @return null|string
45
     */
46
    public function getDataCellValue($model, $key, $index)
47
    {
48
        /** @var \app\components\Formatter $formatter */
49
        $formatter = $this->grid->formatter;
50
51
        $value = [];
52
        $value[] = $formatter->format($model->{$this->attribute}, $this->numberFormat);
53
54
        // daily change
55
        $key = "{$this->attribute}_daily";
56
        if ($this->visibleData === true || isset($this->visibleData[$key])) {
57
            $dailyChange = $this->dailyDiff->getModel($model->id);
58
            $dailyChange = ArrayHelper::getValue($dailyChange, $this->statsAttribute);
59
            $value[] = is_numeric($dailyChange) ? $formatter->asChange($dailyChange, true, $this->numberFormat) : '-';
60
        }
61
62
        // monthly change
63
        $key = "{$this->attribute}_monthly";
64
        if ($this->visibleData === true || isset($this->visibleData[$key])) {
65
            $monthlyChange = $this->monthlyDiff->getModel($model->id);
66
            $monthlyChange = ArrayHelper::getValue($monthlyChange, $this->statsAttribute);
67
            $value[] = is_numeric($monthlyChange) ? $formatter->asChange($monthlyChange, true, $this->numberFormat) : '-';
68
        }
69
70
        $count = count($value);
71
        if ($count == 3) {
72
            return sprintf('%s (%s/%s)', $value['0'], $value['1'], $value['2']);
73
        } elseif ($count == 2) {
74
            return sprintf('%s (%s)', $value['0'], $value['1']);
75
        }
76
77
        return $value['0'];
78
    }
79
}