Checks if the types of returned expressions are compatible with the hinted types.
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(), []); |
||
38 | $model = new static(['scenario' => 'default']); |
||
39 | foreach ($originalModel->bindings as $binding) { |
||
40 | $attribute = $binding->typeWithNo . '_id'; |
||
41 | if ($model->hasAttribute($attribute)) { |
||
42 | $attributes[$binding->typeWithNo . '_id'] = $binding->switch_id; |
||
43 | $attributes[$binding->typeWithNo . '_port'] = $binding->port; |
||
44 | } |
||
45 | } |
||
46 | $model->setAttributes($attributes); |
||
47 | |||
48 | return $model; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
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])) { |
||
99 | return $map[$this->type]; |
||
100 | } |
||
101 | |||
102 | return $this->switchVariants; |
||
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) { |
||
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}', [ |
||
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 |