Completed
Push — master ( 691d80...8f3098 )
by Dmitry
13:51
created

PricePresenter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 3.41 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setPriceAttribute() 0 6 1
A __construct() 0 4 1
A renderPrice() 3 27 3
A renderInfo() 0 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\grid\presenters\price;
12
13
use hipanel\modules\finance\models\Price;
14
use hipanel\widgets\ArraySpoiler;
15
use Yii;
16
use yii\bootstrap\Html;
17
18
/**
19
 * Class PricePresenter contains methods that present price properties.
20
 * You can override this class to add custom presentations support.
21
 *
22
 * @author Dmytro Naumenko <[email protected]>
23
 */
24
class PricePresenter
25
{
26
    /**
27
     * @var \yii\i18n\Formatter
28
     */
29
    private $formatter;
30
31
    /**
32
     * @var string
33
     */
34
    private $priceAttribute = 'price';
35
36
    public function __construct()
37
    {
38
        $this->formatter = Yii::$app->formatter;
39
    }
40
41
    /**
42
     * @param string $priceAttribute
43
     * @return $this
44
     */
45
    public function setPriceAttribute(string $priceAttribute): self
46
    {
47
        $this->priceAttribute = $priceAttribute;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param Price $price
54
     * @throws \yii\base\InvalidConfigException
55
     * @return string
56
     */
57
    public function renderPrice(Price $price): string
58
    {
59
        $unit = $formula = '';
60 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...
61
            $unit = ' ' . Yii::t('hipanel:finance', 'per {unit}', ['unit' => $price->getUnitLabel()]);
62
        }
63
64
        if (\count($price->formulaLines()) > 0) {
65
            $formula = ArraySpoiler::widget([
66
                'data' => $price->formulaLines(),
67
                'formatter' => function ($v) {
68
                    return Html::tag('kbd', $v, ['class' => 'javascript']);
69
                },
70
                'visibleCount' => 0,
71
                'delimiter' => '<br />',
72
                'button' => [
73
                    'label' => '&sum;',
74
                    'popoverOptions' => [
75
                        'placement' => 'bottom',
76
                        'html' => true,
77
                    ],
78
                ],
79
            ]);
80
        }
81
82
        return Html::tag('strong', $this->formatter->asCurrency($price->{$this->priceAttribute}, $price->currency)) . $unit . $formula;
83
    }
84
85
    /**
86
     * @param Price $price
87
     * @return string
88
     */
89
    public function renderInfo(Price $price): string
90
    {
91
        if (!$price->isQuantityPredefined()) {
92
            return Yii::t('hipanel:finance', '{icon} Quantity: {quantity}', [
93
                'icon' => Html::tag('i', '', ['class' => 'fa fa-calculator']),
94
                'quantity' => Html::tag('b', '<i class="fa fa-spin fa-refresh"></i>', ['data-dynamic-quantity' => true]),
95
            ]);
96
        }
97
        if ($price->isOveruse()) {
98
            return Yii::t('hipanel:finance', '{coins}&nbsp;&nbsp;{amount,number} {unit}', [
99
                'coins' => Html::tag('i', '', ['class' => 'fa fa-money', 'title' => Yii::t('hipanel.finance.price', 'Prepaid amount')]),
100
                'amount' => $price->quantity,
101
                'unit' => $price->getUnitLabel(),
102
            ]);
103
        }
104
105
        if ($price->getSubtype() === 'hardware') {
106
            return $price->object->label;
107
        }
108
109
        return ''; // Do not display any information unless we are sure what we are displaying
110
    }
111
}
112