Total Complexity | 105 |
Total Lines | 682 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
21 | class AdminController extends Controller |
||
22 | { |
||
23 | /** |
||
24 | * @var \DaveJamesMiller\Breadcrumbs\Manager |
||
25 | */ |
||
26 | protected $breadcrumbs; |
||
27 | |||
28 | /** |
||
29 | * @var |
||
30 | */ |
||
31 | protected $breadCrumbsData; |
||
32 | |||
33 | /** |
||
34 | * @var AdminInterface |
||
35 | */ |
||
36 | protected $admin; |
||
37 | |||
38 | /** |
||
39 | * @var |
||
40 | */ |
||
41 | private $parentBreadcrumb = 'home'; |
||
42 | |||
43 | /** |
||
44 | * @var Application |
||
45 | */ |
||
46 | public $app; |
||
47 | |||
48 | /** |
||
49 | * AdminController constructor. |
||
50 | * |
||
51 | * @param Request $request |
||
52 | * @param AdminInterface $admin |
||
53 | * @param Application $application |
||
54 | */ |
||
55 | public function __construct(Request $request, AdminInterface $admin, Application $application) |
||
56 | { |
||
57 | $this->app = $application; |
||
58 | $this->admin = $admin; |
||
59 | $this->breadcrumbs = $admin->template()->breadcrumbs(); |
||
60 | |||
61 | $admin->navigation()->setCurrentUrl($request->getUri()); |
||
|
|||
62 | |||
63 | if (! $this->breadcrumbs->exists('home')) { |
||
64 | $this->breadcrumbs->register('home', function (Generator $breadcrumbs) { |
||
65 | $breadcrumbs->push(trans('sleeping_owl::lang.dashboard'), route('admin.dashboard')); |
||
66 | }); |
||
67 | } |
||
68 | |||
69 | $this->breadCrumbsData = []; |
||
70 | |||
71 | if ($currentPage = $admin->navigation()->getCurrentPage()) { |
||
72 | foreach ($currentPage->getPathArray() as $page) { |
||
73 | $this->breadCrumbsData[] = [ |
||
74 | 'id' => $page['id'], |
||
75 | 'title' => $page['title'], |
||
76 | 'url' => $page['url'], |
||
77 | 'parent' => $this->parentBreadcrumb, |
||
78 | ]; |
||
79 | |||
80 | $this->parentBreadcrumb = $page['id']; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getParentBreadcrumb() |
||
89 | { |
||
90 | return $this->parentBreadcrumb; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param string $parentBreadcrumb |
||
95 | */ |
||
96 | public function setParentBreadcrumb($parentBreadcrumb) |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
103 | */ |
||
104 | public function getDashboard() |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
114 | */ |
||
115 | public function getEnvEditor() |
||
116 | { |
||
117 | $envFile = app()->environmentFilePath(); |
||
118 | $envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW)); |
||
119 | |||
120 | /** |
||
121 | * Use filter masks. |
||
122 | * @param $key |
||
123 | * @return bool |
||
124 | */ |
||
125 | $envContent = $envContent->filter(function ($value, $key) { |
||
126 | return ! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key); |
||
127 | }); |
||
128 | |||
129 | return $this->renderContent( |
||
130 | $this->admin->template()->view('env_editor', ['data' => $envContent]), |
||
131 | trans('sleeping_owl::lang.env_editor.title') |
||
132 | ); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param Request $request |
||
137 | * @return \Illuminate\Http\RedirectResponse |
||
138 | */ |
||
139 | public function postEnvEditor(Request $request) |
||
140 | { |
||
141 | $envFile = app()->environmentFilePath(); |
||
142 | $envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW)); |
||
143 | |||
144 | $requestContent = collect($request->input('variables')); |
||
145 | $removeContent = collect(); |
||
146 | |||
147 | foreach ($envContent as $key => $value) { |
||
148 | if (! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key)) { |
||
149 | if ($requestContent->has($key)) { |
||
150 | $envContent[$key] = $requestContent[$key]['value']; |
||
151 | $requestContent->forget($key); |
||
152 | } else { |
||
153 | $envContent->forget($key); |
||
154 | $removeContent->put($key, null); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | foreach ($requestContent as $key => $value) { |
||
160 | if (! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key)) { |
||
161 | $this->writeEnvData($key, $value['value'], 1); |
||
162 | } |
||
163 | $requestContent->forget($key); |
||
164 | } |
||
165 | |||
166 | foreach ($removeContent as $key => $value) { |
||
167 | $this->writeEnvData($key); |
||
168 | } |
||
169 | |||
170 | foreach ($envContent as $key => $value) { |
||
171 | $this->writeEnvData($key, $value); |
||
172 | } |
||
173 | |||
174 | return redirect()->back()->with('success_message', 'Env Updated'); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param $key |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function filterKey($key) |
||
182 | { |
||
183 | foreach (config('sleeping_owl.env_editor_excluded_keys') as $val) { |
||
184 | if (strpos($val, '*') !== false) { |
||
185 | $val = str_replace('*', '', $val); |
||
186 | if (strpos($key, $val) !== false) { |
||
187 | return true; |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | |||
192 | return false; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param $key |
||
197 | * @param null $data |
||
198 | * @param bool $new |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function writeEnvData($key, $data = null, $new = null) |
||
202 | { |
||
203 | $envFile = app()->environmentFilePath(); |
||
204 | $str = file_get_contents($envFile); |
||
205 | |||
206 | if (is_null($data)) { |
||
207 | $str = preg_replace("/$key=.*/m", '', $str); |
||
208 | file_put_contents($envFile, $str); |
||
209 | |||
210 | return false; |
||
211 | } |
||
212 | |||
213 | if (is_null($new)) { |
||
214 | $str = preg_replace("/$key=.*/m", "$key=$data", $str); |
||
215 | file_put_contents($envFile, $str); |
||
216 | |||
217 | return false; |
||
218 | } |
||
219 | |||
220 | $str = $str."\r\n$key=$data"; |
||
221 | file_put_contents($envFile, $str); |
||
222 | |||
223 | return true; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param ModelConfigurationInterface $model |
||
228 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
229 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
230 | */ |
||
231 | public function getDisplay(ModelConfigurationInterface $model) |
||
232 | { |
||
233 | if (! $model->isDisplayable()) { |
||
234 | abort(404); |
||
235 | } |
||
236 | |||
237 | $display = $model->fireDisplay(); |
||
238 | |||
239 | $this->registerBreadcrumbs($model); |
||
240 | |||
241 | return $this->render($model, $display); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param ModelConfigurationInterface $model |
||
246 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
247 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
248 | */ |
||
249 | public function getCreate(ModelConfigurationInterface $model) |
||
250 | { |
||
251 | if (! $model->isCreatable()) { |
||
252 | abort(404); |
||
253 | } |
||
254 | |||
255 | $create = $model->fireCreate(); |
||
256 | |||
257 | $this->registerBreadcrumb($model->getCreateTitle(), $this->parentBreadcrumb); |
||
258 | $this->registerBreadcrumbs($model); |
||
259 | |||
260 | return $this->render($model, $create, $model->getCreateTitle()); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @param ModelConfigurationInterface $model |
||
265 | * @param Request $request |
||
266 | * |
||
267 | * @return \Illuminate\Http\RedirectResponse |
||
268 | */ |
||
269 | public function postStore(ModelConfigurationInterface $model, Request $request) |
||
270 | { |
||
271 | if (! $model->isCreatable()) { |
||
272 | abort(404); |
||
273 | } |
||
274 | |||
275 | $createForm = $model->fireCreate(); |
||
276 | $nextAction = $request->input('next_action'); |
||
277 | |||
278 | $backUrl = $this->getBackUrl($request); |
||
279 | |||
280 | if ($createForm instanceof FormInterface) { |
||
281 | try { |
||
282 | $createForm->validateForm($request, $model); |
||
283 | |||
284 | if ($createForm->saveForm($request, $model) === false) { |
||
285 | return redirect()->back()->with([ |
||
286 | '_redirectBack' => $backUrl, |
||
287 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
288 | ]); |
||
289 | } |
||
290 | } catch (ValidationException $exception) { |
||
291 | return redirect()->back() |
||
292 | ->withErrors($exception->validator) |
||
293 | ->withInput() |
||
294 | ->with([ |
||
295 | '_redirectBack' => $backUrl, |
||
296 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
297 | ]); |
||
298 | } |
||
299 | } |
||
300 | |||
301 | if ($nextAction == 'save_and_continue') { |
||
302 | $newModel = $createForm->getModel(); |
||
303 | $primaryKey = $newModel->getKeyName(); |
||
304 | |||
305 | $redirectUrl = $model->getEditUrl($newModel->{$primaryKey}); |
||
306 | $redirectPolicy = $model->getRedirect(); |
||
307 | |||
308 | /* Make redirect when use in model config && Fix editable redirect */ |
||
309 | if ($redirectPolicy->get('create') == 'display' || ! $model->isEditable($newModel)) { |
||
310 | $redirectUrl = $model->getDisplayUrl(); |
||
311 | } |
||
312 | |||
313 | $response = redirect()->to( |
||
314 | $redirectUrl |
||
315 | )->with([ |
||
316 | '_redirectBack' => $backUrl, |
||
317 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
318 | ]); |
||
319 | } elseif ($nextAction == 'save_and_create') { |
||
320 | $response = redirect()->to($model->getCreateUrl($request->except([ |
||
321 | '_redirectBack', |
||
322 | '_token', |
||
323 | 'url', |
||
324 | 'next_action', |
||
325 | ])))->with([ |
||
326 | '_redirectBack' => $backUrl, |
||
327 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
328 | ]); |
||
329 | } else { |
||
330 | $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl())); |
||
331 | } |
||
332 | |||
333 | return $response->with('success_message', $model->getMessageOnCreate()); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param ModelConfigurationInterface $model |
||
338 | * @param $id |
||
339 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
340 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
341 | */ |
||
342 | public function getEdit(ModelConfigurationInterface $model, $id) |
||
343 | { |
||
344 | $item = $model->getRepository()->find($id); |
||
345 | |||
346 | if (is_null($item) || ! $model->isEditable($item)) { |
||
347 | abort(404); |
||
348 | } |
||
349 | |||
350 | $this->registerBreadcrumb($model->getEditTitle($item), $this->parentBreadcrumb); |
||
351 | |||
352 | $edit = $model->fireEdit($id); |
||
353 | |||
354 | $this->registerBreadcrumbs($model); |
||
355 | |||
356 | return $this->render($model, $edit, $model->getEditTitle($item)); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param ModelConfigurationInterface $model |
||
361 | * @param Request $request |
||
362 | * @param int $id |
||
363 | * |
||
364 | * @return \Illuminate\Http\RedirectResponse |
||
365 | * |
||
366 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
367 | */ |
||
368 | public function postUpdate(ModelConfigurationInterface $model, Request $request, $id) |
||
369 | { |
||
370 | /** @var FormInterface $editForm */ |
||
371 | $editForm = $model->fireEdit($id); |
||
372 | $item = $editForm->getModel(); |
||
373 | |||
374 | if (is_null($item) || ! $model->isEditable($item)) { |
||
375 | abort(404); |
||
376 | } |
||
377 | |||
378 | $nextAction = $request->input('next_action'); |
||
379 | |||
380 | $backUrl = $this->getBackUrl($request); |
||
381 | |||
382 | if ($editForm instanceof FormInterface) { |
||
383 | try { |
||
384 | $editForm->validateForm($request, $model); |
||
385 | |||
386 | if ($editForm->saveForm($request, $model) === false) { |
||
387 | return redirect()->back()->with([ |
||
388 | '_redirectBack' => $backUrl, |
||
389 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
390 | ]); |
||
391 | } |
||
392 | } catch (ValidationException $exception) { |
||
393 | return redirect()->back() |
||
394 | ->withErrors($exception->validator) |
||
395 | ->withInput() |
||
396 | ->with([ |
||
397 | '_redirectBack' => $backUrl, |
||
398 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
399 | ]); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | $redirectPolicy = $model->getRedirect(); |
||
404 | |||
405 | if ($nextAction == 'save_and_continue') { |
||
406 | $response = redirect()->back()->with([ |
||
407 | '_redirectBack' => $backUrl, |
||
408 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
409 | ]); |
||
410 | |||
411 | if ($redirectPolicy->get('edit') == 'display') { |
||
412 | $response = redirect()->to( |
||
413 | $model->getDisplayUrl() |
||
414 | )->with([ |
||
415 | '_redirectBack' => $backUrl, |
||
416 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
417 | ]); |
||
418 | } |
||
419 | } elseif ($nextAction == 'save_and_create') { |
||
420 | $response = redirect()->to($model->getCreateUrl($request->except([ |
||
421 | '_redirectBack', |
||
422 | '_token', |
||
423 | 'url', |
||
424 | 'next_action', |
||
425 | ])))->with([ |
||
426 | '_redirectBack' => $backUrl, |
||
427 | 'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
||
428 | ]); |
||
429 | } else { |
||
430 | $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl())); |
||
431 | } |
||
432 | |||
433 | return $response->with('success_message', $model->getMessageOnUpdate()); |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @param ModelConfigurationInterface $model |
||
438 | * @param Request $request |
||
439 | * |
||
440 | * @return bool |
||
441 | * |
||
442 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
443 | */ |
||
444 | public function inlineEdit(ModelConfigurationInterface $model, Request $request) |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * @param ModelConfigurationInterface $model |
||
518 | * @param Request $request |
||
519 | * @param int $id |
||
520 | * @return \Illuminate\Http\RedirectResponse |
||
521 | */ |
||
522 | public function deleteDelete(ModelConfigurationInterface $model, Request $request, $id) |
||
523 | { |
||
524 | $item = $model->getRepository()->find($id); |
||
525 | |||
526 | if (is_null($item) || ! $model->isDeletable($item)) { |
||
527 | abort(404); |
||
528 | } |
||
529 | |||
530 | $model->fireDelete($id); |
||
531 | |||
532 | if ($model->fireEvent('deleting', true, $item, $request) === false) { |
||
533 | return redirect()->back(); |
||
534 | } |
||
535 | |||
536 | $model->getRepository()->delete($id); |
||
537 | |||
538 | $model->fireEvent('deleted', false, $item, $request); |
||
539 | |||
540 | return redirect($request->input('_redirectBack', back()->getTargetUrl())) |
||
541 | ->with('success_message', $model->getMessageOnDelete()); |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * @param ModelConfigurationInterface $model |
||
546 | * @param Request $request |
||
547 | * @param int $id |
||
548 | * |
||
549 | * @return \Illuminate\Http\RedirectResponse |
||
550 | * |
||
551 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
552 | */ |
||
553 | public function deleteDestroy(ModelConfigurationInterface $model, Request $request, $id) |
||
554 | { |
||
555 | if (! $model->isRestorableModel()) { |
||
556 | abort(404); |
||
557 | } |
||
558 | |||
559 | $item = $model->getRepository()->findOnlyTrashed($id); |
||
560 | |||
561 | if (is_null($item) || ! $model->isRestorable($item)) { |
||
562 | abort(404); |
||
563 | } |
||
564 | |||
565 | $model->fireDestroy($id); |
||
566 | |||
567 | if ($model->fireEvent('destroying', true, $item, $request) === false) { |
||
568 | return redirect()->back(); |
||
569 | } |
||
570 | |||
571 | $model->getRepository()->forceDelete($id); |
||
572 | |||
573 | $model->fireEvent('destroyed', false, $item, $request); |
||
574 | |||
575 | return redirect($request->input('_redirectBack', back()->getTargetUrl())) |
||
576 | ->with('success_message', $model->getMessageOnDestroy()); |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * @param ModelConfigurationInterface|ModelConfiguration $model |
||
581 | * @param Request $request |
||
582 | * @param int $id |
||
583 | * |
||
584 | * @return \Illuminate\Http\RedirectResponse |
||
585 | * |
||
586 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
587 | */ |
||
588 | public function postRestore(ModelConfigurationInterface $model, Request $request, $id) |
||
589 | { |
||
590 | if (! $model->isRestorableModel()) { |
||
591 | abort(404); |
||
592 | } |
||
593 | |||
594 | $item = $model->getRepository()->findOnlyTrashed($id); |
||
595 | |||
596 | if (is_null($item) || ! $model->isRestorable($item)) { |
||
597 | abort(404); |
||
598 | } |
||
599 | |||
600 | $model->fireRestore($id); |
||
601 | |||
602 | if ($model->fireEvent('restoring', true, $item, $request) === false) { |
||
603 | return redirect()->back(); |
||
604 | } |
||
605 | |||
606 | $model->getRepository()->restore($id); |
||
607 | |||
608 | $model->fireEvent('restored', false, $item, $request); |
||
609 | |||
610 | return redirect($request->input('_redirectBack', back()->getTargetUrl())) |
||
611 | ->with('success_message', $model->getMessageOnRestore()); |
||
612 | } |
||
613 | |||
614 | /** |
||
615 | * @param ModelConfigurationInterface $model |
||
616 | * @param Renderable|string $content |
||
617 | * @param string|null $title |
||
618 | * |
||
619 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
620 | */ |
||
621 | public function render(ModelConfigurationInterface $model, $content, $title = null) |
||
622 | { |
||
623 | if ($content instanceof Renderable) { |
||
624 | $content = $content->render(); |
||
625 | } |
||
626 | |||
627 | if (is_null($title)) { |
||
628 | $title = $model->getTitle(); |
||
629 | } |
||
630 | |||
631 | return $this->admin->template()->view('_layout.inner') |
||
632 | ->with('title', $title) |
||
633 | ->with('content', $content) |
||
634 | ->with('breadcrumbKey', $this->parentBreadcrumb); |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * @param Renderable|string $content |
||
639 | * @param string|null $title |
||
640 | * |
||
641 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
642 | */ |
||
643 | public function renderContent($content, $title = null) |
||
644 | { |
||
645 | if ($content instanceof Renderable) { |
||
646 | $content = $content->render(); |
||
647 | } |
||
648 | |||
649 | return $this->admin->template()->view('_layout.inner') |
||
650 | ->with('title', $title) |
||
651 | ->with('content', $content) |
||
652 | ->with('breadcrumbKey', $this->parentBreadcrumb); |
||
653 | } |
||
654 | |||
655 | /** |
||
656 | * @param Request $request |
||
657 | * |
||
658 | * @return null|string |
||
659 | */ |
||
660 | protected function getBackUrl(Request $request) |
||
661 | { |
||
662 | if (($backUrl = $request->input('_redirectBack')) == \URL::previous()) { |
||
663 | $backUrl = null; |
||
664 | $request->merge(['_redirectBack' => $backUrl]); |
||
665 | } |
||
666 | |||
667 | return $backUrl; |
||
668 | } |
||
669 | |||
670 | public function getWildcard() |
||
671 | { |
||
672 | abort(404); |
||
673 | } |
||
674 | |||
675 | /** |
||
676 | * @param $title |
||
677 | * @param $parent |
||
678 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
679 | */ |
||
680 | protected function registerBreadcrumb($title, $parent) |
||
688 | } |
||
689 | |||
690 | /** |
||
691 | * @param ModelConfigurationInterface $model |
||
692 | * @throws \DaveJamesMiller\Breadcrumbs\Exception |
||
693 | */ |
||
694 | protected function registerBreadcrumbs(ModelConfigurationInterface $model) |
||
703 | }); |
||
704 | } |
||
705 | } |
||
706 | } |
||
707 | } |
||
708 |