Completed
Push — master ( ce194a...ac6b1c )
by Klochok
13:10
created

Server::isOperable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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