Completed
Push — master ( 4d6fa7...83de61 )
by
unknown
13:33
created

PricePresenter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 3.16 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 7
dl 3
loc 95
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

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
use yii\i18n\Formatter;
18
use yii\web\User;
19
20
/**
21
 * Class PricePresenter contains methods that present price properties.
22
 * You can override this class to add custom presentations support.
23
 *
24
 * @author Dmytro Naumenko <[email protected]>
25
 */
26
class PricePresenter
27
{
28
    protected Formatter $formatter;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
29
30
    protected User $user;
31
32
    protected string $priceAttribute = 'price';
33
34
    public function __construct(Formatter $formatter, User $user)
35
    {
36
        $this->formatter = $formatter;
37
        $this->user = $user;
38
    }
39
40
    /**
41
     * @param string $priceAttribute
42
     * @return $this
43
     */
44
    public function setPriceAttribute(string $priceAttribute): self
45
    {
46
        $this->priceAttribute = $priceAttribute;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param Price $price
53
     * @throws \yii\base\InvalidConfigException
54
     * @return string
55
     */
56
    public function renderPrice(Price $price): string
57
    {
58
        $unit = $formula = '';
59
        if ($price->getUnitLabel()) {
60
            $unit = ' ' . Yii::t('hipanel:finance', 'per {unit}', ['unit' => $price->getUnitLabel()]);
61
        }
62
63
        if (\count($price->formulaLines()) > 0) {
64
            $formula = ArraySpoiler::widget([
65
                'data' => $price->formulaLines(),
66
                'formatter' => function ($v) {
67
                    return Html::tag('kbd', $v, ['class' => 'javascript']);
68
                },
69
                'visibleCount' => 0,
70
                'delimiter' => '<br />',
71
                'button' => [
72
                    'label' => '&sum;',
73
                    'popoverOptions' => [
74
                        'placement' => 'bottom',
75
                        'html' => true,
76
                    ],
77
                ],
78
            ]);
79
        }
80
81
        return Html::tag('strong', $this->formatter->asCurrency($price->{$this->priceAttribute}, $price->currency)) . $unit . $formula;
82
    }
83
84
    /**
85
     * @param Price $price
86
     * @param string $attribute
87
     * @return string
88
     */
89
    public function renderInfo(Price $price, string $attribute = 'quantity'): 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->{$attribute},
101
                'unit' => $price->getUnitLabel(),
102
            ]);
103
        }
104
105
        if ($price->getSubtype() === 'hardware' && $this->user->can('part.read')) {
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