Completed
Push — master ( 958dbc...2d3a59 )
by Dmitry
05:33
created

Price::instantiate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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 View Code Duplication
    public function getTypeOptions()
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...
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 View Code Duplication
    public function getUnitOptions()
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...
76
    {
77
        return Ref::getList('type,unit', null, [
78
            'with_recursive' => 1,
79
            'select' => 'oname_label',
80
            'mapOptions' => ['from' => 'oname'],
81
        ]);
82
    }
83
84
    public function getCurrencyOptions()
85
    {
86
        return Ref::getList('type,currency');
87
    }
88
89
    public function getObject()
90
    {
91
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
92
    }
93
94
    public function getPlan()
95
    {
96
        return $this->hasOne(Plan::class, ['id' => 'plan_id']);
97
    }
98
99
    public static function tableName()
100
    {
101
        return Inflector::camel2id(StringHelper::basename(__CLASS__), '-');
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public static function instantiate($row)
108
    {
109
        if (empty($row['class'])) {
110
            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...
111
        }
112
113
        /** @var PriceModelFactory $factory */
114
        $factory = Yii::$container->get(PriceModelFactory::class);
115
        return $factory->build($row['class']);
116
    }
117
}
118