Completed
Push — master ( 5e0ad4...117a3a )
by Dmitry
10:56
created

Price::getPlan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\models;
4
5
use hipanel\models\Ref;
6
use hipanel\modules\finance\models\factories\PriceModelFactory;
7
use Yii;
8
use yii\helpers\Inflector;
9
use yii\helpers\StringHelper;
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
 *
24
 * @property TargetObject $object
25
 * @property Plan $plan
26
 *
27
 * @author Dmytro Naumenko <[email protected]>
28
 */
29
class Price extends \hipanel\base\Model
30
{
31
    use \hipanel\base\ModelTrait;
32
33
    const SCENARIO_CREATE = 'create';
34
    const SCENARIO_UPDATE = 'update';
35
    const SCENARIO_DELETE = 'delete';
36
37
    public function rules()
38
    {
39
        return array_merge(parent::rules(), [
40
            [['id', 'parent_id', 'plan_id', 'object_id', 'type_id', 'unit_id', 'currency_id', 'main_object_id'], 'integer'],
41
            [['type', 'plan_name', 'unit', 'currency', 'note', 'data'], 'string'],
42
            [['quantity', 'price'], 'number'],
43
            [['class'], 'string'], // todo: probably, refactor is needed
44
45
            [['plan_id', 'type', 'price', 'currency'], 'required', 'on' => ['create', 'update']],
46
            [['id'], 'required', 'on' => ['update', 'set-note', 'delete']],
47
            [['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...
48
                return (new \ReflectionClass($this))->getShortName();
49
            }],
50
            [['class'], 'string'],
51
        ]);
52
    }
53
54
    public function attributeLabels()
55
    {
56
        return [
57
            'plan_id' => Yii::t('hipanel:finance', 'Plan'),
58
            'plan' => Yii::t('hipanel:finance', 'Plan'),
59
            'quantity' => Yii::t('hipanel:finance', 'Prepaid'),
60
            'unit' => Yii::t('hipanel:finance', 'Unit'),
61
            'price' => Yii::t('hipanel:finance', 'Price'),
62
            'note' => Yii::t('hipanel', 'Note'),
63
            'type' => Yii::t('hipanel', 'Type'),
64
        ];
65
    }
66
67
    public function getTypeOptions()
68
    {
69
        return Ref::getList('type,bill', null, [
70
            'select' => 'oname_label',
71
            'pnames' => 'monthly,overuse',
72
            'with_recursive' => 1,
73
            'mapOptions' => ['from' => 'oname'],
74
        ]);
75
    }
76
77
    public function getUnitOptions()
78
    {
79
        $unitGroup = [
80
            'speed' => ['bps', 'kbps', 'mbps', 'gbps', 'tbps'],
81
            'size' => ['mb', 'mb10', 'mb100', 'gb', 'tb'],
82
        ];
83
84
        $availableUnitsByPriceType = [
85
            'overuse,ip_num' => ['items'],
86
            'overuse,support_time' => ['hour'],
87
            'overuse,backup_du' => $unitGroup['size'],
88
            'overuse,server_traf_max' => $unitGroup['size'],
89
            'overuse,server_traf95_max' => $unitGroup['speed'],
90
            'overuse,server_du' => $unitGroup['size'],
91
            'overuse,server_ssd' => $unitGroup['size'],
92
            'overuse,server_sata' => $unitGroup['size'],
93
        ];
94
95
        $units = Ref::getList('type,unit', 'hipanel.finance.units', [
96
            'with_recursive' => 1,
97
            'select' => 'oname_label',
98
            'mapOptions' => ['from' => 'oname'],
99
        ]);
100
101
        $possibleTypes = $availableUnitsByPriceType[$this->type] ?? [];
102
        return array_intersect_key($units, array_combine($possibleTypes, $possibleTypes));
103
    }
104
105
    public function getUnitLabel()
106
    {
107
        return $this->getUnitOptions()[$this->unit] ?? null;
108
    }
109
110
    public function getCurrencyOptions()
111
    {
112
        return Ref::getList('type,currency');
113
    }
114
115
    public function getObject()
116
    {
117
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
118
    }
119
120
    public function getPlan()
121
    {
122
        return $this->hasOne(Plan::class, ['id' => 'plan_id']);
123
    }
124
125
    public static function tableName()
126
    {
127
        return Inflector::camel2id(StringHelper::basename(__CLASS__), '-');
128
    }
129
130
    public function isOveruse()
131
    {
132
        return strpos($this->type, 'overuse,') === 0;
133
    }
134
135
    public function getSubtype()
136
    {
137
        [, $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...
138
        return $subtype;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public static function instantiate($row)
145
    {
146
        if (empty($row['class'])) {
147
            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...
148
        }
149
150
        /** @var PriceModelFactory $factory */
151
        $factory = Yii::$container->get(PriceModelFactory::class);
152
        return $factory->build($row['class']);
153
    }
154
}
155