Completed
Push — master ( dc047d...abe986 )
by Klochok
19:13
created

Server::getBinding()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 0
cts 0
cp 0
cc 2
eloc 4
nc 2
nop 1
crap 6
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',
54
                ],
55
                'safe',
56
            ],
57
            [['switches', 'rack', 'net', 'kvm', 'pdu', 'ipmi'], 'safe'],
58
            [['last_expires', 'expires', 'status_time', 'sale_time'], 'date'],
59
            [
60
                ['state'],
61
                'isOperable',
62
                'on' => [
63
                    'reinstall',
64
                    'reboot',
65
                    'reset',
66
                    'shutdown',
67
                    'power-off',
68
                    'power-on',
69
                    'boot-live',
70
                    'regen-root-password',
71
                    'reset-password',
72
                ],
73
            ],
74
            [
75
                ['id'],
76
                'required',
77
                'on' => [
78
                    'refuse', 'delete', 'enable-autorenewal',
79
                    'enable-vnc', 'set-note', 'set-label',
80
                    'enable-block', 'disable-block',
81
                ],
82
            ],
83
            [
84
                ['id'],
85
                'integer',
86
                'on' => [
87
                    'refuse', 'delete', 'enable-autorenewal',
88
                    'enable-vnc', 'set-note', 'set-label',
89
                    'enable-block', 'disable-block',
90
                ],
91
            ],
92
            [['id', 'osimage'], 'required', 'on' => ['reinstall']],
93
            [['id', 'osimage'], 'required', 'on' => ['boot-live']],
94
            [['type', 'comment'], 'required', 'on' => ['enable-block', 'disable-block']],
95
        ];
96
    }
97
98
    /**
99
     * Determine good server states.
100
     *
101
     * @return array
102
     */
103
    public function goodStates()
104
    {
105
        return [static::STATE_OK, static::STATE_DISABLED];
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    public function isOperable()
112
    {
113
        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...
114
            return false;
115
        }
116
117
        return true;
118
    }
119
120
    public function canEnableVNC()
121
    {
122
        return $this->isVNCSupported() && $this->isStateGood();
123
    }
124
125
    public function isStateGood()
126
    {
127
        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...
128
    }
129
130
    /**
131
     * Checks whether server supports VNC.
132
     *
133
     * @return bool
134
     */
135
    public function isVNCSupported()
136
    {
137
        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...
138
    }
139
140
    /**
141
     * Checks whether server supports root password change.
142
     *
143
     * @return bool
144
     */
145
    public function isPwChangeSupported()
146
    {
147
        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...
148
    }
149
150
    /**
151
     * Checks whether server supports LiveCD.
152
     *
153
     * @return bool
154
     */
155
    public function isLiveCDSupported()
156
    {
157
        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...
158
    }
159
160
    /**
161
     * Check whether server is virtual.
162
     *
163
     * @return bool
164
     */
165
    public function isVirtualDevice()
166
    {
167
        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...
168
    }
169
170
    /**
171
     * Check whether server is dedicated.
172
     *
173
     * @return bool
174
     */
175
    public function isDedicatedDevice()
176
    {
177
        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...
178
    }
179
180
    public function getIsBlocked()
181
    {
182
        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...
183
    }
184
185
    /**
186
     * Checks whether server can be operated not.
187
     *
188
     * @throws NotSupportedException
189
     * @return bool
190
     * @see isOperable()
191
     */
192
    public function checkOperable()
193
    {
194
        if (!$this->isOperable()) {
195
            throw new NotSupportedException(Yii::t('hipanel:server', 'Server already has a running task. Can not start new.'));
196
        }
197
        return true;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function scenarioActions()
204
    {
205
        return [
206
            'reinstall' => 'resetup',
207
            'reset-password' => 'regen-root-password',
208
        ];
209
    }
210
211
    /**
212
     * During 5 days after the last expiration client is able to refuse server with full refund.
213
     * Method checks, whether 5 days passed.
214
     * @return bool
215
     */
216
    public function canFullRefuse()
217
    {
218
        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...
219
    }
220
221
    public function groupUsesForCharts()
222
    {
223
        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...
224
    }
225
226
    public function getUses()
227
    {
228
        return $this->hasMany(ServerUse::class, ['object_id' => 'id']);
229
    }
230
231
    public function getIps()
232
    {
233
        return $this->hasMany(Ip::class, ['device_id' => 'id'])->joinWith('links');
234
    }
235
236
    public function getSwitches()
237
    {
238
        return $this->hasMany(Server::class, ['obj_id' => 'id']);
239
    }
240
241
    public function getBindings()
242
    {
243
        return $this->hasMany(Binding::class, ['device_id' => 'id'])->indexBy('type');
244
    }
245
246
    public function getBinding($type)
247
    {
248
        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...
249
            return null;
250
        }
251
252
        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...
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258
    public function attributeLabels()
259
    {
260
        return $this->mergeAttributeLabels([
261
            'remoteid'            => Yii::t('hipanel:server', 'Remote ID'),
262
            'name'                => Yii::t('hipanel:server', 'Name'),
263
            'dc'                  => Yii::t('hipanel:server', 'DC'),
264
            'net'                 => Yii::t('hipanel:server', 'Switch'),
265
            'kvm'                 => Yii::t('hipanel:server', 'KVM'),
266
            'pdu'                 => Yii::t('hipanel:server', 'APC'),
267
            'rack'                => Yii::t('hipanel:server', 'Rack'),
268
            'ipmi'                => Yii::t('hipanel:server', 'IPMI'),
269
            'status_time'         => Yii::t('hipanel:server', 'Status update time'),
270
            'block_reason_label'  => Yii::t('hipanel:server', 'Block reason label'),
271
            'request_state_label' => Yii::t('hipanel:server', 'Request state label'),
272
            'mac'                 => Yii::t('hipanel:server', 'MAC'),
273
            'ips'                 => Yii::t('hipanel:server', 'IPs'),
274
            'label'               => Yii::t('hipanel:server', 'Internal note'),
275
            'os'                  => Yii::t('hipanel:server', 'OS'),
276
            'comment'             => Yii::t('hipanel:server', 'Comment'),
277
            'hwsummary'           => Yii::t('hipanel:server', 'HW summary'),
278
            'sale_time'           => Yii::t('hipanel:server', 'Sale time'),
279
            'expires'             => Yii::t('hipanel:server', 'Expires'),
280
        ]);
281
    }
282
}
283
284