Conditions | 18 |
Paths | > 20000 |
Total Lines | 104 |
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 |
||
38 | public function postGenerator(Request $request) |
||
39 | { |
||
40 | $commandArg = []; |
||
41 | $commandArg['name'] = $request->crud_name; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Make sure we do not already have this module |
||
46 | */ |
||
47 | $directories = glob(base_path() . '/resources/views/*', GLOB_ONLYDIR); |
||
48 | foreach ( $directories as $dir ){ |
||
49 | $this_name = str_replace(base_path() . '/resources/views/','',$dir); |
||
50 | $used_names[] = $this_name; |
||
51 | } |
||
52 | //dd($request->crud_name); |
||
53 | |||
54 | if (in_array(strtolower($request->crud_name), $used_names)){ |
||
55 | return Redirect::back()->withErrors(['Name Exists or Restricted']); |
||
56 | } |
||
57 | |||
58 | |||
59 | |||
60 | |||
61 | |||
62 | if ($request->has('fields')) { |
||
63 | $fieldsArray = []; |
||
64 | $validationsArray = []; |
||
65 | $x = 0; |
||
66 | foreach ($request->fields as $field) { |
||
67 | if ($request->fields_required[$x] == 1) { |
||
68 | $validationsArray[] = $field; |
||
69 | } |
||
70 | |||
71 | $fieldsArray[] = $field . '#' . $request->fields_type[$x]; |
||
72 | |||
73 | $x++; |
||
74 | } |
||
75 | |||
76 | $commandArg['--fields'] = implode(";", $fieldsArray); |
||
77 | } |
||
78 | |||
79 | if (!empty($validationsArray)) { |
||
80 | $commandArg['--validations'] = implode("#required;", $validationsArray) . "#required"; |
||
81 | } |
||
82 | |||
83 | if ($request->has('route')) { |
||
84 | $commandArg['--route'] = $request->route; |
||
85 | } |
||
86 | |||
87 | if ($request->has('view_path')) { |
||
88 | $commandArg['--view-path'] = $request->view_path; |
||
89 | } |
||
90 | |||
91 | if ($request->has('controller_namespace')) { |
||
92 | $commandArg['--controller-namespace'] = $request->controller_namespace; |
||
93 | } |
||
94 | |||
95 | if ($request->has('model_namespace')) { |
||
96 | $commandArg['--model-namespace'] = $request->model_namespace; |
||
97 | } |
||
98 | |||
99 | if ($request->has('route_group')) { |
||
100 | $commandArg['--route-group'] = $request->route_group; |
||
101 | } |
||
102 | |||
103 | if ($request->has('relationships')) { |
||
104 | $commandArg['--relationships'] = $request->relationships; |
||
105 | } |
||
106 | |||
107 | if ($request->has('form_helper')) { |
||
108 | $commandArg['--form-helper'] = $request->form_helper; |
||
109 | } |
||
110 | |||
111 | if ($request->has('soft_deletes')) { |
||
112 | $commandArg['--soft-deletes'] = $request->soft_deletes; |
||
113 | } |
||
114 | |||
115 | try { |
||
116 | Artisan::call('crud:generate', $commandArg); |
||
117 | |||
118 | $menus = json_decode(File::get(base_path('resources/laravel-admin/menus.json'))); |
||
119 | |||
120 | $name = $commandArg['name']; |
||
121 | $routeName = ($commandArg['--route-group']) ? $commandArg['--route-group'] . '/' . snake_case($name, '-') : snake_case($name, '-'); |
||
122 | |||
123 | $menus->menus = array_map(function ($menu) use ($name, $routeName) { |
||
124 | if ($menu->section == 'Resources') { |
||
125 | array_push($menu->items, (object) [ |
||
126 | 'title' => $name, |
||
127 | 'url' => '/' . $routeName, |
||
128 | ]); |
||
129 | } |
||
130 | |||
131 | return $menu; |
||
132 | }, $menus->menus); |
||
133 | |||
134 | File::put(base_path('resources/laravel-admin/menus.json'), json_encode($menus)); |
||
135 | |||
136 | Artisan::call('migrate'); |
||
137 | } catch (\Exception $e) { |
||
138 | return Response::make($e->getMessage(), 500); |
||
139 | } |
||
140 | |||
141 | return redirect('crud')->with('flash_message', 'Your CRUD has been generated. See on the menu.'); |
||
142 | } |
||
144 |