Completed
Push — master ( e77292...ac8c9e )
by Andrii
06:26 queued 02:57
created

AssignHubsForm::generateUniqueValidators()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hipanel\modules\server\forms;
4
5
use hipanel\modules\server\models\Binding;
6
use hipanel\modules\server\models\Server;
7
use Yii;
8
use yii\db\Query;
9
10
/**
11
 * Class AssignHubsForm
12
 */
13
class AssignHubsForm extends Server
14
{
15
    use \hipanel\base\ModelTrait;
16
17
    /**
18
     * @inheritdoc
19
     */
20
    public static function tableName()
21
    {
22
        return 'server';
23
    }
24
25
    /**
26
     * Create AttachHubsForm model from Server model
27
     *
28
     * @param Server $server
29
     * @return AssignHubsForm
30
     */
31
    public static function fromServer(Server $server): AssignHubsForm
32
    {
33
        $attributes = array_merge($server->getAttributes(), []);
34
        $model = new self(['scenario' => 'default']);
35
        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...
36
            if ($model->hasAttribute($binding->typeWithNo . '_id')) {
37
                $attributes[$binding->typeWithNo . '_id'] = $binding->switch_id;
38
                $attributes[$binding->typeWithNo . '_port'] = $binding->port;
39
            }
40
        }
41
        $model->setAttributes($attributes);
42
43
        return $model;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function rules()
50
    {
51
        return array_merge(parent::rules(), [
52
            [['id'], 'required'],
53
            [['net_id', 'kvm_id', 'pdu_id', 'rack_id', 'pdu2_id', 'nic2_id', 'ipmi_id'], 'integer'],
54
            [['net_port', 'kvm_port', 'pdu_port', 'rack_port', 'pdu2_port', 'nic2_port', 'ipmi_port'], 'string'],
55
        ], $this->generateUniqueValidators());
56
    }
57
58
    public function attributeLabels()
59
    {
60
        return array_merge(parent::attributeLabels(), [
61
            'pdu2' => Yii::t('hipanel:server', 'APC 2'),
62
            'nic2' => Yii::t('hipanel:server', 'Switch 2'),
63
        ]);
64
    }
65
66
    /**
67
     * For compatibility with [[hiqdev\hiart\Collection]]
68
     *
69
     * @param $defaultScenario
70
     * @param array $data
71
     * @param array $options
72
     * @return mixed
73
     */
74
    public function batchQuery($defaultScenario, $data = [], array $options = [])
75
    {
76
        $map = [
77
            'update' => 'assign-hubs',
78
        ];
79
        $scenario = isset($map[$defaultScenario]) ? $map[$defaultScenario] : $defaultScenario;
80
81
        return (new Server)->batchQuery($scenario, $data, $options);
82
    }
83
84
    private function generateUniqueValidators(): array
85
    {
86
        $rules = [];
87
        foreach (['net', 'kmv', 'pdu', 'rack', 'pdu2', 'nic2', 'ipmi'] as $variant) {
88
            $rules[] = [
89
                [$variant . '_id', $variant . '_port'],
90
                'unique', 'targetClass' => Binding::class,
91
                'targetAttribute' => [
92
                    $variant . '_port' => 'port', $variant . '_id' => 'switch_id',
93
                ],
94
                'filter' => function ($query) {
95
                    /** @var Query $query */
96
                    $query->andWhere(['check_only' => true]);
97
                    $query->andWhere(['ne', 'device_id', $this->id]);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on hipanel\modules\server\forms\AssignHubsForm. Since you implemented __get, consider adding a @property annotation.
Loading history...
98
                },
99
            ];
100
        }
101
102
        return $rules;
103
    }
104
}
105
106