Completed
Push — master ( 24c174...6bfe3b )
by Dmitry
04:03
created

Server::isVNCSupported()   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
/*
4
 * Server module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-server
7
 * @package   hipanel-module-server
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\server\models;
13
14
use hipanel\modules\hosting\models\Ip;
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
    public function rules()
31
    {
32
        return [
33
            [['id', 'tariff_id', 'client_id', 'seller_id'], 'integer'],
34
            [['osimage'], EidValidator::class],
35
            [['panel'], RefValidator::class],
36
            [
37
                [
38
                    'name', 'dc',
39
                    'client', 'seller',
40
                    'os', 'panel', 'rcp',
41
                    'parent_tariff', 'tariff', 'tariff_note', 'discounts',
42
                    'request_state', 'request_state_label',
43
                    'autorenewal',
44
                    'type', 'type_label', 'state', 'state_label', 'statuses',
45
                    'block_reason_label',
46
                    'running_task',
47
                    'ip', 'ips_num', 'mac',
48
                    'acs_num', 'del_acs_num', 'wizzarded',
49
                    'vnc',
50
                    'note', 'label', 'hwsummary',
51
                ],
52
                'safe',
53
            ],
54
            [['switches', 'rack', 'net', 'kvm', 'pdu', 'ipmi'], 'safe'],
55
            [['last_expires', 'expires', 'status_time', 'sale_time'], 'date'],
56
            [
57
                ['state'],
58
                'isOperable',
59
                'on' => [
60
                    'reinstall',
61
                    'reboot',
62
                    'reset',
63
                    'shutdown',
64
                    'power-off',
65
                    'power-on',
66
                    'boot-live',
67
                    'regen-root-password',
68
                    'reset-password',
69
                ],
70
            ],
71
            [
72
                ['id'],
73
                'required',
74
                'on' => [
75
                    'refuse', 'delete', 'enable-autorenewal',
76
                    'enable-vnc', 'set-note', 'set-label',
77
                    'enable-block', 'disable-block',
78
                ],
79
            ],
80
            [
81
                ['id'],
82
                'integer',
83
                'on' => [
84
                    'refuse', 'delete', 'enable-autorenewal',
85
                    'enable-vnc', 'set-note', 'set-label',
86
                    'enable-block', 'disable-block',
87
                ],
88
            ],
89
            [['id', 'osimage'], 'required', 'on' => ['reinstall']],
90
            [['id', 'osimage'], 'required', 'on' => ['boot-live']],
91
            [['type', 'comment'], 'required', 'on' => ['enable-block']],
92
            [['comment'], 'safe', 'on' => ['disable-block']],
93
        ];
94
    }
95
96
    /**
97
     * Determine good server states.
98
     *
99
     * @return array
100
     */
101
    public function goodStates()
102
    {
103
        return [static::STATE_OK, static::STATE_DISABLED];
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isOperable()
110
    {
111
        if ($this->running_task || !in_array($this->state, $this->goodStates(), true)) {
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...
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...
112
            return false;
113
        }
114
        return true;
115
    }
116
117
    /**
118
     * Checks whether server supports VNC.
119
     *
120
     * @return bool
121
     */
122
    public function isVNCSupported()
123
    {
124
        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...
125
    }
126
127
    /**
128
     * Checks whether server supports root password change.
129
     *
130
     * @return bool
131
     */
132
    public function isPwChangeSupported()
133
    {
134
        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...
135
    }
136
137
    /**
138
     * Checks whether server supports LiveCD.
139
     *
140
     * @return bool
141
     */
142
    public function isLiveCDSupported()
143
    {
144
        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...
145
    }
146
147
    public function getIsBlocked()
148
    {
149
        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...
150
    }
151
152
    /**
153
     * Checks whether server can be operated not.
154
     *
155
     * @throws NotSupportedException
156
     * @return bool
157
     * @see isOperable()
158
     */
159
    public function checkOperable()
160
    {
161
        if (!$this->isOperable()) {
162
            throw new NotSupportedException(Yii::t('hipanel/server', 'Server already has a running task. Can not start new.'));
163
        }
164
        return true;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function scenarioCommands()
171
    {
172
        return [
173
            'reinstall' => 'resetup',
174
            'reset-password' => 'regenRootPassword',
175
        ];
176
    }
177
178
    /**
179
     * During 5 days after the last expiration client is able to refuse server with full refund.
180
     * Method checks, whether 5 days passed.
181
     * @return bool
182
     */
183
    public function canFullRefuse()
184
    {
185
        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...
186
    }
187
188
    public function groupUsesForCharts()
189
    {
190
        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...
191
    }
192
193
    public function getUses()
194
    {
195
        return $this->hasMany(ServerUse::class, ['object_id' => 'id']);
196
    }
197
198
    public function getIps()
199
    {
200
        return $this->hasMany(Ip::class, ['device_id' => 'id'])->join('links');
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function attributeLabels()
207
    {
208
        return $this->mergeAttributeLabels([
209
            'remoteid'            => Yii::t('hipanel/server', 'Remote ID'),
210
            'name'                => Yii::t('hipanel/server', 'Name'),
211
            'dc'                  => Yii::t('hipanel/server', 'DC'),
212
            'net'                 => Yii::t('hipanel/server', 'Switch'),
213
            'kvm'                 => Yii::t('hipanel/server', 'KVM'),
214
            'pdu'                 => Yii::t('hipanel/server', 'APC'),
215
            'rack'                => Yii::t('hipanel/server', 'Rack'),
216
            'ipmi'                => Yii::t('hipanel/server', 'IPMI'),
217
            'status_time'         => Yii::t('hipanel/server', 'Status update time'),
218
            'block_reason_label'  => Yii::t('hipanel/server', 'Block reason label'),
219
            'request_state_label' => Yii::t('hipanel/server', 'Request state label'),
220
            'mac'                 => Yii::t('hipanel/server', 'MAC'),
221
            'ips'                 => Yii::t('hipanel/server', 'IPs'),
222
            'label'               => Yii::t('hipanel/server', 'Internal note'),
223
            'os'                  => Yii::t('hipanel/server', 'OS'),
224
            'comment'             => Yii::t('hipanel/server', 'Comment'),
225
            'hwsummary'           => Yii::t('hipanel/server', 'HW summary'),
226
            'sale_time'           => Yii::t('hipanel/server', 'Sale time'),
227
            'expires'             => Yii::t('hipanel/server', 'Expires'),
228
        ]);
229
    }
230
}
231