Completed
Push — master ( ab9ddd...081ac3 )
by Dmitry
11:41
created

Server::getHardwareSales()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\models;
12
13
use hipanel\models\Ref;
14
use hipanel\modules\finance\models\Price;
15
use hipanel\modules\hosting\models\Ip;
0 ignored issues
show
Bug introduced by
The type hipanel\modules\hosting\models\Ip was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use hipanel\modules\server\helpers\ServerHelper;
17
use hipanel\modules\server\models\query\ServerQuery;
18
use hipanel\validators\EidValidator;
19
use hipanel\validators\RefValidator;
20
use Yii;
21
use yii\base\NotSupportedException;
22
23
/**
24
 * Class Server
25
 *
26
 * @property int $id
27
 * @property string $name
28
 */
29
class Server extends \hipanel\base\Model
30
{
31
    use \hipanel\base\ModelTrait;
32
33
    const STATE_OK = 'ok';
34
    const STATE_DISABLED = 'disabled';
35
    const STATE_BLOCKED = 'blocked';
36
    const STATE_DELETED = 'deleted';
37
38
    const VIRTUAL_DEVICES = ['avds', 'svds', 'ovds'];
39
40
    const SVDS_TYPES = ['avds', 'svds'];
41
42
    const DEFAULT_PANEL = 'rcp';
43 1
44
    public function rules()
45
    {
46 1
        return [
47
            [['id', 'tariff_id', 'client_id', 'seller_id'], 'integer'],
48
            [['osimage'], EidValidator::class],
49
            [['panel'], RefValidator::class],
50
            [
51
                [
52
                    'name', 'dc',
53
                    'client', 'seller',
54
                    'os', 'panel', 'rcp',
55
                    'parent_tariff', 'tariff', 'tariff_note', 'discounts',
56
                    'request_state', 'request_state_label',
57
                    'autorenewal',
58
                    'type', 'type_label', 'state', 'state_label', 'statuses',
59
                    'block_reason_label',
60
                    'running_task',
61
                    'ip', 'ips_num', 'mac',
62
                    'acs_num', 'del_acs_num', 'wizzarded',
63
                    'vnc',
64
                    'note', 'label', 'hwsummary', 'order_no',
65
                ],
66
                'safe',
67
            ],
68
            [['show_del', 'show_nic', 'show_vds', 'show_jail'], 'boolean'],
69
            [['switches', 'rack', 'net', 'kvm', 'pdu', 'ipmi', 'hardwareSales'], 'safe'],
70
            [['last_expires', 'expires', 'status_time'], 'date'],
71
            [['time'], 'date', 'format' => 'php:Y-m-d H:i:s'],
72
            [
73
                ['state'],
74
                'isOperable',
75
                'on' => [
76
                    'reinstall',
77
                    'reboot',
78
                    'reset',
79
                    'shutdown',
80
                    'power-off',
81
                    'power-on',
82
                    'boot-live',
83
                    'regen-root-password',
84
                    'reset-password',
85
                ],
86
            ],
87
            [
88
                ['id'],
89
                'required',
90
                'on' => [
91
                    'refuse', 'delete', 'enable-autorenewal',
92
                    'enable-vnc', 'set-note', 'set-label',
93
                    'enable-block', 'disable-block', 'clear-resources',
94
                    'flush-switch-graphs',
95
                ],
96
            ],
97
            [
98
                ['id'],
99
                'integer',
100
                'on' => [
101
                    'refuse', 'delete', 'enable-autorenewal',
102
                    'enable-vnc', 'set-note', 'set-label',
103
                    'enable-block', 'disable-block',
104
                ],
105
            ],
106
            [['id', 'osimage'], 'required', 'on' => ['reinstall']],
107
            [['id', 'osimage'], 'required', 'on' => ['boot-live']],
108
            [['type', 'comment'], 'required', 'on' => ['enable-block', 'disable-block']],
109
110
            [['tariff_id', 'sale_time'], 'required', 'on' => ['sale']],
111
            [['move_accounts'], 'safe', 'on' => ['sale']],
112
            [['id', 'type'], 'required', 'on' => ['set-type']],
113
        ];
114
    }
115
116
    /**
117
     * Determine good server states.
118
     *
119
     * @return array
120
     */
121
    public function goodStates()
122
    {
123
        return [static::STATE_OK, static::STATE_DISABLED];
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function isOperable()
130
    {
131
        if ($this->running_task || !$this->isStateGood()) {
0 ignored issues
show
Bug Best Practice introduced by
The property running_task does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
132
            return false;
133
        }
134
135
        return true;
136
    }
137
138
    public function canEnableVNC()
139
    {
140
        return $this->isVNCSupported() && $this->isStateGood();
141
    }
142
143
    public function isStateGood()
144
    {
145
        return in_array($this->state, $this->goodStates(), true);
0 ignored issues
show
Bug Best Practice introduced by
The property state does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
146
    }
147
148
    /**
149
     * Checks whether server supports VNC.
150
     *
151
     * @return bool
152
     */
153
    public function isVNCSupported()
154
    {
155
        return in_array($this->type, static::SVDS_TYPES, true);
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
156
    }
157
158
    /**
159
     * Checks whether server supports root password change.
160
     *
161
     * @return bool
162
     */
163
    public function isPwChangeSupported()
164
    {
165
        return $this->type === 'ovds';
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
166
    }
167
168
    /**
169
     * Checks whether server supports LiveCD.
170
     *
171
     * @return bool
172
     */
173
    public function isLiveCDSupported()
174
    {
175
        return in_array($this->type, static::SVDS_TYPES, true);
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
176
    }
177
178
    /**
179
     * Check whether server is virtual.
180
     *
181
     * @return bool
182
     */
183
    public function isVirtualDevice()
184
    {
185
        return in_array($this->type, static::VIRTUAL_DEVICES, true);
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
186
    }
187
188
    /**
189
     * Check whether server is dedicated.
190
     *
191
     * @return bool
192
     */
193
    public function isDedicatedDevice()
194
    {
195
        return $this->type === 'dedicated';
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
196
    }
197
198
    public function getIsBlocked()
199
    {
200
        return $this->state === static::STATE_BLOCKED;
0 ignored issues
show
Bug Best Practice introduced by
The property state does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
201
    }
202
203
    /**
204
     * Checks whether server can be operated not.
205
     *
206
     * @throws NotSupportedException
207
     * @return bool
208
     * @see isOperable()
209
     */
210
    public function checkOperable()
211
    {
212
        if (!$this->isOperable()) {
213
            throw new NotSupportedException(Yii::t('hipanel:server', 'Server already has a running task. Can not start new.'));
214
        }
215
216
        return true;
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    public function scenarioActions()
223
    {
224
        return [
225
            'reinstall' => 'resetup',
226
            'reset-password' => 'regen-root-password',
227
        ];
228
    }
229
230
    /**
231
     * During 5 days after the last expiration client is able to refuse server with full refund.
232
     * Method checks, whether 5 days passed.
233
     * @return bool
234
     */
235
    public function canFullRefuse()
236
    {
237
        if (!is_numeric($this->last_expires)) {
0 ignored issues
show
Bug Best Practice introduced by
The property last_expires does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
238
            return null; // In case server is not sold
239
        }
240
241
        return (time() - Yii::$app->formatter->asTimestamp($this->last_expires)) / 3600 / 24 < 5;
242
    }
243
244
    public function groupUsesForCharts()
245
    {
246
        return ServerHelper::groupUsesForChart($this->uses);
0 ignored issues
show
Bug Best Practice introduced by
The property uses does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
247
    }
248
249
    public function getUses()
250
    {
251
        return $this->hasMany(ServerUse::class, ['object_id' => 'id']);
252
    }
253
254
    public function getIps()
255
    {
256
        if (Yii::getAlias('@ip', false)) {
257
            return $this->hasMany(Ip::class, ['device_id' => 'id'])->joinWith('links');
258
        }
259
260
        return [];
261
    }
262
263
    public function getSwitches()
264
    {
265
        return $this->hasMany(Server::class, ['obj_id' => 'id']);
266
    }
267
268
    public function getConsumptions()
269
    {
270
        return $this->hasMany(Consumption::class, ['object_id' => 'id'])->indexBy('type');
271
    }
272
273
    public function getBindings()
274
    {
275
        return $this->hasMany(Binding::class, ['device_id' => 'id'])->indexBy(function ($binding) {
276
            return $binding->typeWithNo;
277
        });
278
    }
279
280
    public function getMonitoringSettings()
281
    {
282
        return $this->hasOne(MonitoringSettings::class, ['id' => 'id']);
283
    }
284
285
    public function getHardwareSettings()
286
    {
287
        return $this->hasOne(HardwareSettings::class, ['id' => 'id']);
288
    }
289
290
    public function getHardwareSales()
291
    {
292
        return $this->hasOne(HardwareSales::class, ['id' => 'id']);
0 ignored issues
show
Bug introduced by
The type hipanel\modules\server\models\HardwareSales was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
293
    }
294
295
    public function getSoftwareSettings()
296
    {
297
        return $this->hasOne(SoftwareSettings::class, ['id' => 'id']);
298
    }
299
300
    public function getBinding($type)
301
    {
302
        if (!isset($this->bindings[$type])) {
0 ignored issues
show
Bug Best Practice introduced by
The property bindings does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
303
            return null;
304
        }
305
306
        return $this->bindings[$type];
307
    }
308
309
    public function getPanel()
310
    {
311
        if ($this->state === self::STATE_DISABLED) {
0 ignored issues
show
Bug Best Practice introduced by
The property state does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
312
            return null;
313
        }
314
315
        if ($this->panel || $this->isVirtualDevice()) {
0 ignored issues
show
Bug Best Practice introduced by
The property panel does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
316
            return $this->panel;
317
        }
318
319
        return self::DEFAULT_PANEL;
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     */
325
    public function attributeLabels()
326
    {
327
        return $this->mergeAttributeLabels([
328
            'remoteid' => Yii::t('hipanel:server', 'Remote ID'),
329
            'name' => Yii::t('hipanel:server', 'Name'),
330
            'dc' => Yii::t('hipanel:server', 'DC'),
331
            'net' => Yii::t('hipanel:server', 'Switch'),
332
            'kvm' => Yii::t('hipanel:server', 'KVM'),
333
            'pdu' => Yii::t('hipanel:server', 'APC'),
334
            'rack' => Yii::t('hipanel:server', 'Rack'),
335
            'ipmi' => Yii::t('hipanel:server', 'IPMI'),
336
            'status_time' => Yii::t('hipanel:server', 'Status update time'),
337
            'block_reason_label' => Yii::t('hipanel:server', 'Block reason label'),
338
            'request_state_label' => Yii::t('hipanel:server', 'Request state label'),
339
            'mac' => Yii::t('hipanel:server', 'MAC'),
340
            'ips' => Yii::t('hipanel:server', 'IPs'),
341
            'label' => Yii::t('hipanel:server', 'Internal note'),
342
            'os' => Yii::t('hipanel:server', 'OS'),
343
            'comment' => Yii::t('hipanel:server', 'Comment'),
344
            'hwsummary' => Yii::t('hipanel:server', 'HW summary'),
345
            'sale_time' => Yii::t('hipanel:server', 'Sale time'),
346
            'expires' => Yii::t('hipanel:server', 'Expires'),
347
            'tariff_id' => Yii::t('hipanel:server', 'Tariff'),
348
            'order_no' => Yii::t('hipanel:server', 'Order'),
349
            'move_accounts' => Yii::t('hipanel:server', 'Move accounts to new client'),
350
            'server' => Yii::t('hipanel:server', 'Server name'),
351
        ]);
352
    }
353
354
    public function getTypeOptions(): array
355
    {
356
        return Ref::getList('type,device,server', 'hipanel:server');
357
    }
358
359
    public function getStateOptions(): array
360
    {
361
        return [
362
            self::STATE_DISABLED    => Yii::t('hipanel:server', 'Ok, panel OFF'),
363
            self::STATE_OK          => Yii::t('hipanel:server', 'Ok'),
364
            self::STATE_BLOCKED     => Yii::t('hipanel:server', 'Blocked'),
365
        ];
366
    }
367
368
    /**
369
     * Check if you can assign hubs
370
     * @return bool
371
     */
372
    public function canAssignHubs(): bool
373
    {
374
        return $this->type !== 'nic'; // XXX todo: need to add more types
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
375
    }
376
377
    /**
378
     * {@inheritdoc}
379
     * @return ServerQuery
380
     */
381
    public static function find($options = [])
382
    {
383
        return new ServerQuery(get_called_class(), [
384
            'options' => $options,
385
        ]);
386
    }
387
}
388