Completed
Push — master ( 441ca2...ee333a )
by Dmitry
04:29
created

ServerOrderProduct::validateClusterId()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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\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 = 'hipanel\modules\server\cart\ServerOrderPurchase';
26
27
    /** @var Package */
28
    protected $_model;
29
30
    /** {@inheritdoc} */
31
    protected $_calculationModel = OrderCalculation::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
    /** {@inheritdoc} */
65
    public static function primaryKey()
66
    {
67
        return ['model_id'];
68
    }
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])->one();
0 ignored issues
show
Documentation Bug introduced by
It seems like \hipanel\modules\server\...$this->osimage))->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');
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
        return 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...
101
    }
102
103
    /** {@inheritdoc} */
104
    public function getCalculationModel($options = [])
105
    {
106
        return parent::getCalculationModel(array_merge([
107
            'tariff_id' => $this->tariff_id,
108
        ], $options));
109
    }
110
111
    /** {@inheritdoc} */
112
    public function getPurchaseModel($options = [])
113
    {
114
        $this->ensureRelatedData(); // To get fresh domain expiration date
115
        return parent::getPurchaseModel(array_merge([
116
            'osimage' => $this->os,
0 ignored issues
show
Documentation introduced by
The property os does not exist on object<hipanel\modules\s...art\ServerOrderProduct>. 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...
117
            'panel' => $this->_image->getPanelName(),
118
            'cluster_id' => $this->cluster_id,
119
            'social' => $this->social,
120
            'purpose' => $this->purpose,
121
            'tariff_id' => $this->tariff_id
122
        ], $options));
123
    }
124
125
    /** {@inheritdoc} */
126
    public function rules()
127
    {
128
        return array_merge(parent::rules(), [
129
            [['cluster_id', 'tariff_id'], 'integer'],
130
            [['social', 'osimage'], 'safe'],
131
            [['tariff_id', 'purpose', 'osimage'], 'required'],
132
            [['cluster_id'], 'validateClusterId']
133
        ]);
134
    }
135
136
    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...
137
    {
138
        return in_array($this->cluster_id, array_keys($this->_model->getLocations()));
139
    }
140
141
    /** {@inheritdoc} */
142
    public function attributeLabels()
143
    {
144
        return ArrayHelper::merge(parent::attributeLabels(), [
145
            'cluster_id' => Yii::t('hipanel/server', 'Server location'),
146
            'purpose' => Yii::t('hipanel/server', 'Purpose'),
147
            'social' => Yii::t('hipanel/server', 'Social network')
148
        ]);
149
    }
150
151
    /** {@inheritdoc} */
152
    public function attributeHints()
153
    {
154
        return ArrayHelper::merge(parent::attributeHints(), [
155
            'purpose' => Yii::t('hipanel/server', 'How are you going to use the server'),
156
            'social' => Yii::t('hipanel/server', 'Any social network link. Will be used in case of emergency contact'),
157
        ]);
158
    }
159
160
    /** {@inheritdoc} */
161
    public function renderDescription()
162
    {
163
        return OrderPositionDescriptionWidget::widget(['position' => $this]);
164
    }
165
166
    /**
167
     * @return Osimage
168
     */
169
    public function getImage()
170
    {
171
        return $this->_image;
172
    }
173
}
174