Issues (213)

src/cart/ServerOrderDedicatedProduct.php (1 issue)

possible assignment of non-compatible types.

Bug Documentation Minor
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\widgets\cart\OrderPositionDescriptionWidget;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
use yii\helpers\ArrayHelper;
19
use yii\helpers\Html;
20
21
class ServerOrderDedicatedProduct extends AbstractServerProduct
22
{
23
    /** {@inheritdoc} */
24
    protected $_purchaseModel = ServerOrderDedicatedPurchase::class;
25
26
    /** @var Config */
27
    protected $_model;
28
29
    /** {@inheritdoc} */
30
    protected $_calculationModel = ConfigCalculation::class;
31
32
    /** {@inheritdoc} */
33
    protected $duration = [1];
34
35
    /**
36
     * @var Osimage the selected OS image detailed information
37
     */
38
    protected $_image;
39
40
    /**
41
     * @var string
42
     */
43
    public $location;
44
45
    /**
46
     * @var integer
47
     */
48
    public $object_id;
49
50
    /**
51
     * @var integer
52
     */
53
    public $tariff_id;
54
55
    /**
56
     * @var string
57
     */
58
    public $label;
59
60
    /**
61
     * @var string link to any kind of social network
62
     */
63
    public $administration;
64
65
    /**
66
     * @var string osimage name. Is used to load [[_image]] on product initialisation
67
     */
68
    public $osimage;
69
70
    /**
71
     * @var string software package name
72
     */
73
    public $softpack;
74
75
    /** {@inheritdoc} */
76
    public function load($data, $formName = null)
77
    {
78
        if ($result = parent::load($data, '')) {
79
            $this->ensureRelatedData();
80
        }
81
82
        return $result;
83
    }
84
85
    /** {@inheritdoc} */
86
    protected function ensureRelatedData()
87
    {
88
        $availableConfig = Config::find(['batch' => true])->getAvailable()->withPrices()->withSellerOptions()->andWhere(['id' => $this->object_id])->limit(1)->createCommand()->send()->getData();
89
        if (empty($availableConfig)) {
90
            throw new InvalidConfigException('Failed to find config');
91
        }
92
        $config = new Config();
93
        $config->setAttributes(reset($availableConfig));
94
        $this->_model = $config;
95
96
        $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...
97
        if ($this->_image === null) {
98
            throw new InvalidConfigException('Failed to find osimage');
99
        }
100
        $this->name = $this->_model->name;
101
        $this->description = $this->_model->descr;
102
    }
103
104
    /** {@inheritdoc} */
105
    public function getId()
106
    {
107
        if ($this->_id === null) {
108
            $this->_id = hash('crc32b', implode('_', ['server', 'order', 'dedicated', $this->_model->id, mt_rand()]));
109
        }
110
111
        return $this->_id;
112
    }
113
114
    /** {@inheritdoc} */
115
    public function getCalculationModel($options = [])
116
    {
117
        return parent::getCalculationModel(array_merge([
118
            'tariff_id' => $this->tariff_id,
119
            'object_id' => $this->object_id,
120
            'location' => $this->location,
121
            'image' => $this->osimage,
122
        ], $options));
123
    }
124
125
    /** {@inheritdoc} */
126
    public function getPurchaseModel($options = [])
127
    {
128
        $this->ensureRelatedData();
129
130
        $options = array_merge([
131
            'image' => $this->osimage,
132
            'config_id' => $this->object_id,
133
            'label' => $this->label,
134
            'administration' => $this->administration,
135
            'softpack' => $this->softpack,
136
            'tariff_id' => $this->tariff_id,
137
            'location' => $this->location,
138
        ], $options);
139
140
        return parent::getPurchaseModel($options);
141
    }
142
143
    /** {@inheritdoc} */
144
    public function rules()
145
    {
146
        return array_merge(parent::rules(), [
147
            [['object_id', 'tariff_id'], 'integer'],
148
            [['administration', 'osimage', 'label', 'location'], 'string'],
149
            [['tariff_id', 'object_id', 'osimage', 'location'], 'required'],
150
        ]);
151
    }
152
153
    /** {@inheritdoc} */
154
    public function attributeLabels()
155
    {
156
        return ArrayHelper::merge(parent::attributeLabels(), [
157
            'object_id' => Yii::t('hipanel:server:order', 'Server config'),
158
            'label' => Yii::t('hipanel:server:order', 'Label'),
159
        ]);
160
    }
161
162
    /** {@inheritdoc} */
163
    public function attributeHints()
164
    {
165
        return ArrayHelper::merge(parent::attributeHints(), [
166
            'label' => Yii::t('hipanel:server:order', 'How are you going to use the server?'),
167
            'administration' => 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['object_id'] = $this->object_id;
189
        $parent['osimage'] = $this->osimage;
190
        $parent['label'] = $this->label;
191
        $parent['administration'] = $this->administration;
192
        $parent['softpack'] = $this->softpack;
193
        $parent['tariff_id'] = $this->tariff_id;
194
        $parent['location'] = $this->location;
195
        $parent['_image'] = $this->_image;
196
197
        return $parent;
198
    }
199
200
    public function getDisplayAdministration(): string
201
    {
202
        $map = [
203
            'unmanaged' => Yii::t('hipanel:server:order', 'Basic maintenance'),
204
            'managed' => Yii::t('hipanel:server:order', 'Expert service 24/7'),
205
        ];
206
207
        return $map[$this->administration] ?? 'unknown state of administration';
208
    }
209
210
    public function getDisplayLocation(): string
211
    {
212
        $map = [
213
            'nl' => Yii::t('hipanel:server:order', 'Netherlands'),
214
            'us' => Yii::t('hipanel:server:order', 'USA'),
215
        ];
216
217
        return $map[$this->location] ?? 'unknown place';
218
    }
219
220
    public function getAdditionalLinks(): array
221
    {
222
        return [
223
            [
224
                Yii::$app->params['module.server.order.frontend.url'] ?? '/server/order/dedicated',
225
                Yii::t('hipanel:server:order', 'Order another server')
226
            ],
227
        ];
228
    }
229
}
230