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\finance\cart\Calculation; |
||
14 | use hipanel\modules\server\helpers\ServerHelper; |
||
15 | use hipanel\modules\server\models\Osimage; |
||
16 | use hipanel\modules\server\models\Package; |
||
17 | use hipanel\modules\server\widgets\cart\OrderPositionDescriptionWidget; |
||
18 | use Yii; |
||
19 | use yii\base\InvalidConfigException; |
||
20 | use yii\helpers\ArrayHelper; |
||
21 | |||
22 | class ServerOrderProduct extends AbstractServerProduct |
||
23 | { |
||
24 | /** {@inheritdoc} */ |
||
25 | protected $_purchaseModel = ServerOrderPurchase::class; |
||
26 | |||
27 | /** @var Package */ |
||
28 | protected $_model; |
||
29 | |||
30 | /** {@inheritdoc} */ |
||
31 | protected $_calculationModel = Calculation::class; |
||
32 | |||
33 | /** |
||
34 | * @var Osimage the selected OS image detailed information |
||
35 | */ |
||
36 | protected $_image; |
||
37 | |||
38 | /** |
||
39 | * @var integer |
||
40 | */ |
||
41 | public $tariff_id; |
||
42 | |||
43 | /** |
||
44 | * @var integer id of cluster. See [[hipanel\modules\server\models\Package::getLocations()]] for details |
||
45 | * @see \hipanel\modules\server\models\Package::getLocations() |
||
46 | */ |
||
47 | public $cluster_id; |
||
48 | |||
49 | /** |
||
50 | * @var string the purpose for the server |
||
51 | */ |
||
52 | public $purpose; |
||
53 | |||
54 | /** |
||
55 | * @var string link to any kind of social network |
||
56 | */ |
||
57 | public $social; |
||
58 | |||
59 | /** |
||
60 | * @var string osimage name. Is used to load [[_image]] on product initialisation |
||
61 | */ |
||
62 | public $osimage; |
||
63 | |||
64 | /** |
||
65 | * @var string software package name |
||
66 | */ |
||
67 | public $panel_soft; |
||
68 | |||
69 | /** {@inheritdoc} */ |
||
70 | public function load($data, $formName = null) |
||
71 | { |
||
72 | if ($result = parent::load($data, '')) { |
||
73 | $this->ensureRelatedData(); |
||
74 | } |
||
75 | |||
76 | return $result; |
||
77 | } |
||
78 | |||
79 | /** {@inheritdoc} */ |
||
80 | private function ensureRelatedData() |
||
81 | { |
||
82 | $this->_model = ServerHelper::getAvailablePackages(null, $this->tariff_id); |
||
0 ignored issues
–
show
|
|||
83 | if ($this->_model === null) { |
||
84 | throw new InvalidConfigException('Failed to find tariff'); |
||
85 | } |
||
86 | |||
87 | $this->_image = Osimage::find()->where(['osimage' => $this->osimage, 'type' => $this->_model->getType()])->one(); |
||
0 ignored issues
–
show
It seems like
hipanel\modules\server\m...del->getType()))->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 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...
|
|||
88 | if ($this->_image === null) { |
||
89 | throw new InvalidConfigException('Failed to find osimage'); |
||
90 | } |
||
91 | |||
92 | $this->name = $this->_model->getName(); |
||
93 | $this->description = Yii::t('hipanel:server:order', 'Order'); |
||
94 | } |
||
95 | |||
96 | /** {@inheritdoc} */ |
||
97 | public function getId() |
||
98 | { |
||
99 | if ($this->_id === null) { |
||
100 | $this->_id = hash('crc32b', implode('_', ['server', 'order', $this->_model->id])); |
||
0 ignored issues
–
show
The property
id does not exist on hipanel\modules\server\models\Package . Since you implemented __get , consider adding a @property annotation.
Loading history...
|
|||
101 | } |
||
102 | |||
103 | return $this->_id; |
||
104 | } |
||
105 | |||
106 | /** {@inheritdoc} */ |
||
107 | public function getCalculationModel($options = []) |
||
108 | { |
||
109 | return parent::getCalculationModel(array_merge([ |
||
110 | 'tariff_id' => $this->tariff_id, |
||
111 | 'object' => 'server', |
||
112 | ], $options)); |
||
113 | } |
||
114 | |||
115 | /** {@inheritdoc} */ |
||
116 | public function getPurchaseModel($options = []) |
||
117 | { |
||
118 | $this->ensureRelatedData(); // To get fresh domain expiration date |
||
119 | |||
120 | $options = array_merge([ |
||
121 | 'osimage' => $this->osimage, |
||
122 | 'panel' => $this->_image->getPanelName(), |
||
123 | 'cluster_id' => $this->cluster_id, |
||
124 | 'social' => $this->social, |
||
125 | 'purpose' => $this->purpose, |
||
126 | 'tariff_id' => $this->tariff_id, |
||
127 | ], $options); |
||
128 | |||
129 | if ($options['panel'] === Osimage::NO_PANEL) { |
||
130 | unset($options['panel']); |
||
131 | } |
||
132 | |||
133 | return parent::getPurchaseModel($options); |
||
134 | } |
||
135 | |||
136 | /** {@inheritdoc} */ |
||
137 | public function rules() |
||
138 | { |
||
139 | return array_merge(parent::rules(), [ |
||
140 | [['cluster_id', 'tariff_id'], 'integer'], |
||
141 | [['social', 'osimage'], 'safe'], |
||
142 | [['tariff_id', 'purpose', 'osimage'], 'required'], |
||
143 | [['cluster_id'], 'validateClusterId'], |
||
144 | ]); |
||
145 | } |
||
146 | |||
147 | public function validateClusterId($value) |
||
148 | { |
||
149 | return in_array($this->cluster_id, array_keys($this->_model->getLocations()), true); |
||
150 | } |
||
151 | |||
152 | /** {@inheritdoc} */ |
||
153 | public function attributeLabels() |
||
154 | { |
||
155 | return ArrayHelper::merge(parent::attributeLabels(), [ |
||
156 | 'cluster_id' => Yii::t('hipanel:server:order', 'Server location'), |
||
157 | 'purpose' => Yii::t('hipanel:server:order', 'Purpose'), |
||
158 | 'social' => Yii::t('hipanel:server:order', 'Social network'), |
||
159 | ]); |
||
160 | } |
||
161 | |||
162 | /** {@inheritdoc} */ |
||
163 | public function attributeHints() |
||
164 | { |
||
165 | return ArrayHelper::merge(parent::attributeHints(), [ |
||
166 | 'purpose' => Yii::t('hipanel:server:order', 'How are you going to use the server?'), |
||
167 | 'social' => Yii::t('hipanel:server:order', 'Any social network link. Will be used in case of emergency contact'), |
||
168 | ]); |
||
169 | } |
||
170 | |||
171 | /** {@inheritdoc} */ |
||
172 | public function renderDescription() |
||
173 | { |
||
174 | return OrderPositionDescriptionWidget::widget(['position' => $this]); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return Osimage |
||
179 | */ |
||
180 | public function getImage() |
||
181 | { |
||
182 | return $this->_image; |
||
183 | } |
||
184 | |||
185 | protected function serializationMap() |
||
186 | { |
||
187 | $parent = parent::serializationMap(); |
||
188 | $parent['cluster_id'] = $this->cluster_id; |
||
189 | $parent['osimage'] = $this->osimage; |
||
190 | $parent['panel_soft'] = $this->panel_soft; |
||
191 | $parent['purpose'] = $this->purpose; |
||
192 | $parent['social'] = $this->social; |
||
193 | $parent['tariff_id'] = $this->tariff_id; |
||
194 | $parent['_image'] = $this->_image; |
||
195 | |||
196 | return $parent; |
||
197 | } |
||
198 | } |
||
199 |
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 theid
property of an instance of theAccount
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.