Total Complexity | 79 |
Total Lines | 543 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DisplayTab 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 DisplayTab, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class DisplayTab implements TabInterface, DisplayInterface, FormInterface |
||
31 | { |
||
32 | use VisibleCondition, \SleepingOwl\Admin\Traits\Renderable, HtmlAttributes, FormElementsRecursiveIterator; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $label; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $active = false; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $name; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $icon; |
||
53 | |||
54 | /** |
||
55 | * @var Renderable |
||
56 | */ |
||
57 | protected $content; |
||
58 | |||
59 | /** |
||
60 | * @var |
||
61 | */ |
||
62 | protected $badge; |
||
63 | |||
64 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $external_form = false; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $view = 'display.tab'; |
||
73 | |||
74 | /** |
||
75 | * @param Renderable $content |
||
76 | * @param string|null $label |
||
77 | * @param string|null $icon |
||
78 | * @param Badge|string|\Closure|null $badge |
||
79 | */ |
||
80 | public function __construct(Renderable $content, $label = null, $icon = null, $badge = null) |
||
81 | { |
||
82 | $this->content = $content; |
||
83 | |||
84 | if (! is_null($label)) { |
||
85 | $this->setLabel($label); |
||
86 | } |
||
87 | |||
88 | if (! is_null($icon)) { |
||
89 | $this->setIcon($icon); |
||
90 | } |
||
91 | |||
92 | if (! is_null($badge)) { |
||
93 | $this->setBadge($badge); |
||
94 | } |
||
95 | |||
96 | $this->setHtmlAttribute('data-toggle', 'tab'); |
||
97 | $this->setHtmlAttribute('class', 'nav-item nav-link'); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param Badge|string|\Closure|null $badge |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function setBadge($badge) |
||
105 | { |
||
106 | $badgeData = null; |
||
107 | |||
108 | if (is_string($badge) || is_callable($badge) || is_numeric($badge)) { |
||
109 | $badgeData = new Badge(); |
||
110 | $badgeData->setView('_partials.tabs.badge'); |
||
111 | $badgeData->setValue($badge); |
||
112 | } |
||
113 | |||
114 | $this->badge = $badgeData; |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function getBadge() |
||
123 | { |
||
124 | return $this->badge; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getLabel() |
||
131 | { |
||
132 | return $this->label; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param string $label |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function setLabel($label) |
||
141 | { |
||
142 | $this->label = $label; |
||
143 | |||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function isActive() |
||
151 | { |
||
152 | return $this->active; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param bool $active |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function setActive($active = true) |
||
161 | { |
||
162 | $this->active = (bool) $active; |
||
163 | |||
164 | if ($active) { |
||
165 | $this->setHtmlAttribute('class', 'active'); |
||
166 | } |
||
167 | |||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return $this |
||
173 | * @throws \SleepingOwl\Admin\Exceptions\Display\DisplayTabException |
||
174 | * @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException |
||
175 | */ |
||
176 | public function addTabElement() |
||
177 | { |
||
178 | if ($this->content instanceof FormInterface) { |
||
179 | $this->content->addElement( |
||
|
|||
180 | new FormElements([ |
||
181 | (new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()), |
||
182 | ]) |
||
183 | ); |
||
184 | } |
||
185 | |||
186 | if ($this->content instanceof FormElements) { |
||
187 | foreach ($this->content->getElements() as $element) { |
||
188 | if ($element instanceof FormDefault && $element instanceof FormCard) { |
||
189 | $element->addElement( |
||
190 | new FormElements([ |
||
191 | (new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()), |
||
192 | ]) |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | if ($element instanceof FormElements) { |
||
197 | foreach ($element->getElements() as $subElement) { |
||
198 | if ($subElement instanceof FormDefault) { |
||
199 | $subElement->addElement( |
||
200 | new FormElements([ |
||
201 | (new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()), |
||
202 | ]) |
||
203 | ); |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | |||
208 | if ($element instanceof Columns) { |
||
209 | foreach ($element->getElements() as $column) { |
||
210 | if ($column instanceof Column) { |
||
211 | foreach ($column->getElements() as $columnElement) { |
||
212 | if ($columnElement instanceof FormInterface) { |
||
213 | $columnElement->addElement( |
||
214 | new FormElements([ |
||
215 | (new Hidden('sleeping_owl_tab_id'))->setDefaultValue($this->getName()), |
||
216 | ]) |
||
217 | ); |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @return string |
||
231 | * @throws DisplayTabException |
||
232 | */ |
||
233 | public function getName() |
||
234 | { |
||
235 | if (is_null($this->name) && is_null($this->getLabel())) { |
||
236 | throw new DisplayTabException('You should set name or label'); |
||
237 | } |
||
238 | |||
239 | return is_null($this->name) |
||
240 | ? md5($this->getLabel()) |
||
241 | : $this->name; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param string $name |
||
246 | * |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function setName($name) |
||
250 | { |
||
251 | $this->name = $name; |
||
252 | |||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | public function getIcon() |
||
260 | { |
||
261 | return $this->icon; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param string $icon |
||
266 | * |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function setIcon($icon) |
||
270 | { |
||
271 | $this->icon = $icon; |
||
272 | |||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @return Renderable |
||
278 | */ |
||
279 | public function getContent() |
||
280 | { |
||
281 | return $this->content; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param string $class |
||
286 | * |
||
287 | * @return $this |
||
288 | */ |
||
289 | public function setModelClass($class) |
||
290 | { |
||
291 | if (($content = $this->getContent()) instanceof DisplayInterface) { |
||
292 | $content->setModelClass($class); |
||
293 | } |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Initialize tab. |
||
300 | * |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function initialize() |
||
304 | { |
||
305 | if (($content = $this->getContent()) instanceof Initializable) { |
||
306 | $content->initialize(); |
||
307 | } |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param string $action |
||
314 | * |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function setAction($action) |
||
318 | { |
||
319 | if (($content = $this->getContent()) instanceof FormInterface) { |
||
320 | $content->setAction($action); |
||
321 | } |
||
322 | |||
323 | return $this; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @param bool $bool |
||
328 | * |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function setExternalForm($bool) |
||
332 | { |
||
333 | $this->external_form = $bool; |
||
334 | |||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @return bool |
||
340 | */ |
||
341 | public function getExternalForm() |
||
342 | { |
||
343 | return $this->external_form; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param int $id |
||
348 | * |
||
349 | * @return $this |
||
350 | */ |
||
351 | public function setId($id) |
||
352 | { |
||
353 | if (($content = $this->getContent()) instanceof FormInterface) { |
||
354 | $content->setId($id); |
||
355 | } |
||
356 | |||
357 | return $this; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param \Illuminate\Http\Request $request |
||
362 | * @param ModelConfigurationInterface $model |
||
363 | * |
||
364 | * @throws ValidationException |
||
365 | */ |
||
366 | public function validateForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null) |
||
367 | { |
||
368 | if (($content = $this->getContent()) instanceof FormInterface) { |
||
369 | $content->validateForm($request, $model); |
||
370 | } |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Save model. |
||
375 | * |
||
376 | * @param \Illuminate\Http\Request $request |
||
377 | * @param ModelConfigurationInterface $model |
||
378 | * |
||
379 | * @return void |
||
380 | */ |
||
381 | public function saveForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null) |
||
382 | { |
||
383 | if (($content = $this->getContent()) instanceof FormInterface) { |
||
384 | $content->saveForm($request, $model); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Set currently rendered instance. |
||
390 | * |
||
391 | * @param Model $model |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function setModel(Model $model) |
||
396 | { |
||
397 | if (($content = $this->getContent()) instanceof WithModelInterface) { |
||
398 | $content->setModel($model); |
||
399 | } |
||
400 | |||
401 | return $this; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @return Model $model |
||
406 | */ |
||
407 | public function getModel() |
||
408 | { |
||
409 | if (($content = $this->getContent()) instanceof WithModelInterface) { |
||
410 | return $content->getModel(); |
||
411 | } |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Get form item validation rules. |
||
416 | * @return array |
||
417 | */ |
||
418 | public function getValidationRules() |
||
419 | { |
||
420 | if (($content = $this->getContent()) instanceof Validable) { |
||
421 | return $content->getValidationRules(); |
||
422 | } |
||
423 | |||
424 | return []; |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * @return array |
||
429 | */ |
||
430 | public function getValidationMessages() |
||
431 | { |
||
432 | if (($content = $this->getContent()) instanceof Validable) { |
||
433 | return $content->getValidationMessages(); |
||
434 | } |
||
435 | |||
436 | return []; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @return array |
||
441 | */ |
||
442 | public function getValidationLabels() |
||
443 | { |
||
444 | if (($content = $this->getContent()) instanceof Validable) { |
||
445 | return $content->getValidationLabels(); |
||
446 | } |
||
447 | |||
448 | return []; |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * @param \Illuminate\Http\Request $request |
||
453 | * |
||
454 | * @return void |
||
455 | */ |
||
456 | public function save(\Illuminate\Http\Request $request) |
||
457 | { |
||
458 | if (($content = $this->getContent()) instanceof FormElementInterface) { |
||
459 | $content->save($request); |
||
460 | } |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param \Illuminate\Http\Request $request |
||
465 | * |
||
466 | * @return void |
||
467 | */ |
||
468 | public function afterSave(\Illuminate\Http\Request $request) |
||
469 | { |
||
470 | if (($content = $this->getContent()) instanceof FormElementInterface) { |
||
471 | $content->afterSave($request); |
||
472 | } |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * @return mixed |
||
477 | */ |
||
478 | public function getValue() |
||
479 | { |
||
480 | if (($content = $this->getContent()) instanceof FormElementInterface) { |
||
481 | return $content->getValue(); |
||
482 | } |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @return bool |
||
487 | */ |
||
488 | public function isReadonly() |
||
489 | { |
||
490 | if (($content = $this->getContent()) instanceof FormElementInterface) { |
||
491 | return $content->isReadonly(); |
||
492 | } |
||
493 | |||
494 | return false; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * @return bool |
||
499 | */ |
||
500 | public function getVisibled() |
||
501 | { |
||
502 | if (($content = $this->getContent()) instanceof FormElementInterface) { |
||
503 | return $content->getVisibled(); |
||
504 | } |
||
505 | |||
506 | return false; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * @return bool |
||
511 | */ |
||
512 | public function isValueSkipped() |
||
513 | { |
||
514 | if (($content = $this->getContent()) instanceof FormElementInterface) { |
||
515 | return $content->isValueSkipped(); |
||
516 | } |
||
517 | |||
518 | return false; |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * @param string $path |
||
523 | * |
||
524 | * @return FormElementInterface|null |
||
525 | */ |
||
526 | public function getElement($path) |
||
527 | { |
||
528 | if (($content = $this->getContent()) instanceof ElementsInterface) { |
||
529 | return $content->getElement($path); |
||
530 | } |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @return FormElementsCollection |
||
535 | */ |
||
536 | public function getElements() |
||
537 | { |
||
538 | if (($content = $this->getContent()) instanceof ElementsInterface) { |
||
539 | return $content->getElements(); |
||
540 | } |
||
541 | |||
542 | return new FormElementsCollection(); |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * @param array $elements |
||
547 | * |
||
548 | * @return $this |
||
549 | */ |
||
550 | public function setElements(array $elements) |
||
551 | { |
||
552 | if (($content = $this->getContent()) instanceof ElementsInterface) { |
||
553 | $content->setElements($elements); |
||
554 | } |
||
555 | |||
556 | return $this; |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * @return array |
||
561 | * @throws \SleepingOwl\Admin\Exceptions\Display\DisplayTabException |
||
562 | */ |
||
563 | public function toArray() |
||
573 | ]; |
||
574 | } |
||
575 | } |
||
576 |