Passed
Push — master ( d23c33...74888d )
by Andrii
03:48
created

AssignHubsForm::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\server\forms;
4
5
use hipanel\modules\server\models\Server;
6
use Yii;
7
8
/**
9
 * Class AssignHubsForm
10
 */
11
class AssignHubsForm extends Server
12
{
13
    use \hipanel\base\ModelTrait;
14
15
    /**
16
     * @inheritdoc
17
     */
18
    public static function tableName()
19
    {
20
        return 'server';
21
    }
22
23
    /**
24
     * Create AttachHubsForm model from Server model
25
     *
26
     * @param Server $server
27
     * @return AssignHubsForm
28
     */
29
    public static function fromServer(Server $server): AssignHubsForm
30
    {
31
        $attributes = array_merge($server->getAttributes(), []);
32
        foreach ($server->bindings as $binding) {
0 ignored issues
show
Bug Best Practice introduced by
The property bindings does not exist on hipanel\modules\server\models\Server. Since you implemented __get, consider adding a @property annotation.
Loading history...
33
            $attributes[$binding->type . '_id'] = $binding->switch_id;
34
            $attributes[$binding->type . '_port'] = $binding->port;
35
        }
36
37
        return new self(array_merge($attributes, ['scenario' => 'default']));
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function rules()
44
    {
45
        return array_merge(parent::rules(), [
46
            [['id'], 'required'],
47
            [['net_id', 'kvm_id', 'pdu_id', 'rack_id', 'pdu2_id', 'nic2_id', 'ipmi_id'], 'integer'],
48
            [['net_port', 'kvm_port', 'pdu_port', 'rack_port', 'pdu2_port', 'nic2_port', 'ipmi_port'], 'string'],
49
        ]);
50
    }
51
52
    public function attributeLabels()
53
    {
54
        return array_merge(parent::attributeLabels(), [
55
            'pdu2' => Yii::t('hipanel:server', 'APC 2'),
56
            'nic2' => Yii::t('hipanel:server', 'Switch 2'),
57
        ]);
58
    }
59
60
    /**
61
     * For compatibility with [[hiqdev\hiart\Collection]]
62
     *
63
     * @param $defaultScenario
64
     * @param array $data
65
     * @param array $options
66
     * @return mixed
67
     */
68
    public function batchQuery($defaultScenario, $data = [], array $options = [])
69
    {
70
        $map = [
71
            'update' => 'assign-hubs',
72
        ];
73
        $scenario = isset($map[$defaultScenario]) ? $map[$defaultScenario] : $defaultScenario;
74
75
        return (new Server)->batchQuery($scenario, $data, $options);
76
    }
77
}
78
79