Conditions | 18 |
Paths | > 20000 |
Total Lines | 96 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | public function postGenerator(Request $request) |
||
32 | { |
||
33 | $commandArg = []; |
||
34 | $commandArg['name'] = $request->crud_name; |
||
35 | |||
36 | // Make sure we do not already have this module |
||
37 | $directories = glob(base_path().'/resources/views/*', GLOB_ONLYDIR); |
||
38 | foreach ($directories as $dir){ |
||
39 | $this_name = str_replace(base_path().'/resources/views/', '', $dir); |
||
40 | $used_names[] = $this_name; |
||
41 | } |
||
42 | |||
43 | if (in_array(strtolower($request->crud_name), $used_names)) { |
||
44 | return Redirect::back()->withErrors(['Name Exists or Restricted']); |
||
45 | } |
||
46 | |||
47 | if ($request->has('fields')) { |
||
48 | $fieldsArray = []; |
||
49 | $validationsArray = []; |
||
50 | $x = 0; |
||
51 | foreach ($request->fields as $field) { |
||
52 | if ($request->fields_required[$x] == 1) { |
||
53 | $validationsArray[] = $field; |
||
54 | } |
||
55 | |||
56 | $fieldsArray[] = $field.'#'.$request->fields_type[$x]; |
||
57 | |||
58 | $x++; |
||
59 | } |
||
60 | |||
61 | $commandArg['--fields'] = implode(';', $fieldsArray); |
||
62 | } |
||
63 | |||
64 | if (!empty($validationsArray)) { |
||
65 | $commandArg['--validations'] = implode('#required;', $validationsArray).'#required'; |
||
66 | } |
||
67 | |||
68 | if ($request->has('route')) { |
||
69 | $commandArg['--route'] = $request->route; |
||
70 | } |
||
71 | |||
72 | if ($request->has('view_path')) { |
||
73 | $commandArg['--view-path'] = $request->view_path; |
||
74 | } |
||
75 | |||
76 | if ($request->has('controller_namespace')) { |
||
77 | $commandArg['--controller-namespace'] = $request->controller_namespace; |
||
78 | } |
||
79 | |||
80 | if ($request->has('model_namespace')) { |
||
81 | $commandArg['--model-namespace'] = $request->model_namespace; |
||
82 | } |
||
83 | |||
84 | if ($request->has('route_group')) { |
||
85 | $commandArg['--route-group'] = $request->route_group; |
||
86 | } |
||
87 | |||
88 | if ($request->has('relationships')) { |
||
89 | $commandArg['--relationships'] = $request->relationships; |
||
90 | } |
||
91 | |||
92 | if ($request->has('form_helper')) { |
||
93 | $commandArg['--form-helper'] = $request->form_helper; |
||
94 | } |
||
95 | |||
96 | if ($request->has('soft_deletes')) { |
||
97 | $commandArg['--soft-deletes'] = $request->soft_deletes; |
||
98 | } |
||
99 | |||
100 | try { |
||
101 | Artisan::call('crud:generate', $commandArg); |
||
102 | |||
103 | $menus = json_decode(File::get(base_path('resources/laravel-admin/menus.json'))); |
||
104 | |||
105 | $name = $commandArg['name']; |
||
106 | $routeName = ($commandArg['--route-group']) ? $commandArg['--route-group'].'/'.snake_case($name, '-') : snake_case($name, '-'); |
||
107 | |||
108 | $menus->menus = array_map(function ($menu) use ($name, $routeName) { |
||
109 | if ($menu->section == 'Resources') { |
||
110 | array_push($menu->items, (object) [ |
||
111 | 'title' => $name, |
||
112 | 'url' => '/'.$routeName, |
||
113 | ]); |
||
114 | } |
||
115 | |||
116 | return $menu; |
||
117 | }, $menus->menus); |
||
118 | |||
119 | File::put(base_path('resources/laravel-admin/menus.json'), json_encode($menus)); |
||
120 | |||
121 | Artisan::call('migrate'); |
||
122 | } catch (\Exception $e) { |
||
123 | return Response::make($e->getMessage(), 500); |
||
124 | } |
||
125 | |||
126 | return redirect('crud')->with('flash_message', 'Your CRUD has been generated. See on the menu.'); |
||
127 | } |
||
129 |