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

Hub::getBinding()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace hipanel\modules\server\models;
4
5
use hipanel\modules\server\validators\MacValidator;
6
use Yii;
7
8
class Hub extends \hipanel\base\Model
9
{
10
    use \hipanel\base\ModelTrait;
11
12
    const SCENARIO_CREATE = 'create';
13
    const SCENARIO_UPDATE = 'update';
14
    const SCENARIO_OPTIONS = 'options';
15
16
    public function rules()
17
    {
18
        return array_merge(parent::rules(), [
19
            [['id', 'access_id', 'type_id', 'state_id', 'buyer_id', 'units'], 'integer'],
20
            [[
21
                'name', 'dc', 'mac', 'remoteid', 'note', 'ip', 'type_label', 'buyer', 'note', 'inn', 'model',
22
                'community', 'login', 'traf_server_id', 'order_no', 'password', 'ports_num', 'traf_server_id',
23
                'vlan_server_id', 'community', 'snmp_version_id', 'digit_capacity_id', 'nic_media', 'base_port_no',
24
                'oob_key', 'traf_server_id_label', 'vlan_server_id_label',
25
            ], 'string'],
26
            [['virtual'], 'boolean'],
27
28
            // Create and update
29
            [['type_id', 'name'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
30
            [['id'], 'integer', 'on' => self::SCENARIO_UPDATE],
31
            [['inn', 'mac', 'ip', 'model', 'order_no', 'note'], 'string', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
32
33
            // set Options
34
            [[
35
                'id', 'inn', 'model', 'login', 'password', 'ports_num', 'community',
36
                'snmp_version_id', 'digit_capacity_id', 'nic_media', 'base_port_no', 'base_port_no',
37
            ], 'safe', 'on' => 'options'],
38
            [['traf_server_id', 'vlan_server_id'], 'integer', 'on' => self::SCENARIO_OPTIONS],
39
40
            [['ip'], 'ip', 'on' => ['create', 'update', 'options']],
41
            [['mac'], MacValidator::class],
42
        ]);
43
    }
44
45
    public function attributeLabels()
46
    {
47
        return array_merge(parent::attributeLabels(), [
48
            'type_id' => Yii::t('hipanel', 'Type'),
49
            'buyer_id' => Yii::t('hipanel:server:hub', 'Buyer'),
50
            'inn' => Yii::t('hipanel:server:hub', 'INN'),
51
            'model' => Yii::t('hipanel:server:hub', 'Model'),
52
            'order_no' => Yii::t('hipanel:server:hub', 'Order No.'),
53
            'login' => Yii::t('hipanel:server:hub', 'Login'),
54
            'password' => Yii::t('hipanel:server:hub', 'Password'),
55
            'ports_num' => Yii::t('hipanel:server:hub', 'Number of ports'),
56
            'traf_server_id' => Yii::t('hipanel:server:hub', 'Server where traf is counted'),
57
            'vlan_server_id' => Yii::t('hipanel:server:hub', 'Server in same VLAN'),
58
            'community' => Yii::t('hipanel:server:hub', 'Community'),
59
            'snmp_version_id' => Yii::t('hipanel:server:hub', 'SNMP version'),
60
            'digit_capacity_id' => Yii::t('hipanel:server:hub', 'Digit capacity'),
61
            'nic_media' => Yii::t('hipanel:server:hub', 'NIC media'),
62
            'base_port_no' => Yii::t('hipanel:server:hub', 'Base port no'),
63
            'oob_key' => Yii::t('hipanel:server:hub', 'OOB license key'),
64
            'mac' => Yii::t('hipanel:server:hub', 'MAC address'),
65
            'name' => Yii::t('hipanel:server:hub', 'Name'),
66
            'note' => Yii::t('hipanel:server:hub', 'Note'),
67
            'net' => Yii::t('hipanel:server', 'Switch'),
68
            'kvm' => Yii::t('hipanel:server', 'KVM'),
69
            'pdu' => Yii::t('hipanel:server', 'APC'),
70
            'rack' => Yii::t('hipanel:server', 'Rack'),
71
            'ipmi' => Yii::t('hipanel:server', 'IPMI'),
72
        ]);
73
    }
74
75
    public function getBindings()
76
    {
77
        return $this->hasMany(Binding::class, ['device_id' => 'id'])->indexBy('type');
78
    }
79
80
    public function getBinding($type)
81
    {
82
        if (!isset($this->bindings[$type])) {
0 ignored issues
show
Documentation introduced by
The property bindings does not exist on object<hipanel\modules\server\models\Hub>. 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...
83
            return null;
84
        }
85
86
        return $this->bindings[$type];
0 ignored issues
show
Documentation introduced by
The property bindings does not exist on object<hipanel\modules\server\models\Hub>. 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...
87
    }
88
}
89