Completed
Push — master ( af4be8...690aba )
by Andrii
12:21
created

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