Completed
Push — master ( 27dc7b...6de725 )
by Andrii
05:04
created

ServerOrderDedicatedProduct::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\cart;
12
13
use hipanel\modules\server\models\Config;
14
use hipanel\modules\server\models\Osimage;
15
use hipanel\modules\server\models\Package;
16
use hipanel\modules\server\widgets\cart\OrderPositionDescriptionWidget;
17
use Yii;
18
use yii\helpers\ArrayHelper;
19
20
class ServerOrderDedicatedProduct extends AbstractServerProduct
21
{
22
    /** {@inheritdoc} */
23
    protected $_purchaseModel = ServerOrderDedicatedPurchase::class;
24
25
    /** @var Package */
26
    protected $_model;
27
28
    /** {@inheritdoc} */
29
    protected $_calculationModel = ConfigCalculation::class;
30
31
    /**
32
     * @var Osimage the selected OS image detailed information
33
     */
34
    protected $_image;
35
36
    /**
37
     * @var string
38
     */
39
    public $location;
40
41
    /**
42
     * @var integer
43
     */
44
    public $object_id;
45
46
    /**
47
     * @var integer
48
     */
49
    public $tariff_id;
50
51
    /**
52
     * @var string
53
     */
54
    public $label;
55
56
    /**
57
     * @var string link to any kind of social network
58
     */
59
    public $administration;
60
61
    /**
62
     * @var string osimage name. Is used to load [[_image]] on product initialisation
63
     */
64
    public $osimage;
65
66
    /**
67
     * @var string software package name
68
     */
69
    public $softpack;
70
71
    /** {@inheritdoc} */
72
    public function load($data, $formName = null)
73
    {
74
        if ($result = parent::load($data, '')) {
75
            $this->ensureRelatedData();
76
        }
77
78
        return $result;
79
    }
80
81
    /** {@inheritdoc} */
82
    private function ensureRelatedData()
83
    {
84
        $this->_model = Config::findOne($this->object_id);
0 ignored issues
show
Documentation Bug introduced by
It seems like hipanel\modules\server\m...ndOne($this->object_id) of type hiqdev\hiart\ActiveRecord is incompatible with the declared type hipanel\modules\server\models\Package of property $_model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85
        $this->_image = Osimage::find()->where(['osimage' => $this->osimage, 'type' => 'dedicated'])->one();
0 ignored issues
show
Documentation Bug introduced by
It seems like hipanel\modules\server\m...=> 'dedicated'))->one() can also be of type array. However, the property $_image is declared as type hipanel\modules\server\models\Osimage. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
86
        $this->name = $this->_model->name;
87
        $this->description = $this->_model->descr;
88
    }
89
90
    /** {@inheritdoc} */
91
    public function getId()
92
    {
93
        if ($this->_id === null) {
94
            $this->_id = hash('crc32b', implode('_', ['server', 'order', 'dedicated', $this->_model->id]));
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on hipanel\modules\server\models\Package. Since you implemented __get, consider adding a @property annotation.
Loading history...
95
        }
96
97
        return $this->_id;
98
    }
99
100
    /** {@inheritdoc} */
101
    public function getCalculationModel($options = [])
102
    {
103
        return parent::getCalculationModel(array_merge([
104
            'tariff_id' => $this->tariff_id,
105
            'object_id' => $this->object_id,
106
            'location' => $this->location,
107
        ], $options));
108
    }
109
110
    /** {@inheritdoc} */
111
    public function getPurchaseModel($options = [])
112
    {
113
        $this->ensureRelatedData();
114
115
        $options = array_merge([
116
            'osimage' => $this->osimage,
117
            'object_id' => $this->object_id,
118
            'label' => $this->label,
119
            'administration' => $this->administration,
120
            'softpack' => $this->softpack,
121
            'tariff_id' => $this->tariff_id,
122
        ], $options);
123
124
        return parent::getPurchaseModel($options);
125
    }
126
127
    /** {@inheritdoc} */
128
    public function rules()
129
    {
130
        return array_merge(parent::rules(), [
131
            [['object_id', 'tariff_id'], 'integer'],
132
            [['administration', 'osimage', 'label', 'location'], 'string'],
133
            [['tariff_id', 'object_id', 'osimage', 'location'], 'required'],
134
        ]);
135
    }
136
137
    /** {@inheritdoc} */
138
    public function attributeLabels()
139
    {
140
        return ArrayHelper::merge(parent::attributeLabels(), [
141
            'object_id' => Yii::t('hipanel:server:order', 'Server config'),
142
            'label' => Yii::t('hipanel:server:order', 'Label'),
143
        ]);
144
    }
145
146
    /** {@inheritdoc} */
147
    public function attributeHints()
148
    {
149
        return ArrayHelper::merge(parent::attributeHints(), [
150
            'label' => Yii::t('hipanel:server:order', 'How are you going to use the server?'),
151
            'administration' => Yii::t('hipanel:server:order', 'Any social network link. Will be used in case of emergency contact'),
152
        ]);
153
    }
154
155
    /** {@inheritdoc} */
156
    public function renderDescription()
157
    {
158
        return OrderPositionDescriptionWidget::widget(['position' => $this]);
159
    }
160
161
    /**
162
     * @return Osimage
163
     */
164
    public function getImage()
165
    {
166
        return $this->_image;
167
    }
168
169
    protected function serializationMap()
170
    {
171
        $parent = parent::serializationMap();
172
        $parent['object_id'] = $this->object_id;
173
        $parent['osimage'] = $this->osimage;
174
        $parent['label'] = $this->label;
175
        $parent['administration'] = $this->administration;
176
        $parent['softpack'] = $this->softpack;
177
        $parent['tariff_id'] = $this->tariff_id;
178
        $parent['location'] = $this->location;
179
        $parent['_image'] = $this->_image;
180
181
        return $parent;
182
    }
183
}
184