Completed
Pull Request — master (#136)
by Klochok
07:02 queued 03:42
created

src/cart/ServerOrderDedicatedProduct.php (2 issues)

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
20
class ServerOrderDedicatedProduct extends AbstractServerProduct
21
{
22
    /** {@inheritdoc} */
23
    protected $_purchaseModel = ServerOrderDedicatedPurchase::class;
24
25
    /** @var Config */
26
    protected $_model;
27
28
    /** {@inheritdoc} */
29
    protected $_calculationModel = ConfigCalculation::class;
30
31
    /** {@inheritdoc} */
32
    protected $duration = [1];
33
34
    /**
35
     * @var Osimage the selected OS image detailed information
36
     */
37
    protected $_image;
38
39
    /**
40
     * @var string
41
     */
42
    public $location;
43
44
    /**
45
     * @var integer
46
     */
47
    public $object_id;
48
49
    /**
50
     * @var integer
51
     */
52
    public $tariff_id;
53
54
    /**
55
     * @var string
56
     */
57
    public $label;
58
59
    /**
60
     * @var string link to any kind of social network
61
     */
62
    public $administration;
63
64
    /**
65
     * @var string osimage name. Is used to load [[_image]] on product initialisation
66
     */
67
    public $osimage;
68
69
    /**
70
     * @var string software package name
71
     */
72
    public $softpack;
73
74
    /** {@inheritdoc} */
75
    public function load($data, $formName = null)
76
    {
77
        if ($result = parent::load($data, '')) {
78
            $this->ensureRelatedData();
79
        }
80
81
        return $result;
82
    }
83
84
    /** {@inheritdoc} */
85
    protected function ensureRelatedData()
86
    {
87
        $availableConfig = Config::find(['batch' => true])->getAvailable()->withPrices()->withSellerOptions()->andWhere(['id' => $this->object_id])->limit(1)->createCommand()->send()->getData();
88
        if (empty($availableConfig)) {
89
            throw new InvalidConfigException('Failed to find config');
90
        }
91
        $config = new Config();
92
        $config->setAttributes(reset($availableConfig));
93
        $this->_model = $config;
94
95
        $this->_image = Osimage::find()->where(['osimage' => $this->osimage, 'type' => 'dedicated'])->one();
96
        if ($this->_image === null) {
97
            throw new InvalidConfigException('Failed to find osimage');
98
        }
99
        $this->name = $this->_model->name;
100
        $this->description = $this->_model->descr;
0 ignored issues
show
Bug Best Practice introduced by Andrey Klochok
The property descr does not exist on hipanel\modules\server\models\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
101
    }
102
103
    /** {@inheritdoc} */
104
    public function getId()
105
    {
106
        if ($this->_id === null) {
107
            $this->_id = hash('crc32b', implode('_', ['server', 'order', 'dedicated', $this->_model->id, mt_rand()]));
0 ignored issues
show
Bug Best Practice introduced by Andrey Klochok
The property id does not exist on hipanel\modules\server\models\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
108
        }
109
110
        return $this->_id;
111
    }
112
113
    /** {@inheritdoc} */
114
    public function getCalculationModel($options = [])
115
    {
116
        return parent::getCalculationModel(array_merge([
117
            'tariff_id' => $this->tariff_id,
118
            'object_id' => $this->object_id,
119
            'location' => $this->location,
120
            'image' => $this->osimage,
121
        ], $options));
122
    }
123
124
    /** {@inheritdoc} */
125
    public function getPurchaseModel($options = [])
126
    {
127
        $this->ensureRelatedData();
128
129
        $options = array_merge([
130
            'image' => $this->osimage,
131
            'config_id' => $this->object_id,
132
            'label' => $this->label,
133
            'administration' => $this->administration,
134
            'softpack' => $this->softpack,
135
            'tariff_id' => $this->tariff_id,
136
            'location' => $this->location,
137
        ], $options);
138
139
        return parent::getPurchaseModel($options);
140
    }
141
142
    /** {@inheritdoc} */
143
    public function rules()
144
    {
145
        return array_merge(parent::rules(), [
146
            [['object_id', 'tariff_id'], 'integer'],
147
            [['administration', 'osimage', 'label', 'location'], 'string'],
148
            [['tariff_id', 'object_id', 'osimage', 'location'], 'required'],
149
        ]);
150
    }
151
152
    /** {@inheritdoc} */
153
    public function attributeLabels()
154
    {
155
        return ArrayHelper::merge(parent::attributeLabels(), [
156
            'object_id' => Yii::t('hipanel:server:order', 'Server config'),
157
            'label' => Yii::t('hipanel:server:order', 'Label'),
158
        ]);
159
    }
160
161
    /** {@inheritdoc} */
162
    public function attributeHints()
163
    {
164
        return ArrayHelper::merge(parent::attributeHints(), [
165
            'label' => Yii::t('hipanel:server:order', 'How are you going to use the server?'),
166
            'administration' => Yii::t('hipanel:server:order', 'Any social network link. Will be used in case of emergency contact'),
167
        ]);
168
    }
169
170
    /** {@inheritdoc} */
171
    public function renderDescription()
172
    {
173
        return OrderPositionDescriptionWidget::widget(['position' => $this]);
174
    }
175
176
    /**
177
     * @return Osimage
178
     */
179
    public function getImage()
180
    {
181
        return $this->_image;
182
    }
183
184
    protected function serializationMap()
185
    {
186
        $parent = parent::serializationMap();
187
        $parent['object_id'] = $this->object_id;
188
        $parent['osimage'] = $this->osimage;
189
        $parent['label'] = $this->label;
190
        $parent['administration'] = $this->administration;
191
        $parent['softpack'] = $this->softpack;
192
        $parent['tariff_id'] = $this->tariff_id;
193
        $parent['location'] = $this->location;
194
        $parent['_image'] = $this->_image;
195
196
        return $parent;
197
    }
198
199
    public function getDisplayAdministration(): string
200
    {
201
        $map = [
202
            'unmanaged' => Yii::t('hipanel:server:order', 'Basic maintenance'),
203
            'managed' => Yii::t('hipanel:server:order', 'Expert service 24/7'),
204
        ];
205
206
        return $map[$this->administration] ?? 'unknown state of administration';
207
    }
208
209
    public function getDisplayLocation(): string
210
    {
211
        $map = [
212
            'nl' => Yii::t('hipanel:server:order', 'Netherlands'),
213
            'us' => Yii::t('hipanel:server:order', 'USA'),
214
        ];
215
216
        return $map[$this->location] ?? 'unknown place';
217
    }
218
}
219