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

AssignSwitchTrait::generateUniqueValidators()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 32
rs 8.9777
c 0
b 0
f 0
cc 6
nc 3
nop 0
1
<?php
2
3
namespace hipanel\modules\server\models\traits;
4
5
use hipanel\modules\server\models\AssignSwitchInterface;
6
use hipanel\modules\server\models\Binding;
7
use hipanel\modules\server\models\Server;
8
use Yii;
9
use yii\base\InvalidConfigException;
10
11
trait AssignSwitchTrait
12
{
13
    /**
14
     * List of switch types
15
     * Example: ['net', 'kvm', 'pdu', 'rack', 'console']
16
     *
17
     * @var array
18
     */
19
    public $switchVariants = [];
20
21
    /**
22
     * @param AssignSwitchInterface $originalModel
23
     *
24
     * @return AssignSwitchInterface
25
     */
26
    public static function fromOriginalModel(AssignSwitchInterface $originalModel): AssignSwitchInterface
27
    {
28
        $attributes = array_merge($originalModel->getAttributes(), []);
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on hipanel\modules\server\m...s\AssignSwitchInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hipanel\modules\server\m...s\AssignSwitchInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $attributes = array_merge($originalModel->/** @scrutinizer ignore-call */ getAttributes(), []);
Loading history...
29
        $model = new static(['scenario' => 'default']);
0 ignored issues
show
Unused Code introduced by
The call to hipanel\modules\server\m...tchTrait::__construct() has too many arguments starting with array('scenario' => 'default'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $model = /** @scrutinizer ignore-call */ new static(['scenario' => 'default']);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
30
        foreach ($originalModel->bindings as $binding) {
0 ignored issues
show
Bug introduced by
Accessing bindings on the interface hipanel\modules\server\m...s\AssignSwitchInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
31
            $attribute = $binding->typeWithNo . '_id';
32
            if ($model->hasAttribute($attribute)) {
0 ignored issues
show
Bug introduced by
It seems like hasAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            if ($model->/** @scrutinizer ignore-call */ hasAttribute($attribute)) {
Loading history...
33
                $attributes[$binding->typeWithNo . '_id'] = $binding->switch_id;
34
                $attributes[$binding->typeWithNo . '_port'] = $binding->port;
35
            }
36
        }
37
        $model->setAttributes($attributes);
0 ignored issues
show
Bug introduced by
It seems like setAttributes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        $model->/** @scrutinizer ignore-call */ 
38
                setAttributes($attributes);
Loading history...
38
39
        return $model;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model returns the type hipanel\modules\server\m...raits\AssignSwitchTrait which is incompatible with the type-hinted return hipanel\modules\server\m...s\AssignSwitchInterface.
Loading history...
40
    }
41
42
    public function defaultSwitchRules(): array
43
    {
44
        $variantIds = [];
45
        $variantPorts = [];
46
        foreach ($this->switchVariants as $variant) {
47
            $variantIds[] = $variant . '_id';
48
            $variantPorts[] = $variant . '_port';
49
        }
50
        return [
51
            [['id'], 'required'],
52
            [$variantIds, 'integer'],
53
            [$variantPorts, 'string'],
54
        ];
55
    }
56
57
    /**
58
     * For compatibility with [[hiqdev\hiart\Collection]]
59
     *
60
     * @param $defaultScenario
61
     * @param array $data
62
     * @param array $options
63
     *
64
     * @return mixed
65
     */
66
    public function batchQuery($defaultScenario, $data = [], array $options = [])
67
    {
68
        $map = [
69
            'update' => 'assign-hubs',
70
        ];
71
        $scenario = isset($map[$defaultScenario]) ? $map[$defaultScenario] : $defaultScenario;
72
73
        return parent::batchQuery($scenario, $data, $options);
74
    }
75
76
    /**
77
     * Added to model's rules list of switch pairs
78
     *
79
     * @return array
80
     * @throws InvalidConfigException
81
     */
82
    protected function generateUniqueValidators(): array
83
    {
84
        if (empty($this->switchVariants)) {
85
            throw new InvalidConfigException('Please specify `switchVariants` array to use AssignSwitchTrait::generateUniqueValidators()');
86
        }
87
        $rules = [];
88
89
        foreach ($this->switchVariants as $variant) {
90
            $rules[] = [
91
                [$variant . '_port'],
92
                function ($attribute, $params, $validator) use ($variant) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
                function ($attribute, /** @scrutinizer ignore-unused */ $params, $validator) use ($variant) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
                function ($attribute, $params, /** @scrutinizer ignore-unused */ $validator) use ($variant) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
                    if ($this->{$attribute} && $this->{$variant . '_id'}) {
94
                        $query = Binding::find();
95
                        $query->andWhere(['port' => $this->{$attribute}]);
96
                        $query->andWhere(['switch_id' => $this->{$variant . '_id'}]);
97
                        $query->andWhere(['ne', 'base_device_id', $this->id]);
98
                        /** @var Binding[] $bindings */
99
                        $bindings = $query->all();
100
                        if (!empty($bindings)) {
101
                            $binding = reset($bindings);
102
                            $this->addError($attribute, Yii::t('hipanel:server', '{switch}::{port} already taken by {device}', [
0 ignored issues
show
Bug introduced by
It seems like addError() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
                            $this->/** @scrutinizer ignore-call */ 
103
                                   addError($attribute, Yii::t('hipanel:server', '{switch}::{port} already taken by {device}', [
Loading history...
103
                                'switch' => $binding->switch_name,
104
                                'port' => $binding->port,
105
                                'device' => $binding->device_name,
106
                            ]));
107
                        }
108
                    }
109
                },
110
            ];
111
        }
112
113
        return $rules;
114
    }
115
}
116