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-2019, HiQDev (http://hiqdev.com/) |
||||||
9 | */ |
||||||
10 | |||||||
11 | namespace hipanel\modules\server\models\traits; |
||||||
12 | |||||||
13 | use hipanel\modules\server\forms\AssignHubsForm; |
||||||
14 | use hipanel\modules\server\forms\AssignSwitchesForm; |
||||||
15 | use hipanel\modules\server\models\AssignSwitchInterface; |
||||||
16 | use hipanel\modules\server\models\Binding; |
||||||
17 | use Yii; |
||||||
18 | use yii\base\InvalidConfigException; |
||||||
19 | |||||||
20 | trait AssignSwitchTrait |
||||||
21 | { |
||||||
22 | /** |
||||||
23 | * List of switch types |
||||||
24 | * Example: ['net', 'kvm', 'pdu', 'rack', 'console']. |
||||||
25 | * |
||||||
26 | * @var array |
||||||
27 | */ |
||||||
28 | protected $switchVariants = []; |
||||||
29 | |||||||
30 | /** |
||||||
31 | * @param AssignSwitchInterface $originalModel |
||||||
32 | * |
||||||
33 | * @return AssignSwitchInterface |
||||||
34 | */ |
||||||
35 | public static function fromOriginalModel(AssignSwitchInterface $originalModel): AssignSwitchInterface |
||||||
36 | { |
||||||
37 | $attributes = array_merge($originalModel->getAttributes(), []); |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
38 | $model = new static(['scenario' => 'default']); |
||||||
0 ignored issues
–
show
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
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...
|
|||||||
39 | foreach ($originalModel->bindings as $binding) { |
||||||
0 ignored issues
–
show
|
|||||||
40 | $attribute = $binding->typeWithNo . '_id'; |
||||||
41 | if ($model->hasAttribute($attribute)) { |
||||||
0 ignored issues
–
show
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
Loading history...
|
|||||||
42 | $attributes[$binding->typeWithNo . '_id'] = $binding->switch_id; |
||||||
43 | $attributes[$binding->typeWithNo . '_port'] = $binding->port; |
||||||
44 | } |
||||||
45 | } |
||||||
46 | $model->setAttributes($attributes); |
||||||
0 ignored issues
–
show
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
Loading history...
|
|||||||
47 | |||||||
48 | return $model; |
||||||
0 ignored issues
–
show
|
|||||||
49 | } |
||||||
50 | |||||||
51 | public function defaultSwitchRules(): array |
||||||
52 | { |
||||||
53 | $variantIds = []; |
||||||
54 | $variantPorts = []; |
||||||
55 | foreach ($this->getSwitchVariants() as $variant) { |
||||||
56 | $variantIds[] = $variant . '_id'; |
||||||
57 | $variantPorts[] = $variant . '_port'; |
||||||
58 | } |
||||||
59 | |||||||
60 | return [ |
||||||
61 | [['id'], 'required'], |
||||||
62 | [$variantIds, 'integer'], |
||||||
63 | [$variantPorts, 'string'], |
||||||
64 | ]; |
||||||
65 | } |
||||||
66 | |||||||
67 | /** |
||||||
68 | * For compatibility with [[hiqdev\hiart\Collection]]. |
||||||
69 | * |
||||||
70 | * @param $defaultScenario |
||||||
71 | * @param array $data |
||||||
72 | * @param array $options |
||||||
73 | * |
||||||
74 | * @return mixed |
||||||
75 | */ |
||||||
76 | public function batchQuery($defaultScenario, $data = [], array $options = []) |
||||||
77 | { |
||||||
78 | $map = [ |
||||||
79 | 'update' => 'assign-hubs', |
||||||
80 | ]; |
||||||
81 | $scenario = isset($map[$defaultScenario]) ? $map[$defaultScenario] : $defaultScenario; |
||||||
82 | |||||||
83 | return parent::batchQuery($scenario, $data, $options); |
||||||
84 | } |
||||||
85 | |||||||
86 | /** |
||||||
87 | * This method decides which `assigns` will be offered in the form based on the type of the current model |
||||||
88 | * |
||||||
89 | * @return array |
||||||
90 | */ |
||||||
91 | public function getSwitchVariants(): array |
||||||
92 | { |
||||||
93 | $map = [ |
||||||
94 | 'rack' => ['location'], |
||||||
95 | 'location' => ['location'], |
||||||
96 | ]; |
||||||
97 | /** @var AssignSwitchesForm|AssignHubsForm $this */ |
||||||
98 | if ($this instanceof AssignSwitchesForm && isset($map[$this->type])) { |
||||||
0 ignored issues
–
show
The property
type does not exist on hipanel\modules\server\forms\AssignSwitchesForm . Since you implemented __get , consider adding a @property annotation.
Loading history...
|
|||||||
99 | return $map[$this->type]; |
||||||
100 | } |
||||||
101 | |||||||
102 | return $this->switchVariants; |
||||||
0 ignored issues
–
show
The property
$switchVariants is declared protected in hipanel\modules\server\forms\AssignSwitchesForm . Since you implement __get , consider adding a @property or @property-read.
Loading history...
The property
$switchVariants is declared protected in hipanel\modules\server\forms\AssignHubsForm . Since you implement __get , consider adding a @property or @property-read.
Loading history...
|
|||||||
103 | } |
||||||
104 | |||||||
105 | /** |
||||||
106 | * Added to model's rules list of switch pairs. |
||||||
107 | * |
||||||
108 | * @return array |
||||||
109 | * @throws InvalidConfigException |
||||||
110 | */ |
||||||
111 | protected function generateUniqueValidators(): array |
||||||
112 | { |
||||||
113 | if (empty($this->switchVariants)) { |
||||||
114 | throw new InvalidConfigException('Please specify `switchVariants` array to use AssignSwitchTrait::generateUniqueValidators()'); |
||||||
115 | } |
||||||
116 | $rules = []; |
||||||
117 | |||||||
118 | foreach ($this->getSwitchVariants() as $variant) { |
||||||
119 | $rules[] = [ |
||||||
120 | [$variant . '_port'], |
||||||
121 | function ($attribute, $params, $validator) use ($variant) { |
||||||
0 ignored issues
–
show
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
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...
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
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...
|
|||||||
122 | if ($this->{$attribute} && $this->{$variant . '_id'}) { |
||||||
123 | $query = Binding::find(); |
||||||
124 | $query->andWhere(['port' => $this->{$attribute}]); |
||||||
125 | $query->andWhere(['switch_id' => $this->{$variant . '_id'}]); |
||||||
126 | $query->andWhere(['ne', 'base_device_id', $this->id]); |
||||||
127 | /** @var Binding[] $bindings */ |
||||||
128 | $bindings = $query->all(); |
||||||
129 | if (!empty($bindings)) { |
||||||
130 | $binding = reset($bindings); |
||||||
131 | $this->addError($attribute, Yii::t('hipanel:server', '{switch}::{port} already taken by {device}', [ |
||||||
0 ignored issues
–
show
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
Loading history...
|
|||||||
132 | 'switch' => $binding->switch_name, |
||||||
133 | 'port' => $binding->port, |
||||||
134 | 'device' => $binding->device_name, |
||||||
135 | ])); |
||||||
136 | } |
||||||
137 | } |
||||||
138 | }, |
||||||
139 | ]; |
||||||
140 | } |
||||||
141 | |||||||
142 | return $rules; |
||||||
143 | } |
||||||
144 | } |
||||||
145 |