Completed
Push — master ( 8b0ad6...c5ba67 )
by Andrii
04:36
created

Consumption::getTypeLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hipanel\modules\server\models;
4
5
use hipanel\helpers\ArrayHelper;
6
use hipanel\modules\finance\providers\BillTypesProvider;
7
use Yii;
8
9
/**
10
 * Class Consumption
11
 *
12
 * @property string $type
13
 * @property float[] $value
14
 * @property float[] $overuse
15
 */
16
class Consumption extends \hipanel\base\Model
17
{
18
    use \hipanel\base\ModelTrait;
19
20
    public function rules()
21
    {
22
        return [
23
            [['id', 'object_id'], 'integer'],
24
            [['value', 'overuse'], 'safe'],
25
            [['type', 'limit', 'time', 'unit', 'action_unit', 'currency'], 'string'],
26
            [['price'], 'number'],
27
        ];
28
    }
29
30
    /**
31
     * Get type label
32
     * @return string
33
     * @throws \yii\base\InvalidConfigException
34
     */
35
    public function getTypeLabel(): string
36
    {
37
        $provider = Yii::createObject(BillTypesProvider::class);
38
        $types = ArrayHelper::index($provider->getTypes(), 'name');
39
        if (!isset($types[$this->type])) {
40
            return '--';
41
        }
42
43
        return $types[$this->type]->label;
44
    }
45
46
    public function getCurrentValue(): ?string
47
    {
48
        return $this->value[$this->getCurrent()];
49
    }
50
51
    public function getCurrentOveruse(): ?string
52
    {
53
        return $this->overuse[$this->getCurrent()];
54
    }
55
56
    public function getPreviousValue(): ?string
57
    {
58
        return $this->value[$this->getPrevious()];
59
    }
60
61
    public function getPreviousOveruse(): ?string
62
    {
63
        return $this->overuse[$this->getPrevious()];
64
    }
65
66
    private function getCurrent(): string
67
    {
68
        return date('m');
69
    }
70
71
    private function getPrevious(): string
72
    {
73
        return date('m', strtotime('-1month'));
74
    }
75
}
76