Completed
Push — master ( 678f4c...d13b92 )
by Andrii
10:30
created

Hub::find()   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 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\models;
12
13
use hipanel\base\ModelTrait;
14
use hipanel\modules\server\models\query\HubQuery;
15
use hipanel\modules\server\models\traits\AssignSwitchTrait;
16
use hipanel\modules\server\validators\MacValidator;
17
use hiqdev\hiart\ActiveQuery;
18
use Yii;
19
20
class Hub extends \hipanel\base\Model implements AssignSwitchInterface
21
{
22
    use ModelTrait, AssignSwitchTrait;
0 ignored issues
show
introduced by
The trait hipanel\modules\server\m...raits\AssignSwitchTrait requires some properties which are not provided by hipanel\modules\server\models\Hub: $device_name, $typeWithNo, $bindings, $switch_name, $switch_id, $id, $port
Loading history...
23
24
    const SCENARIO_CREATE = 'create';
25
    const SCENARIO_UPDATE = 'update';
26
    const SCENARIO_OPTIONS = 'options';
27
28
    public function rules()
29
    {
30
        return array_merge(parent::rules(), [
31
            [['id', 'access_id', 'type_id', 'state_id', 'buyer_id', 'units', 'tariff_id', 'client_id'], 'integer'],
32
            [['tariff'], 'safe'],
33
            [[
34
                'name', 'dc', 'mac', 'remoteid', 'note', 'ip', 'type_label', 'buyer', 'note', 'inn', 'model',
35
                'community', 'login', 'traf_server_id', 'order_no', 'password', 'ports_num', 'traf_server_id',
36
                'vlan_server_id', 'community', 'snmp_version_id', 'digit_capacity_id', 'nic_media', 'base_port_no',
37
                'oob_key', 'traf_server_id_label', 'vlan_server_id_label', 'type'
38
            ], 'string'],
39
            [['virtual'], 'boolean'],
40
41
            [['tariff_id', 'sale_time'], 'required', 'on' => ['sale']],
42
43
            // Create and update
44
            [['type_id', 'name'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
45
            [['name', 'mac', 'ip'], 'unique', 'filter' => function ($query) {
46
                $query->andWhere(['ne', 'id', $this->id]);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on hipanel\modules\server\models\Hub. Since you implemented __get, consider adding a @property annotation.
Loading history...
47
            }, 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
48
            [['id'], 'integer', 'on' => self::SCENARIO_UPDATE],
49
            [['inn', 'mac', 'ip', 'model', 'order_no', 'note'], 'string', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
50
51
            // set Options
52
            [[
53
                'id', 'inn', 'model', 'login', 'password', 'ports_num', 'community',
54
                'snmp_version_id', 'digit_capacity_id', 'nic_media', 'base_port_no', 'base_port_no',
55
            ], 'safe', 'on' => 'options'],
56
            [['traf_server_id', 'vlan_server_id'], 'integer', 'on' => self::SCENARIO_OPTIONS],
57
58
            [['ip'], 'ip', 'on' => ['create', 'update', 'options']],
59
            [['mac'], MacValidator::class],
60
        ]);
61
    }
62
63
    public function attributeLabels()
64
    {
65
        return array_merge(parent::attributeLabels(), [
66
            'type_id' => Yii::t('hipanel', 'Type'),
67
            'buyer_id' => Yii::t('hipanel:server:hub', 'Buyer'),
68
            'buyer' => Yii::t('hipanel:server:hub', 'Buyer'),
69
            'inn' => Yii::t('hipanel:server:hub', 'INN'),
70
            'inn_ilike' => Yii::t('hipanel:server:hub', 'INN'),
71
            'model' => Yii::t('hipanel:server:hub', 'Model'),
72
            'order_no' => Yii::t('hipanel:server:hub', 'Order No.'),
73
            'login' => Yii::t('hipanel:server:hub', 'Login'),
74
            'password' => Yii::t('hipanel:server:hub', 'Password'),
75
            'ports_num' => Yii::t('hipanel:server:hub', 'Number of ports'),
76
            'traf_server_id' => Yii::t('hipanel:server:hub', 'Server where traf is counted'),
77
            'vlan_server_id' => Yii::t('hipanel:server:hub', 'Server in same VLAN'),
78
            'community' => Yii::t('hipanel:server:hub', 'Community'),
79
            'snmp_version_id' => Yii::t('hipanel:server:hub', 'SNMP version'),
80
            'digit_capacity_id' => Yii::t('hipanel:server:hub', 'Digit capacity'),
81
            'nic_media' => Yii::t('hipanel:server:hub', 'NIC media'),
82
            'base_port_no' => Yii::t('hipanel:server:hub', 'Base port no'),
83
            'oob_key' => Yii::t('hipanel:server:hub', 'OOB license key'),
84
            'mac' => Yii::t('hipanel:server:hub', 'MAC address'),
85
            'name' => Yii::t('hipanel:server:hub', 'Name'),
86
            'note' => Yii::t('hipanel:server:hub', 'Note'),
87
            'net' => Yii::t('hipanel:server', 'Switch'),
88
            'kvm' => Yii::t('hipanel:server', 'KVM'),
89
            'pdu' => Yii::t('hipanel:server', 'APC'),
90
            'rack_like' => Yii::t('hipanel:server', 'Rack'),
91
            'ipmi' => Yii::t('hipanel:server', 'IPMI'),
92
            'name_ilike' => Yii::t('hipanel:server:hub', 'Switch'),
93
            'sale_time' => Yii::t('hipanel:server', 'Sale time'),
94
        ]);
95
    }
96
97
    public function getBindings()
98
    {
99
        return $this->hasMany(Binding::class, ['device_id' => 'id'])->indexBy('type');
100
    }
101
102
    public function getBinding($type)
103
    {
104
        if (!isset($this->bindings[$type])) {
0 ignored issues
show
Bug Best Practice introduced by
The property bindings does not exist on hipanel\modules\server\models\Hub. Since you implemented __get, consider adding a @property annotation.
Loading history...
105
            return null;
106
        }
107
108
        return $this->bindings[$type];
109
    }
110
111
    public function getHardwareSettings(): ActiveQuery
112
    {
113
        return $this->hasOne(HardwareSettings::class, ['id' => 'id']);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     * @return HubQuery
119
     */
120
    public static function find($options = [])
121
    {
122
        return new HubQuery(get_called_class(), [
123
            'options' => $options,
124
        ]);
125
    }
126
}
127