ColoredBalance   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 46
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getColor() 0 4 2
B run() 0 19 7
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\widgets;
12
13
use Yii;
14
use yii\base\Widget;
15
use yii\helpers\Html;
16
17
class ColoredBalance extends Widget
18
{
19
    public $model;
20
21
    public $attribute = 'balance';
22
23
    public $nameAttribute = 'balance';
24
25
    public $url;
26
27
    /**
28
     * @var bool|string Whether to compare [[attribute]] with another attribute to change the display colors
29
     *  - boolean false - do not compare
30
     *  - string - name of attribute to compare with
31
     */
32
    public $compare = false;
33
34
    public $colors = [];
35
36
    public $urlCallback;
37
38
    public function getColor($type)
39
    {
40
        return $this->colors[$type] ?: $type;
41
    }
42
43
    public function run()
44
    {
45
        $value = $this->model->{$this->attribute};
46
        $color = $value === 0 ? 'primary' : 'success';
47
48
        if ($value < 0) {
49
            $color = 'warning';
50
        }
51
52
        if ($this->compare && $value < -($this->model->{$this->compare} ?: 0)) {
53
            $color = 'danger';
54
        }
55
56
        $url = $this->url;
57
        $txt = Yii::$app->formatter->format($value, ['currency', $this->model->currency]);
58
        $ops = ['class' => 'text-nowrap text-' . $this->getColor($color), 'data-pjax' => 0];
59
60
        return $url ? Html::a($txt, $url, $ops) : Html::tag('span', $txt, $ops);
61
    }
62
}
63