We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 49 |
Total Lines | 341 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Buttons often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Buttons, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | trait Buttons |
||
9 | { |
||
10 | // ------------ |
||
11 | // BUTTONS |
||
12 | // ------------ |
||
13 | |||
14 | /** |
||
15 | * Order the CRUD buttons. If certain button names are missing from the given order array |
||
16 | * they will be pushed to the end of the button collection. |
||
17 | * |
||
18 | * @param string $stack Stack where the buttons belongs. Options: top, line, bottom. |
||
19 | * @param array $order Ordered names of the buttons. ['update', 'delete', 'show'] |
||
20 | * @param bool $abortIfNotExist If true, an exception will be thrown if a button name is not found. |
||
21 | */ |
||
22 | public function orderButtons(string $stack, array $order, bool $abortIfNotExist = true): void |
||
23 | { |
||
24 | $newButtons = collect([]); |
||
|
|||
25 | $otherButtons = collect([]); |
||
26 | |||
27 | // we get the buttons that belong to the specified stack |
||
28 | $stackButtons = $this->buttons()->reject(function ($item) use ($stack, $otherButtons) { |
||
29 | if ($item->stack != $stack) { |
||
30 | // if the button does not belong to this stack we just add it for merging later |
||
31 | $otherButtons->push($item); |
||
32 | |||
33 | return true; |
||
34 | } |
||
35 | |||
36 | return false; |
||
37 | }); |
||
38 | |||
39 | // we parse the ordered buttons |
||
40 | collect($order)->each(function ($btnKey) use ($abortIfNotExist, $newButtons, $stackButtons) { |
||
41 | if (!$button = $stackButtons->where('name', $btnKey)->first()) { |
||
42 | if ($abortIfNotExist) { |
||
43 | abort(500, 'Button name [«' . $btnKey . '»] not found.', ['developer-error-exception']); |
||
44 | } |
||
45 | } |
||
46 | $newButtons->push($button); |
||
47 | }); |
||
48 | |||
49 | // if the ordered buttons are less than the total number of buttons in the stack |
||
50 | // we add the remaining buttons to the end of the ordered ones |
||
51 | if (count($newButtons) < count($stackButtons)) { |
||
52 | foreach ($stackButtons as $button) { |
||
53 | if (! $newButtons->where('name', $button->name)->first()) { |
||
54 | $newButtons->push($button); |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | |||
59 | $this->setOperationSetting('buttons', $newButtons->merge($otherButtons)); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Add a button to the CRUD table view. |
||
64 | * |
||
65 | * @param string $stack Where should the button be visible? Options: top, line, bottom. |
||
66 | * @param string $name The name of the button. Unique. |
||
67 | * @param string $type Type of button: view or model_function. |
||
68 | * @param string $content The HTML for the button. |
||
69 | * @param bool|string $position Position on the stack: beginning or end. If false, the position will be |
||
70 | * 'beginning' for the line stack or 'end' otherwise. |
||
71 | * @param bool $replaceExisting True if a button with the same name on the given stack should be replaced. |
||
72 | * @return \Backpack\CRUD\app\Library\CrudPanel\CrudButton The new CRUD button. |
||
73 | */ |
||
74 | public function addButton($stack, $name, $type, $content, $position = false, $replaceExisting = true) |
||
75 | { |
||
76 | if ($replaceExisting) { |
||
77 | $this->removeButton($name, $stack); |
||
78 | } |
||
79 | |||
80 | return new CrudButton($name, $stack, $type, $content, $position); |
||
81 | } |
||
82 | |||
83 | public function addCrudButton(CrudButton $crudButton) |
||
84 | { |
||
85 | $this->setOperationSetting('buttons', $this->buttons()->push($crudButton)); |
||
86 | } |
||
87 | |||
88 | public function addButtonFromModelFunction($stack, $name, $model_function_name, $position = false) |
||
89 | { |
||
90 | $this->addButton($stack, $name, 'model_function', $model_function_name, $position); |
||
91 | } |
||
92 | |||
93 | public function addButtonFromView($stack, $name, $view, $position = false) |
||
94 | { |
||
95 | $view = 'crud::buttons.'.$view; |
||
96 | |||
97 | $this->addButton($stack, $name, 'view', $view, $position); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return Collection |
||
102 | */ |
||
103 | public function buttons() |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Modify the attributes of a button. |
||
110 | * |
||
111 | * @param string $name The button name. |
||
112 | * @param array $modifications The attributes and their new values. |
||
113 | * @return CrudButton The button that has suffered the changes, for daisychaining methods. |
||
114 | */ |
||
115 | public function modifyButton($name, $modifications = null) |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Remove a button from the CRUD panel. |
||
137 | * |
||
138 | * @param string $name Button name. |
||
139 | * @param string $stack Optional stack name. |
||
140 | */ |
||
141 | public function removeButton($name, $stack = null) |
||
142 | { |
||
143 | $this->setOperationSetting('buttons', $this->buttons()->reject(function ($button) use ($name, $stack) { |
||
144 | return $stack == null ? $button->name == $name : ($button->stack == $stack) && ($button->name == $name); |
||
145 | })); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param array $names Button names |
||
150 | * @param string|null $stack Optional stack name. |
||
151 | */ |
||
152 | public function removeButtons($names, $stack = null) |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | public function removeAllButtons() |
||
162 | { |
||
163 | $this->setOperationSetting('buttons', collect()); |
||
164 | } |
||
165 | |||
166 | public function removeAllButtonsFromStack($stack) |
||
167 | { |
||
168 | $this->setOperationSetting('buttons', $this->buttons()->reject(function ($button) use ($stack) { |
||
169 | return $button->stack == $stack; |
||
170 | })); |
||
171 | } |
||
172 | |||
173 | public function removeButtonFromStack($name, $stack) |
||
177 | })); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Move the most recently added button before or after the given target button. Default is before. |
||
182 | * |
||
183 | * @param string|array $target The target button name or array. |
||
184 | * @param string|array $where Move 'before' or 'after' the target. |
||
185 | * @param string|array $destination The destination button name or array. |
||
186 | */ |
||
187 | public function moveButton($target, $where, $destination) |
||
188 | { |
||
189 | $targetButton = $this->firstButtonWhere('name', $target); |
||
190 | |||
191 | $destinationButton = $this->firstButtonWhere('name', $destination); |
||
192 | $destinationKey = $this->getButtonKey($destination); |
||
193 | $newDestinationKey = ($where == 'before' ? $destinationKey : $destinationKey + 1); |
||
194 | |||
195 | $newButtons = $this->buttons()->filter(function ($value, $key) use ($target) { |
||
196 | return $value->name != $target; |
||
197 | }); |
||
198 | |||
199 | if (! $targetButton) { |
||
200 | return; |
||
201 | } |
||
202 | |||
203 | if (! $destinationButton) { |
||
204 | return; |
||
205 | } |
||
206 | |||
207 | $firstSlice = $newButtons->slice(0, $newDestinationKey); |
||
208 | $lastSlice = $newButtons->slice($newDestinationKey, null); |
||
209 | |||
210 | $newButtons = $firstSlice->push($targetButton); |
||
211 | |||
212 | $lastSlice->each(function ($item, $key) use ($newButtons) { |
||
213 | $newButtons->push($item); |
||
214 | }); |
||
215 | |||
216 | $this->setOperationSetting('buttons', $newButtons); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Check if a button exists, by any given attribute. |
||
221 | * |
||
222 | * @param string $attribute Attribute name on that button definition array. |
||
223 | * @param string $value Value of that attribute on that button definition array. |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function hasButtonWhere($attribute, $value) |
||
227 | { |
||
228 | return $this->buttons()->contains($attribute, $value); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Get the first button where a given attribute has the given value. |
||
233 | * |
||
234 | * @param string $attribute Attribute name on that button definition array. |
||
235 | * @param string $value Value of that attribute on that button definition array. |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function firstButtonWhere($attribute, $value) |
||
239 | { |
||
240 | return $this->buttons()->firstWhere($attribute, $value); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Get button key from its name. |
||
245 | * |
||
246 | * @param string $buttonName Button name. |
||
247 | * @return string |
||
248 | */ |
||
249 | public function getButtonKey($name) |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Return the buttons for a given stack. |
||
262 | */ |
||
263 | public function getButtonsForStack(string $stack): Collection |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Add a new button to the current CRUD operation. |
||
270 | */ |
||
271 | public function button(string|array $nameOrAttributes): CrudButton |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Reorganize a collection by putting certain elements at the beginning |
||
278 | * |
||
279 | * @param string $stack |
||
280 | * @param array|string $startButtons |
||
281 | * @param bool $abortIfNotExist |
||
282 | */ |
||
283 | public function buttonsStartWith(string $stack, array|string $startButtons, bool $abortIfNotExist = true): void |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Réorganise une collection en mettant certains éléments à la fin |
||
315 | * |
||
316 | * @param string $stack |
||
317 | * @param array|string $endButtons |
||
318 | * @param bool $abortIfNotExist |
||
319 | */ |
||
320 | public |
||
349 | } |
||
350 | |||
351 | } |
||
352 |