Completed
Push — master ( 4c5d72...05ce3e )
by Andrii
24:03 queued 09:06
created

Consumption::hasFormattedAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 3
crap 12
nc 3
nop 0
1
<?php
2
3
namespace hipanel\modules\server\models;
4
5
use hipanel\helpers\ArrayHelper;
6
use hipanel\modules\finance\providers\BillTypesProvider;
7
use hipanel\modules\server\helpers\consumptionPriceFormatter\MultiplePrice;
8
use hipanel\modules\server\helpers\consumptionPriceFormatter\PriceFormatterStrategy;
9
use hipanel\modules\server\helpers\consumptionPriceFormatter\SinglePrice;
10
use Yii;
11
12
/**
13
 * Class Consumption
14
 *
15
 * @property string $type
16
 * @property float[] $value
17
 * @property float[] $overuse
18
 */
19
class Consumption extends \hipanel\base\Model
20
{
21
    use \hipanel\base\ModelTrait;
22
23
    /**
24
     * @var PriceFormatterStrategy
25
     */
26
    private $priceFormatterStrategy;
27
28
    public function rules()
29
    {
30
        return [
31
            [['id', 'object_id'], 'integer'],
32
            [['value', 'overuse', 'prices'], 'safe'],
33
            [['type', 'limit', 'time', 'unit', 'action_unit', 'currency'], 'string'],
34
            [['price'], 'number'],
35
        ];
36
    }
37
38
    /**
39
     * Get type label
40
     * @return string
41
     * @throws \yii\base\InvalidConfigException
42
     */
43
    public function getTypeLabel(): string
44
    {
45
        $provider = Yii::createObject(BillTypesProvider::class);
46
        $types = ArrayHelper::index($provider->getTypes(), 'name');
47
        if (!isset($types[$this->type])) {
48
            return '--';
49
        }
50
51
        return $types[$this->type]->label;
52
    }
53
54
    public function getCurrentValue(): ?string
55
    {
56
        return $this->value[$this->getCurrent()];
57
    }
58
59
    public function getCurrentOveruse(): ?string
60
    {
61
        return $this->overuse[$this->getCurrent()];
62
    }
63
64
    public function getPreviousValue(): ?string
65
    {
66
        return $this->value[$this->getPrevious()];
67
    }
68
69
    public function getPreviousOveruse(): ?string
70
    {
71
        return $this->overuse[$this->getPrevious()];
72
    }
73
74
    private function getCurrent(): string
75
    {
76
        return date('m');
77
    }
78
79
    private function getPrevious(): string
80
    {
81
        return date('m', strtotime('-1month'));
82
    }
83
84
    public function getFormattedPrice(): string
85
    {
86
        if ($this->prices) {
0 ignored issues
show
Bug Best Practice introduced by
The property prices does not exist on hipanel\modules\server\models\Consumption. Since you implemented __get, consider adding a @property annotation.
Loading history...
87
            $this->setPriceFormatterStrategy(new MultiplePrice());
88
        } else {
89
            $this->setPriceFormatterStrategy(new SinglePrice());
90
        }
91
92
        return $this->priceFormatterStrategy->showPrice($this);
93
    }
94
95
    public function hasFormattedAttributes(): bool
96
    {
97
        return $this->limit || ($this->price || $this->prices);
0 ignored issues
show
Bug Best Practice introduced by
The property prices does not exist on hipanel\modules\server\models\Consumption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property limit does not exist on hipanel\modules\server\models\Consumption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property price does not exist on hipanel\modules\server\models\Consumption. Since you implemented __get, consider adding a @property annotation.
Loading history...
98
    }
99
100
    /**
101
     * @param PriceFormatterStrategy $priceFormatterStrategy
102
     * @return Consumption
103
     */
104
    private function setPriceFormatterStrategy(PriceFormatterStrategy $priceFormatterStrategy): Consumption
105
    {
106
        $this->priceFormatterStrategy = $priceFormatterStrategy;
107
108
        return $this;
109
    }
110
}
111