Completed
Push — master ( f7bdb9...8d2b6e )
by Andrii
10:42
created

displayAmountWithUnit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
nc 2
nop 0
crap 6
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\models\decorators\server;
12
13
use hipanel\inputs\TextInput;
14
use hipanel\modules\finance\models\decorators\AbstractResourceDecorator;
15
use hipanel\modules\finance\models\ServerResource;
16
use hiqdev\php\units\Quantity;
17
use hiqdev\php\units\Unit;
18
use Yii;
19
20
abstract class AbstractServerResourceDecorator extends AbstractResourceDecorator
21
{
22
    /**
23
     * @var ServerResource
24
     */
25
    public $resource;
26
27
    public function __construct($resource)
28
    {
29
        parent::__construct($resource);
30
    }
31
32
    public function displayTitle()
33
    {
34
        return $this->resource->getTypes()[$this->resource->type];
35
    }
36
37
    public function displayTitleWithDirection(string $title): string
38
    {
39
        $direction = Yii::t('hipanel', 'OUT');
40
        if (str_contains($this->resource->type, '_in')) {
41
            $direction = Yii::t('hipanel', 'IN');
42
        }
43
        if (str_contains($this->resource->type, '_max')) {
44
            $direction = '';
45
        }
46
47
        return Yii::t('hipanel:finance', '{title} {direction}', ['title' => $title, 'direction' => $direction]);
48
    }
49
50
    public function getPrepaidQuantity()
51
    {
52
        return $this->resource->quantity;
53
    }
54
55
    public function getOverusePrice()
56
    {
57
        return $this->resource->price;
58
    }
59
60
    public function displayUnit()
61
    {
62
        return $this->resource->unit;
63
    }
64
65
    public function toUnit(): string
66
    {
67
        return $this->displayUnit();
68
    }
69
70
    public function displayOverusePrice()
71
    {
72
        return Yii::$app->formatter->asCurrency($this->getOverusePrice(), $this->resource->currency);
73
    }
74
75
    public function displayPrepaidAmount()
76
    {
77
        return Yii::t('hipanel:finance:tariff', '{amount} {unit}', [
78
            'amount' => $this->getPrepaidQuantity(),
79
            'unit' => $this->displayUnit(),
80
        ]);
81
    }
82
83
    public function prepaidAmountType()
84
    {
85
        return new TextInput();
86
    }
87
88
    public function displayAmountWithUnit(): string
89
    {
90
        $amount = $this->getPrepaidQuantity();
91
        $convertibleTypes = [
92
            'backup_du',
93
            'hdd',
94
            'ram',
95
            'speed',
96
            'server_traf95_max',
97
            'server_traf95_in',
98
            'server_traf95',
99
            'server_traf_max',
100
            'server_traf_in',
101
            'server_traf',
102
            'server_du',
103
        ];
104
        if (in_array($this->resource->type, $convertibleTypes, true)) {
105
            $amount = Quantity::create(Unit::create($this->resource->unit), $amount)
0 ignored issues
show
Documentation introduced by
\hiqdev\php\units\Unit::...($this->resource->unit) is of type object<hiqdev\php\units\Unit>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
                ->convert(Unit::create($this->toUnit()))
107
                ->getQuantity();
108
            $amount = number_format($amount, 3);
109
        }
110
111
        return Yii::t('hipanel:finance:tariff', '{amount} {unit}', [
112
            'amount' => $amount,
113
            'unit' => $this->displayUnit(),
114
        ]);
115
    }
116
}
117