Passed
Push — master ( d23c33...74888d )
by Andrii
03:48
created

Server::isPwChangeSupported()   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
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
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\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...
15
use hipanel\modules\server\helpers\ServerHelper;
16
use hipanel\modules\server\models\query\ServerQuery;
17
use hipanel\validators\EidValidator;
18
use hipanel\validators\RefValidator;
19
use Yii;
20
use yii\base\NotSupportedException;
21
22
class Server extends \hipanel\base\Model
23
{
24
    use \hipanel\base\ModelTrait;
25
26
    const STATE_OK = 'ok';
27
    const STATE_DISABLED = 'disabled';
28
    const STATE_BLOCKED = 'blocked';
29
    const STATE_DELETED = 'deleted';
30
31
    const VIRTUAL_DEVICES = ['avds', 'svds', 'ovds'];
32
33
    const SVDS_TYPES = ['avds', 'svds'];
34
35 1
    public function rules()
36
    {
37
        return [
38 1
            [['id', 'tariff_id', 'client_id', 'seller_id'], 'integer'],
39
            [['osimage'], EidValidator::class],
40
            [['panel'], RefValidator::class],
41
            [
42
                [
43
                    'name', 'dc',
44
                    'client', 'seller',
45
                    'os', 'panel', 'rcp',
46
                    'parent_tariff', 'tariff', 'tariff_note', 'discounts',
47
                    'request_state', 'request_state_label',
48
                    'autorenewal',
49
                    'type', 'type_label', 'state', 'state_label', 'statuses',
50
                    'block_reason_label',
51
                    'running_task',
52
                    'ip', 'ips_num', 'mac',
53
                    'acs_num', 'del_acs_num', 'wizzarded',
54
                    'vnc',
55
                    'note', 'label', 'hwsummary', 'order_no',
56
                ],
57
                'safe',
58
            ],
59
            [['show_del', 'show_nic', 'show_vds', 'show_jail'], 'boolean'],
60
            [['switches', 'rack', 'net', 'kvm', 'pdu', 'ipmi'], 'safe'],
61
            [['last_expires', 'expires', 'status_time'], 'date'],
62
            [['time'], 'date', 'format' => 'php:Y-m-d H:i:s'],
63
            [
64
                ['state'],
65
                'isOperable',
66
                'on' => [
67
                    'reinstall',
68
                    'reboot',
69
                    'reset',
70
                    'shutdown',
71
                    'power-off',
72
                    'power-on',
73
                    'boot-live',
74
                    'regen-root-password',
75
                    'reset-password',
76
                ],
77
            ],
78
            [
79
                ['id'],
80
                'required',
81
                'on' => [
82
                    'refuse', 'delete', 'enable-autorenewal',
83
                    'enable-vnc', 'set-note', 'set-label',
84
                    'enable-block', 'disable-block', 'clear-resources',
85
                    'flush-switch-graphs',
86
                ],
87
            ],
88
            [
89
                ['id'],
90
                'integer',
91
                'on' => [
92
                    'refuse', 'delete', 'enable-autorenewal',
93
                    'enable-vnc', 'set-note', 'set-label',
94
                    'enable-block', 'disable-block',
95
                ],
96
            ],
97
            [['id', 'osimage'], 'required', 'on' => ['reinstall']],
98
            [['id', 'osimage'], 'required', 'on' => ['boot-live']],
99
            [['type', 'comment'], 'required', 'on' => ['enable-block', 'disable-block']],
100
101
            [['tariff_id', 'sale_time'], 'required', 'on' => ['sale']],
102
            [['move_accounts'], 'safe', 'on' => ['sale']],
103
            [['id', 'type'], 'required', 'on' => ['set-type']],
104
        ];
105
    }
106
107
    /**
108
     * Determine good server states.
109
     *
110
     * @return array
111
     */
112
    public function goodStates()
113
    {
114
        return [static::STATE_OK, static::STATE_DISABLED];
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function isOperable()
121
    {
122
        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...
123
            return false;
124
        }
125
126
        return true;
127
    }
128
129
    public function canEnableVNC()
130
    {
131
        return $this->isVNCSupported() && $this->isStateGood();
132
    }
133
134
    public function isStateGood()
135
    {
136
        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...
137
    }
138
139
    /**
140
     * Checks whether server supports VNC.
141
     *
142
     * @return bool
143
     */
144
    public function isVNCSupported()
145
    {
146
        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...
147
    }
148
149
    /**
150
     * Checks whether server supports root password change.
151
     *
152
     * @return bool
153
     */
154
    public function isPwChangeSupported()
155
    {
156
        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...
157
    }
158
159
    /**
160
     * Checks whether server supports LiveCD.
161
     *
162
     * @return bool
163
     */
164
    public function isLiveCDSupported()
165
    {
166
        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...
167
    }
168
169
    /**
170
     * Check whether server is virtual.
171
     *
172
     * @return bool
173
     */
174
    public function isVirtualDevice()
175
    {
176
        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...
177
    }
178
179
    /**
180
     * Check whether server is dedicated.
181
     *
182
     * @return bool
183
     */
184
    public function isDedicatedDevice()
185
    {
186
        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...
187
    }
188
189
    public function getIsBlocked()
190
    {
191
        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...
192
    }
193
194
    /**
195
     * Checks whether server can be operated not.
196
     *
197
     * @throws NotSupportedException
198
     * @return bool
199
     * @see isOperable()
200
     */
201
    public function checkOperable()
202
    {
203
        if (!$this->isOperable()) {
204
            throw new NotSupportedException(Yii::t('hipanel:server', 'Server already has a running task. Can not start new.'));
205
        }
206
207
        return true;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function scenarioActions()
214
    {
215
        return [
216
            'reinstall' => 'resetup',
217
            'reset-password' => 'regen-root-password',
218
        ];
219
    }
220
221
    /**
222
     * During 5 days after the last expiration client is able to refuse server with full refund.
223
     * Method checks, whether 5 days passed.
224
     * @return bool
225
     */
226
    public function canFullRefuse()
227
    {
228
        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...
229
            return null; // In case server is not sold
230
        }
231
232
        return (time() - Yii::$app->formatter->asTimestamp($this->last_expires)) / 3600 / 24 < 5;
233
    }
234
235
    public function groupUsesForCharts()
236
    {
237
        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...
238
    }
239
240
    public function getUses()
241
    {
242
        return $this->hasMany(ServerUse::class, ['object_id' => 'id']);
243
    }
244
245
    public function getIps()
246
    {
247
        if (Yii::getAlias('@ip', false)) {
248
            return $this->hasMany(Ip::class, ['device_id' => 'id'])->joinWith('links');
249
        }
250
251
        return [];
252
    }
253
254
    public function getSwitches()
255
    {
256
        return $this->hasMany(Server::class, ['obj_id' => 'id']);
257
    }
258
259
    public function getBindings()
260
    {
261
        return $this->hasMany(Binding::class, ['device_id' => 'id'])->indexBy('type');
262
    }
263
264
    public function getMonitoringSettings()
265
    {
266
        return $this->hasOne(MonitoringSettings::class, ['id' => 'id']);
267
    }
268
269
    public function getHardwareSettings()
270
    {
271
        return $this->hasOne(HardwareSettings::class, ['id' => 'id']);
272
    }
273
274
    public function getSoftwareSettings()
275
    {
276
        return $this->hasOne(SoftwareSettings::class, ['id' => 'id']);
277
    }
278
279
    public function getBinding($type)
280
    {
281
        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...
282
            return null;
283
        }
284
285
        return $this->bindings[$type];
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function attributeLabels()
292
    {
293
        return $this->mergeAttributeLabels([
294
            'remoteid' => Yii::t('hipanel:server', 'Remote ID'),
295
            'name' => Yii::t('hipanel:server', 'Name'),
296
            'dc' => Yii::t('hipanel:server', 'DC'),
297
            'net' => Yii::t('hipanel:server', 'Switch'),
298
            'kvm' => Yii::t('hipanel:server', 'KVM'),
299
            'pdu' => Yii::t('hipanel:server', 'APC'),
300
            'rack' => Yii::t('hipanel:server', 'Rack'),
301
            'ipmi' => Yii::t('hipanel:server', 'IPMI'),
302
            'status_time' => Yii::t('hipanel:server', 'Status update time'),
303
            'block_reason_label' => Yii::t('hipanel:server', 'Block reason label'),
304
            'request_state_label' => Yii::t('hipanel:server', 'Request state label'),
305
            'mac' => Yii::t('hipanel:server', 'MAC'),
306
            'ips' => Yii::t('hipanel:server', 'IPs'),
307
            'label' => Yii::t('hipanel:server', 'Internal note'),
308
            'os' => Yii::t('hipanel:server', 'OS'),
309
            'comment' => Yii::t('hipanel:server', 'Comment'),
310
            'hwsummary' => Yii::t('hipanel:server', 'HW summary'),
311
            'sale_time' => Yii::t('hipanel:server', 'Sale time'),
312
            'expires' => Yii::t('hipanel:server', 'Expires'),
313
            'tariff_id' => Yii::t('hipanel:server', 'Tariff'),
314
            'order_no' => Yii::t('hipanel:server', 'Order'),
315
            'move_accounts' => Yii::t('hipanel:server', 'Move accounts to new client'),
316
            'server' => Yii::t('hipanel:server', 'Server name'),
317
        ]);
318
    }
319
320
    public function getTypeOptions(): array
321
    {
322
        return Ref::getList('type,device,server', 'hipanel:server');
323
    }
324
325
    /**
326
     * Check if you can assign hubs
327
     * @return bool
328
     */
329
    public function canAssignHubs(): bool
330
    {
331
        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...
332
    }
333
334
    /**
335
     * {@inheritdoc}
336
     * @return ServerQuery
337
     */
338
    public static function find($options = [])
339
    {
340
        return new ServerQuery(get_called_class(), [
341
            'options' => $options,
342
        ]);
343
    }
344
}
345