Completed
Push — master ( ed48ab...3849c5 )
by Dmitry
11:59
created

ServerOrderProduct::primaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Server module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-server
7
 * @package   hipanel-module-server
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\server\cart;
13
14
use hipanel\modules\finance\cart\Calculation;
15
use hipanel\modules\server\helpers\ServerHelper;
16
use hipanel\modules\server\models\Osimage;
17
use hipanel\modules\server\models\Package;
18
use hipanel\modules\server\widgets\cart\OrderPositionDescriptionWidget;
19
use Yii;
20
use yii\base\InvalidConfigException;
21
use yii\helpers\ArrayHelper;
22
23
class ServerOrderProduct extends AbstractServerProduct
24
{
25
    /** {@inheritdoc} */
26
    protected $_purchaseModel = ServerOrderPurchase::class;
27
28
    /** @var Package */
29
    protected $_model;
30
31
    /** {@inheritdoc} */
32
    protected $_calculationModel = Calculation::class;
33
34
    /**
35
     * @var Osimage the selected OS image detailed information
36
     */
37
    protected $_image;
38
39
    /**
40
     * @var integer
41
     */
42
    public $tariff_id;
43
44
    /**
45
     * @var integer id of cluster. See [[hipanel\modules\server\models\Package::getLocations()]] for details
46
     * @see \hipanel\modules\server\models\Package::getLocations()
47
     */
48
    public $cluster_id;
49
50
    /**
51
     * @var string the purpose for the server
52
     */
53
    public $purpose;
54
55
    /**
56
     * @var string link to any kind of social network
57
     */
58
    public $social;
59
60
    /**
61
     * @var string osimage name. Is used to load [[_image]] on product initialisation
62
     */
63
    public $osimage;
64
65
    /**
66
     * @var string software package name
67
     */
68
    public $panel_soft;
69
70
    /** {@inheritdoc} */
71
    public function load($data, $formName = null)
72
    {
73
        if ($result = parent::load($data, '')) {
74
            $this->ensureRelatedData();
75
        }
76
77
        return $result;
78
    }
79
80
    /** {@inheritdoc} */
81
    private function ensureRelatedData()
82
    {
83
        $this->_model = ServerHelper::getAvailablePackages(null, $this->tariff_id);
0 ignored issues
show
Documentation Bug introduced by
It seems like \hipanel\modules\server\...null, $this->tariff_id) can also be of type array. However, the property $_model is declared as type object<hipanel\modules\server\models\Package>. 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...
84
        if ($this->_model === null) {
85
            throw new InvalidConfigException('Failed to find tariff');
86
        }
87
88
        $this->_image = Osimage::find()->where(['osimage' => $this->osimage, 'type' => $this->_model->getType()])->one();
0 ignored issues
show
Documentation Bug introduced by
It seems like \hipanel\modules\server\...del->getType()))->one() can also be of type object<hiqdev\hiart\ActiveRecord> or array. However, the property $_image is declared as type object<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...
89
        if ($this->_image === null) {
90
            throw new InvalidConfigException('Failed to find osimage');
91
        }
92
93
        $this->name = $this->_model->getName();
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<hipanel\modules\s...art\ServerOrderProduct>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
94
        $this->description = Yii::t('hipanel/server/order', 'Order');
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<hipanel\modules\s...art\ServerOrderProduct>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
95
    }
96
97
    /** {@inheritdoc} */
98
    public function getId()
99
    {
100
        if ($this->_id === null) {
101
            $this->_id = hash('crc32b', implode('_', ['server', 'order', $this->_model->id]));
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\server\models\Package>. 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...
102
        }
103
104
        return $this->_id;
105
    }
106
107
    /** {@inheritdoc} */
108
    public function getCalculationModel($options = [])
109
    {
110
        return parent::getCalculationModel(array_merge([
111
            'tariff_id' => $this->tariff_id,
112
            'object' => 'server'
113
        ], $options));
114
    }
115
116
    /** {@inheritdoc} */
117
    public function getPurchaseModel($options = [])
118
    {
119
        $this->ensureRelatedData(); // To get fresh domain expiration date
120
121
        $options = array_merge([
122
            'osimage' => $this->osimage,
123
            'panel' => $this->_image->getPanelName(),
124
            'cluster_id' => $this->cluster_id,
125
            'social' => $this->social,
126
            'purpose' => $this->purpose,
127
            'tariff_id' => $this->tariff_id,
128
        ], $options);
129
130
        if ($options['panel'] === Osimage::NO_PANEL) {
131
            unset($options['panel']);
132
        }
133
134
        return parent::getPurchaseModel($options);
135
    }
136
137
    /** {@inheritdoc} */
138
    public function rules()
139
    {
140
        return array_merge(parent::rules(), [
141
            [['cluster_id', 'tariff_id'], 'integer'],
142
            [['social', 'osimage'], 'safe'],
143
            [['tariff_id', 'purpose', 'osimage'], 'required'],
144
            [['cluster_id'], 'validateClusterId'],
145
        ]);
146
    }
147
148
    public function validateClusterId($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value 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...
149
    {
150
        return in_array($this->cluster_id, array_keys($this->_model->getLocations()), true);
151
    }
152
153
    /** {@inheritdoc} */
154
    public function attributeLabels()
155
    {
156
        return ArrayHelper::merge(parent::attributeLabels(), [
157
            'cluster_id' => Yii::t('hipanel/server/order', 'Server location'),
158
            'purpose' => Yii::t('hipanel/server/order', 'Purpose'),
159
            'social' => Yii::t('hipanel/server/order', 'Social network'),
160
        ]);
161
    }
162
163
    /** {@inheritdoc} */
164
    public function attributeHints()
165
    {
166
        return ArrayHelper::merge(parent::attributeHints(), [
167
            'purpose' => Yii::t('hipanel/server/order', 'How are you going to use the server?'),
168
            'social' => Yii::t('hipanel/server/order', 'Any social network link. Will be used in case of emergency contact'),
169
        ]);
170
    }
171
172
    /** {@inheritdoc} */
173
    public function renderDescription()
174
    {
175
        return OrderPositionDescriptionWidget::widget(['position' => $this]);
176
    }
177
178
    /**
179
     * @return Osimage
180
     */
181
    public function getImage()
182
    {
183
        return $this->_image;
184
    }
185
}
186