Passed
Pull Request — master (#228)
by
unknown
03:36
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
 * Created by PhpStorm.
4
 * User: danielserrick
5
 * Date: 2019-01-04
6
 * Time: 21:04
7
 */
8
9
10
namespace App\Http\Controllers;
11
12
use Artisan;
13
use File;
14
use Illuminate\Http\Request;
15
use Response;
16
use View;
17
use Redirect;
18
19
class CrudController extends Controller
20
{
21
    /**
22
     * Display generator.
23
     *
24
     * @return Response
25
     */
26
    public function getGenerator()
27
    {
28
        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...
29
    }
30
31
    /**
32
     * Process generator.
33
     *
34
     * @param \Illuminate\Http\Request $request
35
     *
36
     * @return Response
37
     */
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)){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $used_names seems to be defined by a foreach iteration on line 48. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
55
            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...
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);
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...
139
        }
140
141
        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...
142
    }
143
}
144