Completed
Push — master ( baf937...93a1c5 )
by Andrii
11:25
created

Server::scenarioActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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 || !$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...
112
            return false;
113
        }
114
115
        return true;
116
    }
117
118
    public function canEnableVNC()
119
    {
120
        return $this->isVNCSupported() && $this->isStateGood();
121
    }
122
123
    public function isStateGood()
124
    {
125
        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...
126
    }
127
128
    /**
129
     * Checks whether server supports VNC.
130
     *
131
     * @return bool
132
     */
133
    public function isVNCSupported()
134
    {
135
        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...
136
    }
137
138
    /**
139
     * Checks whether server supports root password change.
140
     *
141
     * @return bool
142
     */
143
    public function isPwChangeSupported()
144
    {
145
        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...
146
    }
147
148
    /**
149
     * Checks whether server supports LiveCD.
150
     *
151
     * @return bool
152
     */
153
    public function isLiveCDSupported()
154
    {
155
        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...
156
    }
157
158
    public function getIsBlocked()
159
    {
160
        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...
161
    }
162
163
    /**
164
     * Checks whether server can be operated not.
165
     *
166
     * @throws NotSupportedException
167
     * @return bool
168
     * @see isOperable()
169
     */
170
    public function checkOperable()
171
    {
172
        if (!$this->isOperable()) {
173
            throw new NotSupportedException(Yii::t('hipanel:server', 'Server already has a running task. Can not start new.'));
174
        }
175
        return true;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function scenarioActions()
182
    {
183
        return [
184
            'reinstall' => 'resetup',
185
            'reset-password' => 'regen-root-password',
186
        ];
187
    }
188
189
    /**
190
     * During 5 days after the last expiration client is able to refuse server with full refund.
191
     * Method checks, whether 5 days passed.
192
     * @return bool
193
     */
194
    public function canFullRefuse()
195
    {
196
        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...
197
    }
198
199
    public function groupUsesForCharts()
200
    {
201
        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...
202
    }
203
204
    public function getUses()
205
    {
206
        return $this->hasMany(ServerUse::class, ['object_id' => 'id']);
207
    }
208
209
    public function getIps()
210
    {
211
        return $this->hasMany(Ip::class, ['device_id' => 'id'])->joinWith('links');
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function attributeLabels()
218
    {
219
        return $this->mergeAttributeLabels([
220
            'remoteid'            => Yii::t('hipanel:server', 'Remote ID'),
221
            'name'                => Yii::t('hipanel:server', 'Name'),
222
            'dc'                  => Yii::t('hipanel:server', 'DC'),
223
            'net'                 => Yii::t('hipanel:server', 'Switch'),
224
            'kvm'                 => Yii::t('hipanel:server', 'KVM'),
225
            'pdu'                 => Yii::t('hipanel:server', 'APC'),
226
            'rack'                => Yii::t('hipanel:server', 'Rack'),
227
            'ipmi'                => Yii::t('hipanel:server', 'IPMI'),
228
            'status_time'         => Yii::t('hipanel:server', 'Status update time'),
229
            'block_reason_label'  => Yii::t('hipanel:server', 'Block reason label'),
230
            'request_state_label' => Yii::t('hipanel:server', 'Request state label'),
231
            'mac'                 => Yii::t('hipanel:server', 'MAC'),
232
            'ips'                 => Yii::t('hipanel:server', 'IPs'),
233
            'label'               => Yii::t('hipanel:server', 'Internal note'),
234
            'os'                  => Yii::t('hipanel:server', 'OS'),
235
            'comment'             => Yii::t('hipanel:server', 'Comment'),
236
            'hwsummary'           => Yii::t('hipanel:server', 'HW summary'),
237
            'sale_time'           => Yii::t('hipanel:server', 'Sale time'),
238
            'expires'             => Yii::t('hipanel:server', 'Expires'),
239
        ]);
240
    }
241
}
242