Passed
Pull Request — master (#77)
by Ron
41:51 queued 13:39
created

SystemsController::update()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 62
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 6.001

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 33
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 62
ccs 31
cts 32
cp 0.9688
crap 6.001
rs 8.7697

How to fix   Long Method   

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\Installer;
4
5
use App\SystemTypes;
6
use App\SystemDataFields;
7
use App\SystemCategories;
8
use Illuminate\Http\Request;
9
use App\SystemDataFieldTypes;
10
use Illuminate\Validation\Rule;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Facades\Auth;
13
use App\Http\Controllers\Controller;
14
use Illuminate\Support\Facades\Route;
15
16
class SystemsController extends Controller
17
{
18 64
    public function __construct()
19
    {
20 64
        $this->middleware('auth');
21
        $this->middleware(function ($request, $next) {
22 52
            $this->authorize('hasAccess', 'Manage Equipment');
23 40
            return $next($request);
24 64
        });
25 64
    }
26
27
    //  List the systems that can be modified
28 4
    public function index()
29
    {
30 4
        $systems = SystemCategories::with('SystemTypes')->get();
31
32 4
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
33 4
        Log::debug('Fetched Data - ', $systems->toArray());
34 4
        return view('installer.systemsList', [
35 4
            'systems' => $systems
36
        ]);
37
    }
38
39
    //  Open the form to create a new system
40
    // public function create()
41
    // {
42
    //     $categories = SystemCategories::all();
43
    //     $dataTypes  = SystemCustDataTypes::orderBy('name', 'ASC')->get();
44
45
    //     $dropDown = [];
46
    //     foreach($dataTypes as $type)
47
    //     {
48
    //         $dropDown[] = [
49
    //             'value' => $type->data_type_id,
50
    //             'label' => $type->name
51
    //         ];
52
    //     }
53
54
    //     Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
55
    //     return view('installer.newSystem', [
56
    //         'categories' => $categories,
57
    //         'dropDown'   => $dropDown
58
    //     ]);
59
    // }
60
61
    //  Store the new system type
62 12
    public function store(Request $request)
63
    {
64 12
        $request->validate([
65 12
            'category' => 'required|numeric',
66
            'name' => [
67 12
                    'required',
68 12
                    'string',
69 12
                    Rule::unique('system_types'),
70 12
                    'regex:/^[a-zA-Z0-9_ ]*$/'
71
                ],
72 12
            'dataOptions' => 'required'
73
74
        ]);
75
76 6
        $sysData = SystemTypes::create([
77 6
            'cat_id'          => $request->category,
78 6
            'name'            => $request->name,
79 6
            'folder_location' => str_replace(' ', '_', $request->name)
80
        ]);
81 6
        $sysID = $sysData->sys_id;
82 6
        $i = 0;
83
84 6
        foreach($request->dataOptions as $field)
85
        {
86 6
            if(!empty($field))
87
            {
88 6
                if(isset($field['data_type_id']))
89
                {
90 6
                    $id = $field['data_type_id'];
91
                }
92
                else
93
                {
94 2
                    $newField = SystemDataFieldTypes::create([
95 2
                        'name' => $field['name']
96
                    ]);
97 2
                    $id = $newField->data_type_id;
98
                }
99
100 6
                SystemDataFields::create([
101 6
                    'sys_id' => $sysID,
102 6
                    'data_type_id' => $id,
103 6
                    'order' => $i
104
                ]);
105 6
                $i++;
106
            }
107
        }
108
109 6
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
110 6
        Log::debug('Submitted Data - ', $request->toArray());
111 6
        Log::notice('New System Created', ['cat_id' => $request->catName, 'sys_name' => $request->name, 'user_id' => Auth::user()->user_id]);
112 6
        $request->session()->flash('success', 'New System Created');
113
114 6
        return response()->json(['success' => true]);
115
    }
116
117
    //  Open the form to add equipment for the sepcified category
118 4
    public function show($cat)
119
    {
120 4
        $fields = SystemDataFieldTypes::all();
121 4
        $cat = SystemCategories::where('name', str_replace('-', ' ', $cat))->first();
122
123 4
        Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
124 4
        return view('installer.newSystem', [
125 4
            'cat'      => $cat,
126 4
            'dataList' => $fields,
127
        ]);
128
    }
129
130
    //  Edit an existing system
131 4
    public function edit($id)
132
    {
133 4
        $fields = SystemDataFieldTypes::all();
134
        $system = SystemTypes::where('sys_id', $id)->with(['SystemDataFields' => function($query)
135
        {
136 4
            $query->join('system_data_field_types', 'system_data_fields.data_type_id', '=', 'system_data_field_types.data_type_id');
137 4
        }])->withCount('SystemDataFields')->first();
138
139 4
        Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id);
140 4
        return view('installer.editSystem', [
141 4
            'system'   => $system,
142 4
            'dataList' => $fields,
143
        ]);
144
    }
145
146
    //  Update the system data
147 10
    public function update(Request $request, $id)
148
    {
149 10
        $request->validate([
150
            'name'     => [
151 10
                    'required',
152 10
                    'string',
153 10
                    Rule::unique('system_types')->ignore($id, 'sys_id'),
154 10
                    'regex:/^[-a-zA-Z0-9_ ]*$/'
155
            ],
156 10
            'dataOptions' => 'required',
157
        ]);
158
159
        //  Update the system name
160 6
        SystemTypes::find($id)->update([
161 6
            'name' => $request->name
162
        ]);
163
164
        //  Update the order of the existing data fields
165 6
        $i = 0;
166 6
        foreach($request->dataOptions as $data)
167
        {
168 6
            SystemDataFields::where('sys_id', $id)->where('data_type_id', $data['data_type_id'])->update([
169 6
                'order' => $i
170
            ]);
171
172 6
            $i++;
173
        }
174
        //  Process any new data fields
175 6
        if(!empty($request->newOptions))
176
        {
177 2
            foreach($request->newOptions as $field)
178
            {
179 2
                if(!empty($field))
180
                {
181 2
                    if(isset($field['data_type_id']))
182
                    {
183
                        $dataID = $field['data_type_id'];
184
                    }
185
                    else
186
                    {
187 2
                        $newField = SystemDataFieldTypes::create([
188 2
                            'name' => $field['name']
189
                        ]);
190 2
                        $dataID = $newField->data_type_id;
191
                    }
192
193 2
                    SystemDataFields::create([
194 2
                        'sys_id' => $id,
195 2
                        'data_type_id' => $dataID,
196 2
                        'order' => $i
197
                    ]);
198 2
                    $i++;
199
                }
200
            }
201
        }
202
203 6
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
204 6
        Log::debug('Submitted Data - ', $request->toArray());
205 6
        Log::notice('System Updated', ['sys_name' => $request->name, 'user_id' => Auth::user()->user_id]);
206 6
        $request->session()->flash('success', 'System Updated');
207
208 6
        return response()->json(['success' => true]);
209
    }
210
211
    //  Delete an existing system - note this will fail if the system has any customers or tech tips assigned to it
212 6
   public function destroy($id)
213
   {
214
       //
215
       try {
216 6
            SystemTypes::find($id)->delete();
217 4
            return response()->json(['success' => true, 'reason' => 'Equipment Successfully Deleted']);
218
        }
219 2
        catch (\Illuminate\Database\QueryException $e)
220
        {
221 2
            return response()->json(['success' => false, 'reason' => 'Cannot delete this equipment.  It has Customers or Tech Tips assigned to it.  Please delete those first.']);
222
        }
223
   }
224
}
225