DiscountFormatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
eloc 15
dl 0
loc 34
c 0
b 0
f 0
ccs 0
cts 19
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A run() 0 15 3
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\widgets;
12
13
use Yii;
14
use yii\base\Widget;
15
use yii\helpers\Html;
16
17
/**
18
 * Class DiscountFormatter
19
 * Renders a button with popover to display current and upcoming discount.
20
 */
21
class DiscountFormatter extends Widget
22
{
23
    /**
24
     * @var float|string Current discount
25
     */
26
    public $current;
27
28
    /**
29
     * @var float|string Next discount
30
     */
31
    public $next;
32
33
    public function init()
34
    {
35
        parent::init();
36
        $this->current = floatval($this->current);
37
        $this->next = floatval($this->next);
38
    }
39
40
    public function run()
41
    {
42
        if ($this->current > 0 || $this->next > 0) {
43
            $this->getView()->registerJs("$('.discount-popover').popover();", \yii\web\View::POS_READY, 'discount-popover');
44
45
            return Html::a(Yii::$app->formatter->asPercent($this->current / 100), '#', [
46
                'onClick' => 'return false',
47
                'title' => Yii::t('hipanel:server', 'Next discount'),
48
                'class' => 'btn btn-default btn-xs discount-popover',
49
                'data-trigger' => 'focus',
50
                'data-content' => Yii::$app->formatter->asPercent($this->next / 100),
51
            ]);
52
        }
53
54
        return '';
55
    }
56
}
57