Completed
Push — master ( cfc549...9f2fda )
by Klochok
14:31
created

Server::groupUsesForCharts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
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-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
    }
102
103
    /**
104
     * Determine good server states.
105
     *
106
     * @return array
107
     */
108
    public function goodStates()
109
    {
110
        return [static::STATE_OK, static::STATE_DISABLED];
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    public function isOperable()
117
    {
118
        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...
119
            return false;
120
        }
121
122
        return true;
123
    }
124
125
    public function canEnableVNC()
126
    {
127
        return $this->isVNCSupported() && $this->isStateGood();
128
    }
129
130
    public function isStateGood()
131
    {
132
        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...
133
    }
134
135
    /**
136
     * Checks whether server supports VNC.
137
     *
138
     * @return bool
139
     */
140
    public function isVNCSupported()
141
    {
142
        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...
143
    }
144
145
    /**
146
     * Checks whether server supports root password change.
147
     *
148
     * @return bool
149
     */
150
    public function isPwChangeSupported()
151
    {
152
        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...
153
    }
154
155
    /**
156
     * Checks whether server supports LiveCD.
157
     *
158
     * @return bool
159
     */
160
    public function isLiveCDSupported()
161
    {
162
        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...
163
    }
164
165
    /**
166
     * Check whether server is virtual.
167
     *
168
     * @return bool
169
     */
170
    public function isVirtualDevice()
171
    {
172
        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...
173
    }
174
175
    /**
176
     * Check whether server is dedicated.
177
     *
178
     * @return bool
179
     */
180
    public function isDedicatedDevice()
181
    {
182
        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...
183
    }
184
185
    public function getIsBlocked()
186
    {
187
        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...
188
    }
189
190
    /**
191
     * Checks whether server can be operated not.
192
     *
193
     * @throws NotSupportedException
194
     * @return bool
195
     * @see isOperable()
196
     */
197
    public function checkOperable()
198
    {
199
        if (!$this->isOperable()) {
200
            throw new NotSupportedException(Yii::t('hipanel:server', 'Server already has a running task. Can not start new.'));
201
        }
202
        return true;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function scenarioActions()
209
    {
210
        return [
211
            'reinstall' => 'resetup',
212
            'reset-password' => 'regen-root-password',
213
        ];
214
    }
215
216
    /**
217
     * During 5 days after the last expiration client is able to refuse server with full refund.
218
     * Method checks, whether 5 days passed.
219
     * @return bool
220
     */
221
    public function canFullRefuse()
222
    {
223
        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...
224
    }
225
226
    public function groupUsesForCharts()
227
    {
228
        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...
229
    }
230
231
    public function getUses()
232
    {
233
        return $this->hasMany(ServerUse::class, ['object_id' => 'id']);
234
    }
235
236
    public function getIps()
237
    {
238
        return $this->hasMany(Ip::class, ['device_id' => 'id'])->joinWith('links');
239
    }
240
241
    public function getSwitches()
242
    {
243
        return $this->hasMany(Server::class, ['obj_id' => 'id']);
244
    }
245
246
    public function getBindings()
247
    {
248
        return $this->hasMany(Binding::class, ['device_id' => 'id'])->indexBy('type');
249
    }
250
251
    public function getMonitoringSettings()
252
    {
253
        return $this->hasOne(MonitoringSettings::class, ['id' => 'id']);
254
    }
255
256
    public function getHardwareSettings()
257
    {
258
        return $this->hasOne(HardwareSettings::class, ['id' => 'id']);
259
    }
260
261
    public function getSoftwareSettings()
262
    {
263
        return $this->hasOne(SoftwareSettings::class, ['id' => 'id']);
264
    }
265
266
    public function getBinding($type)
267
    {
268
        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...
269
            return null;
270
        }
271
272
        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...
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     */
278
    public function attributeLabels()
279
    {
280
        return $this->mergeAttributeLabels([
281
            'remoteid' => Yii::t('hipanel:server', 'Remote ID'),
282
            'name' => Yii::t('hipanel:server', 'Name'),
283
            'dc' => Yii::t('hipanel:server', 'DC'),
284
            'net' => Yii::t('hipanel:server', 'Switch'),
285
            'kvm' => Yii::t('hipanel:server', 'KVM'),
286
            'pdu' => Yii::t('hipanel:server', 'APC'),
287
            'rack' => Yii::t('hipanel:server', 'Rack'),
288
            'ipmi' => Yii::t('hipanel:server', 'IPMI'),
289
            'status_time' => Yii::t('hipanel:server', 'Status update time'),
290
            'block_reason_label' => Yii::t('hipanel:server', 'Block reason label'),
291
            'request_state_label' => Yii::t('hipanel:server', 'Request state label'),
292
            'mac' => Yii::t('hipanel:server', 'MAC'),
293
            'ips' => Yii::t('hipanel:server', 'IPs'),
294
            'label' => Yii::t('hipanel:server', 'Internal note'),
295
            'os' => Yii::t('hipanel:server', 'OS'),
296
            'comment' => Yii::t('hipanel:server', 'Comment'),
297
            'hwsummary' => Yii::t('hipanel:server', 'HW summary'),
298
            'sale_time' => Yii::t('hipanel:server', 'Sale time'),
299
            'expires' => Yii::t('hipanel:server', 'Expires'),
300
            'tariff_id' => Yii::t('hipanel:server', 'Tariff'),
301
            'order_no' => Yii::t('hipanel:server', 'Order'),
302
        ]);
303
    }
304
}
305
306