Completed
Pull Request — develop (#143)
by Tony
03:52
created

DeviceGroupController::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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;
30
use App\Http\Requests\AdminOnlyRequest;
31
use App\Http\Requests\DeviceGroupRequest;
32
use App\Models\DeviceGroup;
33
34
class DeviceGroupController extends Controller
35
{
36
    /**
37
     * Display a listing of the resource.
38
     *
39
     * @param DeviceGroupDataTable $dataTable
40
     * @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...
41
     */
42
    public function index(DeviceGroupDataTable $dataTable)
43
    {
44
        return $dataTable->render('devices.group-list');
45
    }
46
47
    /**
48
     * Show the form for creating a new resource.
49
     *
50
     * @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...
51
     */
52
    public function create()
53
    {
54
        return view('devices.group-edit');
55
    }
56
57
    /**
58
     * Store a newly created resource in storage.
59
     *
60
     * @param  \Illuminate\Http\Request $request
61
     * @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...
62
     */
63
    public function store(DeviceGroupRequest $request)
64
    {
65
        $group = DeviceGroup::create($request->all());
66
67
        return response()->json(['message' => trans('devices.groups.created', ['name' => $group->name])]);
68
    }
69
70
    /**
71
     * Display the specified resource.
72
     *
73
     * @param  int $id
74
     * @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...
75
     */
76
    public function show($id)
77
    {
78
        return redirect('devices/group='.$id);
79
    }
80
81
    /**
82
     * Show the form for editing the specified resource.
83
     *
84
     * @param  int $id
85
     * @return \Illuminate\Http\Response
86
     */
87
    public function edit($id)
88
    {
89
        $group = DeviceGroup::findOrFail($id);
90
        return view('devices.group-edit')->withGroup($group);
91
    }
92
93
    /**
94
     * Update the specified resource in storage.
95
     *
96
     * @param  \Illuminate\Http\Request $request
97
     * @param  int $id
98
     * @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...
99
     */
100
    public function update(DeviceGroupRequest $request, $id)
101
    {
102
        $group = DeviceGroup::find($id);
103
        $group->update($request->all());
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
}
127