Completed
Push — master ( f70da4...b9e2f3 )
by Andrii
04:31
created

Server::checkOperable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
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', 'mac',
48
                    'vnc',
49
                    'note', 'label', 'hwsummary',
50
                ],
51
                'safe',
52
            ],
53
            [['switches', 'rack', 'net', 'kvm', 'pdu', 'ipmi'], 'safe'],
54
            [['last_expires', 'expires', 'status_time', 'sale_time'], 'date'],
55
            [
56
                ['state'],
57
                'isOperable',
58
                'on' => [
59
                    'reinstall',
60
                    'reboot',
61
                    'reset',
62
                    'shutdown',
63
                    'power-off',
64
                    'power-on',
65
                    'boot-live',
66
                    'regen-root-password',
67
                    'reset-password',
68
                ],
69
            ],
70
            [
71
                ['id'],
72
                'required',
73
                'on' => [
74
                    'refuse', 'delete', 'enable-autorenewal',
75
                    'enable-vnc', 'set-note', 'set-label',
76
                    'enable-block', 'disable-block',
77
                ],
78
            ],
79
            [
80
                ['id'],
81
                'integer',
82
                'on' => [
83
                    'refuse', 'delete', 'enable-autorenewal',
84
                    'enable-vnc', 'set-note', 'set-label',
85
                    'enable-block', 'disable-block',
86
                ],
87
            ],
88
            [['id', 'osimage', 'panel'], 'required', 'on' => ['reinstall']],
89
            [['id', 'osimage'], 'required', 'on' => ['boot-live']],
90
            [['type', 'comment'], 'required', 'on' => ['enable-block']],
91
            [['comment'], 'safe', 'on' => ['disable-block']],
92
        ];
93
    }
94
95
    /**
96
     * Determine good server states.
97
     *
98
     * @return array
99
     */
100
    public function goodStates()
101
    {
102
        return [static::STATE_OK, static::STATE_DISABLED];
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function isOperable()
109
    {
110
        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...
111
            return false;
112
        }
113
        return true;
114
    }
115
116
    /**
117
     * Checks whether server supports VNC.
118
     *
119
     * @return bool
120
     */
121
    public function isVNCSupported()
122
    {
123
        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...
124
    }
125
126
    /**
127
     * Checks whether server supports root password change.
128
     *
129
     * @return bool
130
     */
131
    public function isPwChangeSupported()
132
    {
133
        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...
134
    }
135
136
    /**
137
     * Checks whether server supports LiveCD.
138
     *
139
     * @return bool
140
     */
141
    public function isLiveCDSupported()
142
    {
143
        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...
144
    }
145
146
    public function getIsBlocked()
147
    {
148
        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...
149
    }
150
151
    /**
152
     * Checks whether server can be operated not.
153
     *
154
     * @throws NotSupportedException
155
     * @return bool
156
     * @see isOperable()
157
     */
158
    public function checkOperable()
159
    {
160
        if (!$this->isOperable()) {
161
            throw new NotSupportedException(Yii::t('hipanel/server', 'Server already has a running task. Can not start new.'));
162
        }
163
        return true;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function scenarioCommands()
170
    {
171
        return [
172
            'reinstall' => 'resetup',
173
            'reset-password' => 'regenRootPassword',
174
        ];
175
    }
176
177
    /**
178
     * During 5 days after the last expiration client is able to refuse server with full refund.
179
     * Method checks, whether 5 days passed.
180
     * @return bool
181
     */
182
    public function canFullRefuse()
183
    {
184
        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...
185
    }
186
187
    public function groupUsesForCharts()
188
    {
189
        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...
190
    }
191
192
    public function getUses()
193
    {
194
        return $this->hasMany(ServerUse::class, ['object_id' => 'id']);
195
    }
196
197
    public function getIps()
198
    {
199
        return $this->hasMany(Ip::class, ['device_id' => 'id'])->join('links');
0 ignored issues
show
Bug introduced by
The call to join() misses a required argument $table.

This check looks for function calls that miss required arguments.

Loading history...
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function attributeLabels()
206
    {
207
        return $this->mergeAttributeLabels([
208
            'remoteid'            => Yii::t('hipanel/server', 'Remote ID'),
209
            'name'                => Yii::t('hipanel/server', 'Name'),
210
            'dc'                  => Yii::t('hipanel/server', 'DC'),
211
            'net'                 => Yii::t('hipanel/server', 'Switch'),
212
            'kvm'                 => Yii::t('hipanel/server', 'KVM'),
213
            'pdu'                 => Yii::t('hipanel/server', 'APC'),
214
            'rack'                => Yii::t('hipanel/server', 'Rack'),
215
            'ipmi'                => Yii::t('hipanel/server', 'IPMI'),
216
            'status_time'         => Yii::t('hipanel/server', 'Status update time'),
217
            'block_reason_label'  => Yii::t('hipanel/server', 'Block reason label'),
218
            'request_state_label' => Yii::t('hipanel/server', 'Request state label'),
219
            'mac'                 => Yii::t('hipanel/server', 'MAC'),
220
            'ips'                 => Yii::t('hipanel/server', 'IPs'),
221
            'label'               => Yii::t('hipanel/server', 'Internal note'),
222
            'os'                  => Yii::t('hipanel/server', 'OS'),
223
            'comment'             => Yii::t('hipanel/server', 'Comment'),
224
        ]);
225
    }
226
}
227