Completed
Push — master ( 4aeceb...c161f7 )
by Dmitry
04:53
created

Price::tableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
 *
22
 * @property TargetObject $object
23
 * @property Plan $plan
24
 *
25
 * @author Dmytro Naumenko <[email protected]>
26
 */
27
class Price extends \hipanel\base\Model
28
{
29
    use \hipanel\base\ModelTrait;
30
31
    const SCENARIO_CREATE = 'create';
32
    const SCENARIO_UPDATE = 'update';
33
    const SCENARIO_DELETE = 'delete';
34
35
    public function rules()
36
    {
37
        return array_merge(parent::rules(), [
38
            [['id', 'parent_id', 'plan_id', 'object_id', 'type_id', 'unit_id', 'currency_id', 'main_object_id'], 'integer'],
39
            [['type', 'plan_name', 'unit', 'currency', 'note', 'data'], 'string'],
40
            [['quantity', 'price'], 'number'],
41
            [['class'], 'string'], // todo: probably, refactor is needed
42
43
            [['plan_id', 'type', 'price', 'currency'], 'required', 'on' => ['create', 'update']],
44
            [['id'], 'required', 'on' => ['update', 'set-note', 'delete']],
45
            [['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...
46
                return (new \ReflectionClass($this))->getShortName();
47
            }],
48
            [['class'], 'string'],
49
        ]);
50
    }
51
52 View Code Duplication
    public function attributeLabels()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        return [
55
            'plan_id' => Yii::t('hipanel:finance', 'Plan'),
56
            'plan' => Yii::t('hipanel:finance', 'Plan'),
57
            'quantity' => Yii::t('hipanel:finance', 'Prepaid'),
58
            'unit' => Yii::t('hipanel:finance', 'Unit'),
59
            'price' => Yii::t('hipanel:finance', 'Price'),
60
            'note' => Yii::t('hipanel', 'Note'),
61
            'type' => Yii::t('hipanel', 'Type'),
62
        ];
63
    }
64
65
    public function getTypeOptions()
66
    {
67
        return Ref::getList('type,bill', null, [
68
            'select' => 'oname_label',
69
            'pnames' => 'monthly,overuse',
70
            'with_recursive' => 1,
71
            'mapOptions' => ['from' => 'oname'],
72
        ]);
73
    }
74
75
    public function getUnitOptions()
76
    {
77
        $unitGroup = [
78
            'speed' => ['bps', 'kbps', 'mbps', 'gbps', 'tbps'],
79
            'size' => ['mb', 'mb10', 'mb100', 'gb', 'tb'],
80
        ];
81
82
        $availableUnitsByPriceType = [
83
            'overuse,ip_num' => ['items'],
84
            'overuse,support_time' => ['hour'],
85
            'overuse,backup_du' => $unitGroup['size'],
86
            'overuse,server_traf_max' => $unitGroup['size'],
87
            'overuse,server_traf95_max' => $unitGroup['speed'],
88
            'overuse,server_du' => $unitGroup['size'],
89
            'overuse,server_ssd' => $unitGroup['size'],
90
            'overuse,server_sata' => $unitGroup['size'],
91
        ];
92
93
        $units = Ref::getList('type,unit', 'hipanel:finance:units', [
94
            'with_recursive' => 1,
95
            'select' => 'oname_label',
96
            'mapOptions' => ['from' => 'oname'],
97
        ]);
98
99
        $possibleTypes = $availableUnitsByPriceType[$this->type] ?? [];
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\finance\models\Price>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
        return array_intersect_key($units, array_combine($possibleTypes, $possibleTypes));
101
    }
102
103
    public function getUnitLabel()
104
    {
105
        return $this->getUnitOptions()[$this->unit] ?? null;
106
    }
107
108
    public function getCurrencyOptions()
109
    {
110
        return Ref::getList('type,currency');
111
    }
112
113
    public function getObject()
114
    {
115
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
116
    }
117
118
    public function getPlan()
119
    {
120
        return $this->hasOne(Plan::class, ['id' => 'plan_id']);
121
    }
122
123
    public static function tableName()
124
    {
125
        return Inflector::camel2id(StringHelper::basename(__CLASS__), '-');
126
    }
127
128
    public function isOveruse()
129
    {
130
        return strpos($this->type, 'overuse,') === 0;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\finance\models\Price>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public static function instantiate($row)
137
    {
138
        if (empty($row['class'])) {
139
            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...
140
        }
141
142
        /** @var PriceModelFactory $factory */
143
        $factory = Yii::$container->get(PriceModelFactory::class);
144
        return $factory->build($row['class']);
145
    }
146
}
147