Completed
Push — master ( bb5bc8...bee967 )
by Dmitry
05:17
created

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