Passed
Branch dev5 (1d6d9a)
by Ron
09:07
created

CategoriesController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 71.05%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 76
ccs 27
cts 38
cp 0.7105
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 7 1
A __construct() 0 6 1
A update() 0 19 1
A store() 0 15 1
A destroy() 0 13 2
1
<?php
2
3
namespace App\Http\Controllers\Installer;
4
5
use App\SystemCategories;
6
use Illuminate\Http\Request;
7
use Illuminate\Validation\Rule;
8
use Illuminate\Support\Facades\Log;
9
use Illuminate\Support\Facades\Auth;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Support\Facades\Route;
12
13
class CategoriesController extends Controller
14
{
15 30
    public function __construct()
16
    {
17 30
        $this->middleware('auth');
18
        $this->middleware(function ($request, $next) {
19 24
            $this->authorize('hasAccess', 'Manage Equipment');
20 18
            return $next($request);
21 30
        });
22
23 30
    }
24
    //  View the list of categories
25 4
    public function index()
26
    {
27 4
        $categories = SystemCategories::all();
28
29 4
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
30 4
        return view('installer.categoryList', [
31 4
            'cats' => $categories
32
        ]);
33
    }
34
35
    //  Store the new category form
36 8
    public function store(Request $request)
37
    {
38 8
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray());
39
40 8
        $request->validate([
41 8
            'name' => 'required|string|unique:system_categories|regex:/^[a-zA-Z0-9_ ]*$/'
42
        ]);
43
44 4
        SystemCategories::create([
45 4
            'name' => $request->name
46
        ]);
47
48 4
        Log::info('New System Category - '.$request->name.' created by '.Auth::user()->full_name);
49
50 4
        return response()->json(['success' => true]);
51
    }
52
53
    //  Submit the Edit Category form
54
    public function update(Request $request, $id)
55
    {
56
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray());
57
58
        $request->validate([
59
            'name' => [
60
                    'required',
61
                    'string',
62
                    Rule::unique('system_categories')->ignore($id, 'cat_id'),
63
                    'regex:/^[a-zA-Z0-9_ ]*$/'
64
                ]
65
        ]);
66
67
        SystemCategories::find($id)->update([
68
            'name' => $request->name
69
        ]);
70
71
        Log::info('Category ID - '.$id.' updated by '.Auth::user()->full_name);
72
        return response()->json(['success' => true]);
73
    }
74
75
    //  Delete an existing category - note this will fail if the category has systems assigned to it
76 6
    public function destroy($id)
77
    {
78 6
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
79
80
        try {
81 6
            SystemCategories::find($id)->delete();
82 4
            Log::notice('Category ID '.$id.' deleted by '.Auth::user()->full_name);
83 4
            return response()->json(['success' => true, 'reason' => 'Category Successfully Deleted']);
84
        }
85 2
        catch (\Illuminate\Database\QueryException $e)
86
        {
87 2
            Log::warning('User '.Auth::user()->full_name.' tried to delete category ID '.$id.' but was unable to since it is still in use.');
88 2
            return response()->json(['success' => false, 'reason' => 'Category still in use.  You must delete all systems attached to this category first.']);
89
        }
90
    }
91
}
92