Completed
Push — master ( 182b21...5283a1 )
by Dmitry
05:15
created

PricePresenter::renderPrice()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 17

Duplication

Lines 3
Ratio 11.11 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 3
loc 27
ccs 0
cts 20
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 4
nop 1
crap 12
1
<?php
2
3
namespace hipanel\modules\finance\grid\presenters\price;
4
5
use hipanel\modules\finance\models\Price;
6
use hipanel\widgets\ArraySpoiler;
7
use Yii;
8
use yii\bootstrap\Html;
9
10
/**
11
 * Class PricePresenter contains methods that present price properties.
12
 * You can override this class to add custom presentations support.
13
 *
14
 * @author Dmytro Naumenko <[email protected]>
15
 */
16
class PricePresenter
17
{
18
    /**
19
     * @var \yii\i18n\Formatter
20
     */
21
    private $formatter;
22
23
    public function __construct()
24
    {
25
        $this->formatter = Yii::$app->formatter;
26
    }
27
28
    /**
29
     * @param Price $price
30
     * @return string
31
     * @throws \yii\base\InvalidConfigException
32
     */
33
    public function renderPrice(Price $price): string
34
    {
35
        $unit = $formula = '';
36 View Code Duplication
        if ($price->getUnitLabel()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
            $unit = ' ' . Yii::t('hipanel:finance', 'per {unit}', ['unit' => $price->getUnitLabel()]);
38
        }
39
40
        if (count($price->formulaLines()) > 0) {
41
            $formula = ArraySpoiler::widget([
42
                'data' => $price->formulaLines(),
43
                'formatter' => function ($v) {
44
                    return Html::tag('kbd', $v, ['class' => 'javascript']);
45
                },
46
                'visibleCount' => 0,
47
                'delimiter' => '<br />',
48
                'button' => [
49
                    'label' => '&sum;',
50
                    'popoverOptions' => [
51
                        'placement' => 'bottom',
52
                        'html' => true,
53
                    ],
54
                ],
55
            ]);
56
        }
57
58
        return Html::tag('strong', $this->formatter->asCurrency($price->price, $price->currency)) . $unit . $formula;
59
    }
60
61
    /**
62
     * @param Price $price
63
     * @return string
64
     */
65
    public function renderInfo(Price $price): string
66
    {
67
        if ($price->isOveruse()) {
68
            return Yii::t('hipanel:finance', '{coins}&nbsp;&nbsp;{amount,number} {unit}', [
69
                'coins' => Html::tag('i', '', ['class' => 'fa fa-money', 'title' => Yii::t('hipanel.finance.price', 'Prepaid amount')]),
70
                'amount' => $price->quantity,
71
                'unit' => $price->getUnitLabel()
72
            ]);
73
        }
74
75
        if ($price->getSubtype() === 'hardware') {
76
            return $price->object->label;
77
        }
78
79
        return ''; // Do not display any information unless we are sure what we are displaying
80
    }
81
}
82