Completed
Pull Request — master (#228)
by
unknown
04:29
created

CrudController::postGenerator()   F

Complexity

Conditions 18
Paths > 20000

Size

Total Lines 104
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 53
nc 24578
nop 1
dl 0
loc 104
rs 0.7
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Artisan;
6
use File;
7
use Illuminate\Http\Request;
8
use Response;
9
use View;
10
use Redirect;
11
12
class CrudController extends Controller
13
{
14
    /**
15
     * Display generator.
16
     *
17
     * @return Response
18
     */
19
    public function getGenerator()
20
    {
21
        return view('crudmanagement.add-crud');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('crudmanagement.add-crud') returns the type Illuminate\View\View which is incompatible with the documented return type Response.
Loading history...
22
    }
23
24
    /**
25
     * Process generator.
26
     *
27
     * @param \Illuminate\Http\Request $request
28
     *
29
     * @return Response
30
     */
31
    public function postGenerator(Request $request)
32
    {
33
        $commandArg = [];
34
        $commandArg['name'] = $request->crud_name;
35
36
37
        /**
38
         * Make sure we do not already have this module
39
         */
40
        $directories = glob(base_path() . '/resources/views/*', GLOB_ONLYDIR);
41
        foreach ( $directories as $dir ){
42
            $this_name = str_replace(base_path() . '/resources/views/','',$dir);
43
            $used_names[] = $this_name;
44
        }
45
        //dd($request->crud_name);
46
47
        if (in_array(strtolower($request->crud_name), $used_names)){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $used_names seems to be defined by a foreach iteration on line 41. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
48
            return Redirect::back()->withErrors(['Name Exists or Restricted']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::back()-...Exists or Restricted')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Response.
Loading history...
49
        }
50
51
52
53
54
55
        if ($request->has('fields')) {
56
            $fieldsArray = [];
57
            $validationsArray = [];
58
            $x = 0;
59
            foreach ($request->fields as $field) {
60
                if ($request->fields_required[$x] == 1) {
61
                    $validationsArray[] = $field;
62
                }
63
64
                $fieldsArray[] = $field . '#' . $request->fields_type[$x];
65
66
                $x++;
67
            }
68
69
            $commandArg['--fields'] = implode(";", $fieldsArray);
70
        }
71
72
        if (!empty($validationsArray)) {
73
            $commandArg['--validations'] = implode("#required;", $validationsArray) . "#required";
74
        }
75
76
        if ($request->has('route')) {
77
            $commandArg['--route'] = $request->route;
78
        }
79
80
        if ($request->has('view_path')) {
81
            $commandArg['--view-path'] = $request->view_path;
82
        }
83
84
        if ($request->has('controller_namespace')) {
85
            $commandArg['--controller-namespace'] = $request->controller_namespace;
86
        }
87
88
        if ($request->has('model_namespace')) {
89
            $commandArg['--model-namespace'] = $request->model_namespace;
90
        }
91
92
        if ($request->has('route_group')) {
93
            $commandArg['--route-group'] = $request->route_group;
94
        }
95
96
        if ($request->has('relationships')) {
97
            $commandArg['--relationships'] = $request->relationships;
98
        }
99
100
        if ($request->has('form_helper')) {
101
            $commandArg['--form-helper'] = $request->form_helper;
102
        }
103
104
        if ($request->has('soft_deletes')) {
105
            $commandArg['--soft-deletes'] = $request->soft_deletes;
106
        }
107
108
        try {
109
            Artisan::call('crud:generate', $commandArg);
110
111
            $menus = json_decode(File::get(base_path('resources/laravel-admin/menus.json')));
112
113
            $name = $commandArg['name'];
114
            $routeName = ($commandArg['--route-group']) ? $commandArg['--route-group'] . '/' . snake_case($name, '-') : snake_case($name, '-');
115
116
            $menus->menus = array_map(function ($menu) use ($name, $routeName) {
117
                if ($menu->section == 'Resources') {
118
                    array_push($menu->items, (object) [
119
                        'title' => $name,
120
                        'url' => '/' . $routeName,
121
                    ]);
122
                }
123
124
                return $menu;
125
            }, $menus->menus);
126
127
            File::put(base_path('resources/laravel-admin/menus.json'), json_encode($menus));
128
129
            Artisan::call('migrate');
130
        } catch (\Exception $e) {
131
            return Response::make($e->getMessage(), 500);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Response::make($e->getMessage(), 500) returns the type Illuminate\Http\Response which is incompatible with the documented return type Response.
Loading history...
132
        }
133
134
        return redirect('crud')->with('flash_message', 'Your CRUD has been generated. See on the menu.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('crud')-...ted. See on the menu.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Response.
Loading history...
135
    }
136
}
137