Completed
Push — master ( cf838f...441ca2 )
by Dmitry
04:31
created

ServerOrderProduct::ensureRelatedData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
crap 12
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
        ], $options));
118
    }
119
120
    /** {@inheritdoc} */
121
    public function rules()
122
    {
123
        return array_merge(parent::rules(), [
124
            [['cluster_id', 'tariff_id'], 'integer'],
125
            [['social', 'osimage'], 'safe'],
126
            [['tariff_id', 'purpose', 'osimage'], 'required'],
127
        ]);
128
    }
129
130
    /** {@inheritdoc} */
131
    public function attributeLabels()
132
    {
133
        return ArrayHelper::merge(parent::attributeLabels(), [
134
            'cluster_id' => Yii::t('hipanel/server', 'Server location'),
135
            'purpose' => Yii::t('hipanel/server', 'Purpose'),
136
            'social' => Yii::t('hipanel/server', 'Social network')
137
        ]);
138
    }
139
140
    /** {@inheritdoc} */
141
    public function attributeHints()
142
    {
143
        return ArrayHelper::merge(parent::attributeHints(), [
144
            'purpose' => Yii::t('hipanel/server', 'How are you going to use the server'),
145
            'social' => Yii::t('hipanel/server', 'Any social network link. Will be used in case of emergency contact'),
146
        ]);
147
    }
148
149
    /** {@inheritdoc} */
150
    public function renderDescription()
151
    {
152
        return OrderPositionDescriptionWidget::widget(['position' => $this]);
153
    }
154
155
    /**
156
     * @return Osimage
157
     */
158
    public function getImage()
159
    {
160
        return $this->_image;
161
    }
162
}
163