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