Passed
Branch master (206b08)
by Paweł
03:03
created

StatsColumn::getDataCellValue()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 3
dl 0
loc 17
rs 9.2
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
13
class StatsColumn extends DataColumn
14
{
15
    public $format = 'html';
16
    public $numberFormat = 'integer';
17
    public $statsAttribute;
18
    public $headerOptions = ['class' => 'sort-numerical'];
19
20
    public function init()
21
    {
22
        parent::init();
23
        if (!$this->statsAttribute) {
24
            $this->statsAttribute = $this->attribute;
25
        }
26
    }
27
28
    /**
29
     * @param \app\modules\admin\models\Account|\app\modules\admin\models\Tag $model
30
     * @param mixed $key
31
     * @param int $index
32
     * @return null|string
33
     */
34
    public function getDataCellValue($model, $key, $index)
35
    {
36
        if (!$model->lastStats) {
37
            return null;
38
        }
39
40
        /** @var \app\components\Formatter $formatter */
41
        $formatter = $this->grid->formatter;
42
43
        $lastChange = $model->lastChange($this->statsAttribute);
44
        $monthlyChange = $model->monthlyChange($this->statsAttribute);
45
46
        return sprintf(
47
            "%s (%s/%s)",
48
            $formatter->format($model->lastStats->{$this->statsAttribute}, $this->numberFormat),
49
            $lastChange ? $formatter->asChange($lastChange, true, $this->numberFormat) : $lastChange,
50
            $monthlyChange ? $formatter->asChange($monthlyChange, true, $this->numberFormat) : $monthlyChange
51
        );
52
    }
53
}