Completed
Push — master ( 4f1415...383c20 )
by Dmitry
05:05
created

Price   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 8
dl 0
loc 137
c 0
b 0
f 0
ccs 0
cts 98
cp 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 17 1
A attributeLabels() 0 13 1
A getTypeOptions() 0 9 1
B getUnitOptions() 0 27 1
A getUnitLabel() 0 4 1
A getCurrencyOptions() 0 4 1
A getObject() 0 4 1
A getPlan() 0 4 1
A tableName() 0 4 1
A isOveruse() 0 4 1
A getSubtype() 0 5 1
A instantiate() 0 10 2
A formulaLines() 0 8 2
1
<?php
2
3
namespace hipanel\modules\finance\models;
4
5
use Yii;
6
use yii\helpers\Inflector;
7
use yii\helpers\StringHelper;
8
use hipanel\models\Ref;
9
use hipanel\modules\finance\models\factories\PriceModelFactory;
10
11
/**
12
 * Class Price
13
 *
14
 * @property int $id
15
 * @property int $plan_id
16
 * @property string|int $object_id
17
 * @property string|float $price
18
 * @property string $currency
19
 * @property string|int $main_object_id
20
 * @property string $unit
21
 * @property string $type
22
 * @property string $quantity
23
 * @property string $formula
24
 *
25
 * @property TargetObject $object
26
 * @property Plan $plan
27
 *
28
 * @author Dmytro Naumenko <[email protected]>
29
 */
30
class Price extends \hipanel\base\Model
31
{
32
    use \hipanel\base\ModelTrait;
33
34
    const SCENARIO_CREATE = 'create';
35
    const SCENARIO_UPDATE = 'update';
36
    const SCENARIO_DELETE = 'delete';
37
38
    public function rules()
39
    {
40
        return array_merge(parent::rules(), [
41
            [['id', 'parent_id', 'plan_id', 'object_id', 'type_id', 'unit_id', 'currency_id', 'main_object_id'], 'integer'],
42
            [['type', 'plan_name', 'unit', 'currency', 'note', 'data'], 'string'],
43
            [['quantity', 'price'], 'number'],
44
            [['class'], 'string'], // todo: probably, refactor is needed
45
46
            [['plan_id', 'type', 'price', 'currency'], 'required', 'on' => ['create', 'update']],
47
            [['id'], 'required', 'on' => ['update', 'set-note', 'delete']],
48
            [['class'], 'default', 'value' => function ($model) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
                return (new \ReflectionClass($this))->getShortName();
50
            }],
51
            [['class'], 'string'],
52
            [['formula'], 'string', 'on' => ['create', 'update']] // TODO syn check
53
        ]);
54
    }
55
56
    public function attributeLabels()
57
    {
58
        return [
59
            'plan_id' => Yii::t('hipanel:finance', 'Tariff plan'),
60
            'plan' => Yii::t('hipanel:finance', 'Tariff plan'),
61
            'quantity' => Yii::t('hipanel:finance', 'Prepaid'),
62
            'unit' => Yii::t('hipanel:finance', 'Unit'),
63
            'price' => Yii::t('hipanel:finance', 'Price'),
64
            'formula' => Yii::t('hipanel.finance.price', 'Formula'),
65
            'note' => Yii::t('hipanel', 'Note'),
66
            'type' => Yii::t('hipanel', 'Type'),
67
        ];
68
    }
69
70
    public function getTypeOptions()
71
    {
72
        return Ref::getList('type,bill', null, [
73
            'select' => 'oname_label',
74
            'pnames' => 'monthly,overuse',
75
            'with_recursive' => 1,
76
            'mapOptions' => ['from' => 'oname'],
77
        ]);
78
    }
79
80
    public function getUnitOptions()
81
    {
82
        $unitGroup = [
83
            'speed' => ['bps', 'kbps', 'mbps', 'gbps', 'tbps'],
84
            'size' => ['mb', 'mb10', 'mb100', 'gb', 'tb'],
85
        ];
86
87
        $availableUnitsByPriceType = [
88
            'overuse,ip_num' => ['items'],
89
            'overuse,support_time' => ['hour'],
90
            'overuse,backup_du' => $unitGroup['size'],
91
            'overuse,server_traf_max' => $unitGroup['size'],
92
            'overuse,server_traf95_max' => $unitGroup['speed'],
93
            'overuse,server_du' => $unitGroup['size'],
94
            'overuse,server_ssd' => $unitGroup['size'],
95
            'overuse,server_sata' => $unitGroup['size'],
96
        ];
97
98
        $units = Ref::getList('type,unit', 'hipanel.finance.units', [
99
            'with_recursive' => 1,
100
            'select' => 'oname_label',
101
            'mapOptions' => ['from' => 'oname'],
102
        ]);
103
104
        $possibleTypes = $availableUnitsByPriceType[$this->type] ?? [];
105
        return array_intersect_key($units, array_combine($possibleTypes, $possibleTypes));
106
    }
107
108
    public function getUnitLabel()
109
    {
110
        return $this->getUnitOptions()[$this->unit] ?? null;
111
    }
112
113
    public function getCurrencyOptions()
114
    {
115
        return Ref::getList('type,currency');
116
    }
117
118
    public function getObject()
119
    {
120
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
121
    }
122
123
    public function getPlan()
124
    {
125
        return $this->hasOne(Plan::class, ['id' => 'plan_id']);
126
    }
127
128
    public static function tableName()
129
    {
130
        return Inflector::camel2id(StringHelper::basename(__CLASS__), '-');
131
    }
132
133
    public function isOveruse()
134
    {
135
        return strpos($this->type, 'overuse,') === 0;
136
    }
137
138
    public function getSubtype()
139
    {
140
        [, $subtype] = explode(',', $this->type);
0 ignored issues
show
Bug introduced by
The variable $subtype does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
141
        return $subtype;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public static function instantiate($row)
148
    {
149
        if (empty($row['class'])) {
150
            return parent::instance($row);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (instance() instead of instantiate()). Are you sure this is correct? If so, you might want to change this to $this->instance().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
151
        }
152
153
        /** @var PriceModelFactory $factory */
154
        $factory = Yii::$container->get(PriceModelFactory::class);
155
        return $factory->build($row['class']);
156
    }
157
158
    public function formulaLines(): array
159
    {
160
        if (strlen($this->formula) === 0) {
161
            return [];
162
        }
163
164
        return explode("\n", $this->formula);
165
    }
166
}
167