Total Complexity | 127 |
Total Lines | 778 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 3 | Features | 1 |
Complex classes like AdminController 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 AdminController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class AdminController extends Controller |
||
23 | { |
||
24 | /** |
||
25 | * @var \DaveJamesMiller\Breadcrumbs\Manager |
||
26 | */ |
||
27 | protected $breadcrumbs; |
||
28 | |||
29 | /** |
||
30 | * @var |
||
31 | */ |
||
32 | protected $breadCrumbsData; |
||
33 | |||
34 | /** |
||
35 | * @var AdminInterface |
||
36 | */ |
||
37 | protected $admin; |
||
38 | |||
39 | /** |
||
40 | * @var |
||
41 | */ |
||
42 | private $parentBreadcrumb = 'home'; |
||
43 | |||
44 | /** |
||
45 | * @var Application |
||
46 | */ |
||
47 | public $app; |
||
48 | |||
49 | /** |
||
50 | * @var |
||
51 | */ |
||
52 | protected $envPolicy; |
||
53 | |||
54 | /** |
||
55 | * AdminController constructor. |
||
56 | * |
||
57 | * @param Request $request |
||
58 | * @param AdminInterface $admin |
||
59 | * @param Application $application |
||
60 | */ |
||
61 | public function __construct(Request $request, AdminInterface $admin, Application $application) |
||
62 | { |
||
63 | $this->app = $application; |
||
64 | $this->admin = $admin; |
||
65 | $this->breadcrumbs = $admin->template()->breadcrumbs(); |
||
66 | |||
67 | if ($this->envPolicy = config('sleeping_owl.env_editor_policy')) { |
||
68 | $this->envPolicy = new $this->envPolicy; |
||
69 | } |
||
70 | |||
71 | $admin->navigation()->setCurrentUrl($request->getUri()); |
||
|
|||
72 | |||
73 | if (! $this->breadcrumbs->exists('home')) { |
||
74 | $this->breadcrumbs->register('home', function (Generator $breadcrumbs) { |
||
75 | $breadcrumbs->push(trans('sleeping_owl::lang.dashboard'), route('admin.dashboard')); |
||
76 | }); |
||
77 | } |
||
78 | |||
79 | $this->breadCrumbsData = []; |
||
80 | |||
81 | if ($currentPage = $admin->navigation()->getCurrentPage()) { |
||
82 | foreach ($currentPage->getPathArray() as $page) { |
||
83 | $this->breadCrumbsData[] = [ |
||
84 | 'id' => $page['id'], |
||
85 | 'title' => $page['title'], |
||
86 | 'url' => $page['url'], |
||
87 | 'parent' => $this->parentBreadcrumb, |
||
88 | ]; |
||
89 | |||
90 | $this->parentBreadcrumb = $page['id']; |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getParentBreadcrumb() |
||
99 | { |
||
100 | return $this->parentBreadcrumb; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param string $parentBreadcrumb |
||
105 | */ |
||
106 | public function setParentBreadcrumb($parentBreadcrumb) |
||
107 | { |
||
108 | $this->parentBreadcrumb = $parentBreadcrumb; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
113 | */ |
||
114 | public function getDashboard() |
||
115 | { |
||
116 | return $this->renderContent( |
||
117 | $this->admin->template()->view('dashboard'), |
||
118 | trans('sleeping_owl::lang.dashboard') |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
124 | */ |
||
125 | public function getEnvEditor() |
||
126 | { |
||
127 | $envFile = app()->environmentFilePath(); |
||
128 | $envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW)); |
||
129 | |||
130 | /** |
||
131 | * Use filter masks. |
||
132 | * @param $key |
||
133 | * @return bool |
||
134 | */ |
||
135 | $envContent = $envContent->filter(function ($value, $key) { |
||
136 | return ! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key); |
||
137 | }); |
||
138 | |||
139 | $envContent = $envContent->filter(function ($value, $key) { |
||
140 | return $this->validatePolicy('display', $key); |
||
141 | }); |
||
142 | |||
143 | $envContent = $envContent->map(function ($value, $key) { |
||
144 | return (object) [ |
||
145 | 'value' => $value, |
||
146 | 'editable' => $this->validatePolicy('edit', $key), |
||
147 | 'deletable' => $this->validatePolicy('delete', $key), |
||
148 | ]; |
||
149 | }); |
||
150 | |||
151 | return $this->renderContent( |
||
152 | $this->admin->template()->view('env_editor', ['data' => $envContent]), |
||
153 | trans('sleeping_owl::lang.env_editor.title') |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param $permission |
||
159 | * @param $key |
||
160 | * @return bool |
||
161 | */ |
||
162 | protected function validatePolicy($permission, $key) |
||
163 | { |
||
164 | return ($this->envPolicy && ((method_exists($this->envPolicy, $permission) |
||
165 | && $this->envPolicy->$permission(\Auth::user(), $key) !== false))) |
||
166 | || ! method_exists($this->envPolicy, $permission) || ! $this->envPolicy || $this->validateBeforePolicy($key); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param $key |
||
171 | * @return bool |
||
172 | */ |
||
173 | protected function validateBeforePolicy($key) |
||
174 | { |
||
175 | return ($this->envPolicy && (method_exists($this->envPolicy, 'before')) |
||
176 | && $this->envPolicy->before(\Auth::user(), $key) == true) |
||
177 | || ! method_exists($this->envPolicy, 'before') || ! $this->envPolicy; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param Request $request |
||
182 | * @return \Illuminate\Http\RedirectResponse |
||
183 | */ |
||
184 | public function postEnvEditor(Request $request) |
||
185 | { |
||
186 | $envFile = app()->environmentFilePath(); |
||
187 | $envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW)); |
||
188 | |||
189 | $requestContent = collect($request->input('variables')); |
||
190 | $removeContent = collect(); |
||
191 | |||
192 | foreach ($envContent as $key => $value) { |
||
193 | if (! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key)) { |
||
194 | if ($requestContent->has($key)) { |
||
195 | if ($this->validatePolicy('edit', $key)) { |
||
196 | $envContent[$key] = $requestContent[$key]['value']; |
||
197 | } |
||
198 | $requestContent->forget($key); |
||
199 | } else { |
||
200 | $envContent->forget($key); |
||
201 | $removeContent->put($key, null); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | foreach ($requestContent as $key => $value) { |
||
207 | if (! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key) |
||
208 | && $this->validatePolicy('create', $key)) { |
||
209 | $this->writeEnvData($key, $value['value'], 1); |
||
210 | } |
||
211 | $requestContent->forget($key); |
||
212 | } |
||
213 | |||
214 | foreach ($removeContent as $key => $value) { |
||
215 | if ($this->validatePolicy('delete', $key)) { |
||
216 | $this->writeEnvData($key); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | foreach ($envContent as $key => $value) { |
||
221 | $this->writeEnvData($key, $value); |
||
222 | } |
||
223 | |||
224 | return redirect()->back()->with('success_message', 'Env Updated'); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param $key |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function filterKey($key) |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param $key |
||
247 | * @param null $data |
||
248 | * @param bool $new |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function writeEnvData($key, $data = null, $new = null) |
||
252 | { |
||
253 | $envFile = app()->environmentFilePath(); |
||
254 | $str = file_get_contents($envFile); |
||
255 | |||
256 | if (is_null($data)) { |
||
257 | $str = preg_replace("/$key=.*/m", '', $str); |
||
258 | file_put_contents($envFile, $str); |
||
259 | |||
260 | return false; |
||
261 | } |
||
262 | |||
263 | if (is_null($new)) { |
||
264 | $str = preg_replace("/$key=.*/m", "$key=$data", $str); |
||
265 | file_put_contents($envFile, $str); |
||
266 | |||
267 | return false; |
||
268 | } |
||
269 | |||
270 | $str = $str."\r\n$key=$data"; |
||
271 | file_put_contents($envFile, $str); |
||
272 | |||
273 | return true; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param ModelConfigurationInterface $model |
||
278 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
279 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
280 | */ |
||
281 | public function getDisplay(ModelConfigurationInterface $model) |
||
282 | { |
||
283 | if (! $model->isDisplayable()) { |
||
284 | abort(404); |
||
285 | } |
||
286 | |||
287 | $display = $model->fireDisplay(); |
||
288 | |||
289 | $this->registerBreadcrumbs($model); |
||
290 | |||
291 | return $this->render($model, $display); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param ModelConfigurationInterface $model |
||
296 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
297 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
298 | */ |
||
299 | public function getCreate(ModelConfigurationInterface $model) |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @param ModelConfigurationInterface $model |
||
315 | * @param Request $request |
||
316 | * |
||
317 | * @return \Illuminate\Http\RedirectResponse |
||
318 | */ |
||
319 | public function postStore(ModelConfigurationInterface $model, Request $request) |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @param ModelConfigurationInterface $model |
||
388 | * @param $id |
||
389 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
390 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
391 | */ |
||
392 | public function getEdit(ModelConfigurationInterface $model, $id) |
||
393 | { |
||
394 | $item = $model->getRepository()->find($id); |
||
395 | |||
396 | if (is_null($item) || ! $model->isEditable($item)) { |
||
397 | abort(404); |
||
398 | } |
||
399 | |||
400 | $edit = $model->fireEdit($id); |
||
401 | |||
402 | $this->registerBreadcrumbs($model); |
||
403 | $this->registerBreadcrumb($model->getEditTitle(), $this->parentBreadcrumb); |
||
404 | |||
405 | return $this->render($model, $edit, $model->getEditTitle()); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @param ModelConfigurationInterface $model |
||
410 | * @param Request $request |
||
411 | * @param int $id |
||
412 | * |
||
413 | * @return \Illuminate\Http\RedirectResponse |
||
414 | * |
||
415 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
416 | */ |
||
417 | public function postUpdate(ModelConfigurationInterface $model, Request $request, $id) |
||
418 | { |
||
419 | /** @var FormInterface $editForm */ |
||
420 | $editForm = $model->fireEdit($id); |
||
421 | $item = $editForm->getModel(); |
||
422 | |||
423 | if (is_null($item) || ! $model->isEditable($item)) { |
||
424 | abort(404); |
||
425 | } |
||
426 | |||
427 | $nextAction = $request->input('next_action'); |
||
428 | |||
429 | $backUrl = $this->getBackUrl($request); |
||
430 | |||
431 | if ($editForm instanceof FormInterface) { |
||
432 | try { |
||
433 | $editForm->validateForm($request, $model); |
||
434 | |||
435 | if ($editForm->saveForm($request, $model) === false) { |
||
436 | return redirect()->back()->with([ |
||
437 | '_redirectBack' => $backUrl, |
||
438 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
439 | ]); |
||
440 | } |
||
441 | } catch (ValidationException $exception) { |
||
442 | return redirect()->back() |
||
443 | ->withErrors($exception->validator) |
||
444 | ->withInput() |
||
445 | ->with([ |
||
446 | '_redirectBack' => $backUrl, |
||
447 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
448 | ]); |
||
449 | } |
||
450 | } |
||
451 | |||
452 | $redirectPolicy = $model->getRedirect(); |
||
453 | |||
454 | if ($nextAction == 'save_and_continue') { |
||
455 | $response = redirect()->back()->with([ |
||
456 | '_redirectBack' => $backUrl, |
||
457 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
458 | ]); |
||
459 | |||
460 | if ($redirectPolicy->get('edit') == 'display') { |
||
461 | $response = redirect()->to( |
||
462 | $model->getDisplayUrl() |
||
463 | )->with([ |
||
464 | '_redirectBack' => $backUrl, |
||
465 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
466 | ]); |
||
467 | } |
||
468 | } elseif ($nextAction == 'save_and_create') { |
||
469 | $response = redirect()->to($model->getCreateUrl($request->except([ |
||
470 | '_redirectBack', |
||
471 | '_token', |
||
472 | 'url', |
||
473 | 'next_action', |
||
474 | ])))->with([ |
||
475 | '_redirectBack' => $backUrl, |
||
476 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
477 | ]); |
||
478 | } else { |
||
479 | $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl())); |
||
480 | } |
||
481 | |||
482 | return $response->with('success_message', $model->getMessageOnUpdate()); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @param ModelConfigurationInterface $model |
||
487 | * @param Request $request |
||
488 | * |
||
489 | * @return bool |
||
490 | * |
||
491 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
492 | */ |
||
493 | public function inlineEdit(ModelConfigurationInterface $model, Request $request) |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * @param ModelConfigurationInterface $model |
||
567 | * @param Request $request |
||
568 | * @param int $id |
||
569 | * @return \Illuminate\Http\RedirectResponse |
||
570 | */ |
||
571 | public function deleteDelete(ModelConfigurationInterface $model, Request $request, $id) |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * @param ModelConfigurationInterface $model |
||
595 | * @param Request $request |
||
596 | * @param int $id |
||
597 | * @return \Illuminate\Http\RedirectResponse |
||
598 | */ |
||
599 | public function deletedAll(ModelConfigurationInterface $model, Request $request) |
||
600 | { |
||
601 | if (is_null($request->_id)) { |
||
602 | return redirect()->back(); |
||
603 | } |
||
604 | |||
605 | $items = $request->_id; |
||
606 | |||
607 | foreach ($items as $id) { |
||
608 | $item = $model->getRepository()->find($id); |
||
609 | |||
610 | if (! $item) { |
||
611 | return response()->Json(['error' => 'Haven`t row']); |
||
612 | } |
||
613 | |||
614 | if (isset($item->deleted_at) && $item->deleted_at) { |
||
615 | $model->getRepository()->forceDelete($id); |
||
616 | } else { |
||
617 | $model->getRepository()->delete($id); |
||
618 | } |
||
619 | } |
||
620 | |||
621 | $response = redirect() |
||
622 | ->to($request |
||
623 | ->input('_redirectBack', $model->getDisplayUrl())); |
||
624 | |||
625 | return $response |
||
626 | ->with('success_message', $model->getMessageOnDelete()); |
||
627 | } |
||
628 | |||
629 | /** |
||
630 | * @param ModelConfigurationInterface $model |
||
631 | * @param Request $request |
||
632 | * @param int $id |
||
633 | * |
||
634 | * @return \Illuminate\Http\RedirectResponse |
||
635 | * |
||
636 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
637 | */ |
||
638 | public function deleteDestroy(ModelConfigurationInterface $model, Request $request, $id) |
||
639 | { |
||
640 | if (! $model->isRestorableModel()) { |
||
641 | abort(404); |
||
642 | } |
||
643 | |||
644 | $item = $model->getRepository()->findOnlyTrashed($id); |
||
645 | |||
646 | if (is_null($item) || ! $model->isRestorable($item)) { |
||
647 | abort(404); |
||
648 | } |
||
649 | |||
650 | $model->fireDestroy($id); |
||
651 | |||
652 | if ($model->fireEvent('destroying', true, $item, $request) === false) { |
||
653 | return redirect()->back(); |
||
654 | } |
||
655 | |||
656 | $model->getRepository()->forceDelete($id); |
||
657 | |||
658 | $model->fireEvent('destroyed', false, $item, $request); |
||
659 | |||
660 | return redirect($request->input('_redirectBack', back()->getTargetUrl())) |
||
661 | ->with('success_message', $model->getMessageOnDestroy()); |
||
662 | } |
||
663 | |||
664 | /** |
||
665 | * @param ModelConfigurationInterface|ModelConfiguration $model |
||
666 | * @param Request $request |
||
667 | * @param int $id |
||
668 | * |
||
669 | * @return \Illuminate\Http\RedirectResponse |
||
670 | * |
||
671 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
672 | */ |
||
673 | public function postRestore(ModelConfigurationInterface $model, Request $request, $id) |
||
674 | { |
||
675 | if (! $model->isRestorableModel()) { |
||
676 | abort(404); |
||
677 | } |
||
678 | |||
679 | $item = $model->getRepository()->findOnlyTrashed($id); |
||
680 | |||
681 | if (is_null($item) || ! $model->isRestorable($item)) { |
||
682 | abort(404); |
||
683 | } |
||
684 | |||
685 | $model->fireRestore($id); |
||
686 | |||
687 | if ($model->fireEvent('restoring', true, $item, $request) === false) { |
||
688 | return redirect()->back(); |
||
689 | } |
||
690 | |||
691 | $model->getRepository()->restore($id); |
||
692 | |||
693 | $model->fireEvent('restored', false, $item, $request); |
||
694 | |||
695 | return redirect($request->input('_redirectBack', back()->getTargetUrl())) |
||
696 | ->with('success_message', $model->getMessageOnRestore()); |
||
697 | } |
||
698 | |||
699 | /** |
||
700 | * @param ModelConfigurationInterface $model |
||
701 | * @param Renderable|RedirectResponse|string $content |
||
702 | * @param string|null $title |
||
703 | * |
||
704 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Illuminate\Http\RedirectResponse |
||
705 | */ |
||
706 | public function render(ModelConfigurationInterface $model, $content, $title = null) |
||
707 | { |
||
708 | if ($content instanceof RedirectResponse) { |
||
709 | return $content; |
||
710 | } |
||
711 | |||
712 | if ($content instanceof Renderable) { |
||
713 | $content = $content->render(); |
||
714 | } |
||
715 | |||
716 | if (is_null($title)) { |
||
717 | $title = $model->getTitle(); |
||
718 | } |
||
719 | |||
720 | return $this->admin->template()->view('_layout.inner') |
||
721 | ->with('title', $title) |
||
722 | ->with('content', $content) |
||
723 | ->with('breadcrumbKey', $this->parentBreadcrumb); |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * @param Renderable|string $content |
||
728 | * @param string|null $title |
||
729 | * |
||
730 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
731 | */ |
||
732 | public function renderContent($content, $title = null) |
||
733 | { |
||
734 | if ($content instanceof Renderable) { |
||
735 | $content = $content->render(); |
||
736 | } |
||
737 | |||
738 | return $this->admin->template()->view('_layout.inner') |
||
739 | ->with('title', $title) |
||
740 | ->with('content', $content) |
||
741 | ->with('breadcrumbKey', $this->parentBreadcrumb); |
||
742 | } |
||
743 | |||
744 | /** |
||
745 | * @param Request $request |
||
746 | * |
||
747 | * @return null|string |
||
748 | */ |
||
749 | protected function getBackUrl(Request $request) |
||
750 | { |
||
751 | if (($backUrl = $request->input('_redirectBack')) == \URL::previous()) { |
||
752 | $backUrl = null; |
||
753 | $request->merge(['_redirectBack' => $backUrl]); |
||
754 | } |
||
755 | |||
756 | return $backUrl; |
||
757 | } |
||
758 | |||
759 | public function getWildcard() |
||
760 | { |
||
761 | abort(404); |
||
762 | } |
||
763 | |||
764 | /** |
||
765 | * @param $title |
||
766 | * @param $parent |
||
767 | * @param $name |
||
768 | * @param $url |
||
769 | * |
||
770 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
771 | */ |
||
772 | protected function registerBreadcrumb($title, $parent, $name = 'render', $url = null) |
||
780 | } |
||
781 | |||
782 | /** |
||
783 | * @param ModelConfigurationInterface $model |
||
784 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
785 | */ |
||
786 | protected function registerBreadcrumbs(ModelConfigurationInterface $model) |
||
800 | } |
||
801 | } |
||
802 |