DeviceGroupController::destroy()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 20
rs 9.2
c 1
b 0
f 0
1
<?php
2
/**
3
 * DeviceGroupController.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Http\Controllers;
27
28
use App\DataTables\DeviceGroupDataTable;
29
use App\Http\Requests\AdminOnlyRequest;
30
use App\Http\Requests\DeviceGroupRequest;
31
use App\Models\DeviceGroup;
32
33
class DeviceGroupController extends Controller
34
{
35
    /**
36
     * Display a listing of the resource.
37
     *
38
     * @param DeviceGroupDataTable $dataTable
39
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e|\Illuminate\View\View?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
     */
41 1
    public function index(DeviceGroupDataTable $dataTable)
42
    {
43 1
        return $dataTable->render('devices.group-list');
44
    }
45
46
    /**
47
     * Show the form for creating a new resource.
48
     *
49
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
50
     */
51
    public function create()
52
    {
53
        return view('devices.group-edit');
54
    }
55
56
    /**
57
     * Store a newly created resource in storage.
58
     *
59
     * @param  \Illuminate\Http\Request $request
60
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
61
     */
62
    public function store(DeviceGroupRequest $request)
63
    {
64
        $group = DeviceGroup::create($request->all());
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Models\DeviceGroup. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
65
66
        return response()->json(['message' => trans('devices.groups.created', ['name' => $group->name])]);
67
    }
68
69
    /**
70
     * Display the specified resource.
71
     *
72
     * @param  int $id
73
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    public function show($id)
76
    {
77
        return redirect('devices/group='.$id);
78
    }
79
80
    /**
81
     * Show the form for editing the specified resource.
82
     *
83
     * @param  int $id
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function edit($id)
87
    {
88
        $group = DeviceGroup::findOrFail($id);
89
        return view('devices.group-edit')->withGroup($group);
90
    }
91
92
    /**
93
     * Update the specified resource in storage.
94
     *
95
     * @param  \Illuminate\Http\Request $request
96
     * @param  int $id
97
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
98
     */
99
    public function update(DeviceGroupRequest $request, $id)
100
    {
101
        $group = DeviceGroup::find($id);
102
        $group->update($request->all());
103
        $group->updateRelations();
104
105
        return response()->json(['message' => trans('devices.groups.updated', ['name' => $group->name])]);
106
    }
107
108
    /**
109
     * Remove the specified resource from storage.
110
     *
111
     * @param \Illuminate\Http\Request $request
112
     * @param  int $id
113
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
114
     * @throws \Exception
115
     */
116
    public function destroy(AdminOnlyRequest $request, $id)
117
    {
118
        $group = DeviceGroup::find($id);
119
        if ($group && $group->delete()) {
120
            return response()->json(['message' => trans('devices.groups.deleted', ['name' => $group->name])]);
121
        } else {
122
            return response()->json(['message' => trans('devices.groups.deletefailed', ['name' => $group ? $group->name : ""])], 422);
123
        }
124
    }
125
}
126