Test Setup Failed
Push — dev6 ( 81193f...01d104 )
by Ron
22:34
created

EquipmentCategoriesController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Equip;
4
5
use Inertia\Inertia;
6
7
use App\Models\EquipmentType;
8
use App\Models\EquipmentCategory;
9
use App\Http\Controllers\Controller;
10
use App\Http\Requests\Equipment\EquipmentCategoryRequest;
11
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Auth;
14
15
class EquipmentCategoriesController extends Controller
16
{
17
    /**
18
     *  Show a list of categories available to edit
19
     */
20
    public function index()
21
    {
22
        $this->authorize('create', EquipmentCategory::class);
23
        return Inertia::render('Equipment/listCategories', [
24
            'categories' => EquipmentCategory::all(),
25
        ]);
26
    }
27
28
    /**
29
     *  Form to create a new Equipment Category
30
     */
31
    public function create()
32
    {
33
        $this->authorize('create', EquipmentCategory::class);
34
        return Inertia::render('Equipment/category');
35
    }
36
37
    /**
38
     *  Store the new category
39
     */
40
    public function store(EquipmentCategoryRequest $request)
41
    {
42
        EquipmentCategory::create($request->only('name'));
43
        Log::info('New Equipment Category '.$request->name.' created by '.Auth::user()->full_name);
1 ignored issue
show
Bug introduced by
Accessing full_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
44
        return redirect(route('admin.index'))->with(['message' => 'New Category Created', 'type' => 'success']);
45
    }
46
47
    /**
48
     *  Form to edit a category
49
     */
50
    public function edit($id)
51
    {
52
        $category = EquipmentCategory::findOrFail($id);
53
        $this->authorize('update', $category);
54
55
        return Inertia::render('Equipment/category', [
56
            'cat' => $category,
57
        ]);
58
    }
59
60
    /**
61
     *  Update the selected category
62
     */
63
    public function update(EquipmentCategoryRequest $request, $id)
64
    {
65
        EquipmentCategory::findOrFail($id)->update($request->only('name'));
66
        Log::info('Equipment Category ID '.$id.' name - '.$request->name.' was updated by '.Auth::user()->full_name);
1 ignored issue
show
Bug introduced by
Accessing full_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
67
        return redirect(route('admin.index'))->with(['message' => 'Category Updated', 'type' => 'success']);
68
    }
69
70
    /**
71
     *  Delete a category
72
     */
73
    public function destroy($id)
74
    {
75
        $equip = EquipmentCategory::findOrFail($id);
76
        $this->authorize('delete', $equip);
77
78
        //  Cannot delete a category if it is in use
79
        $inUse = EquipmentType::where('cat_id', $id)->count();
80
        if($inUse)
81
        {
82
            Log::notice('User '.Auth::user()->full_name.' is trying to delete Equipment Category '.$equip->name.' but it is still in use');
1 ignored issue
show
Bug introduced by
Accessing full_name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
83
            return back()->with(['message' => 'This category has Equipment assigned to it.  Please delete this equipment before continuing', 'type' => 'danger']);
84
        }
85
86
        Log::notice('Equipment Category ID '.$id.' name - '.$equip->name.' has been deleted by '.Auth::user()->full_name);
87
        $equip->delete();
88
        return redirect(route('admin.index'))->with(['message' => 'Category Deleted', 'type' => 'success']);
89
    }
90
}
91