Passed
Push — master ( 2ee843...cfc51d )
by Tony
09:10
created

DeviceGroupController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\DeviceGroup;
6
use Illuminate\Validation\Rule;
7
use Illuminate\Http\Request;
8
use LibreNMS\Alerting\QueryBuilderFilter;
9
use LibreNMS\Alerting\QueryBuilderFluentParser;
10
use Toastr;
11
12
class DeviceGroupController extends Controller
13
{
14
    public function __construct()
15
    {
16
        $this->authorizeResource(DeviceGroup::class, 'device_group');
17
    }
18
19
    /**
20
     * Display a listing of the resource.
21
     *
22
     * @return \Illuminate\Http\Response
23
     */
24
    public function index()
25
    {
26
        $this->authorize('manage', DeviceGroup::class);
27
28
        return view('device-group.index', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('device-grou...unt('devices')->get())) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
29
            'device_groups' => DeviceGroup::orderBy('name')->withCount('devices')->get(),
30
        ]);
31
    }
32
33
    /**
34
     * Show the form for creating a new resource.
35
     *
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function create()
39
    {
40
        return view('device-group.create', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('device-grou...ilderFilter('group')))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
41
            'device_group' => new DeviceGroup(),
42
            'filters' => json_encode(new QueryBuilderFilter('group')),
43
        ]);
44
    }
45
46
    /**
47
     * Store a newly created resource in storage.
48
     *
49
     * @param \Illuminate\Http\Request $request
50
     * @return \Illuminate\Http\Response
51
     */
52
    public function store(Request $request)
53
    {
54
        $this->validate($request, [
55
            'name' => 'required|string|unique:device_groups',
56
            'type' => 'required|in:dynamic,static',
57
            'devices' => 'array|required_if:type,static',
58
            'devices.*' => 'integer',
59
            'rules' => 'json|required_if:type,dynamic',
60
        ]);
61
62
        $deviceGroup = DeviceGroup::make($request->only(['name', 'desc', 'type']));
63
        $deviceGroup->rules = json_decode($request->rules);
64
        $deviceGroup->save();
65
66
        if ($request->type == 'static') {
67
            $deviceGroup->devices()->sync($request->devices);
68
        }
69
70
        Toastr::success(__('Device Group :name created', ['name' => $deviceGroup->name]));
71
72
        return redirect()->route('device-groups.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('device-groups.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
73
    }
74
75
    /**
76
     * Display the specified resource.
77
     *
78
     * @param \App\Models\DeviceGroup $deviceGroup
79
     * @return \Illuminate\Http\Response
80
     */
81
    public function show(DeviceGroup $deviceGroup)
82
    {
83
        return redirect(url('/devices/group=' . $deviceGroup->id));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(url('/de...=' . $deviceGroup->id)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
84
    }
85
86
    /**
87
     * Show the form for editing the specified resource.
88
     *
89
     * @param \App\Models\DeviceGroup $deviceGroup
90
     * @return \Illuminate\Http\Response
91
     */
92
    public function edit(DeviceGroup $deviceGroup)
93
    {
94
        // convert old rules on edit
95
        if (is_null($deviceGroup->rules)) {
0 ignored issues
show
introduced by
The condition is_null($deviceGroup->rules) is always false.
Loading history...
96
            $query_builder = QueryBuilderFluentParser::fromOld($deviceGroup->pattern);
97
            $deviceGroup->rules = $query_builder->toArray();
98
        }
99
100
        return view('device-group.edit', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('device-grou...ilderFilter('group')))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
101
            'device_group' => $deviceGroup,
102
            'filters' => json_encode(new QueryBuilderFilter('group')),
103
        ]);
104
    }
105
106
    /**
107
     * Update the specified resource in storage.
108
     *
109
     * @param \Illuminate\Http\Request $request
110
     * @param \App\Models\DeviceGroup $deviceGroup
111
     * @return \Illuminate\Http\Response
112
     */
113
    public function update(Request $request, DeviceGroup $deviceGroup)
114
    {
115
        $this->validate($request, [
116
            'name' => [
117
                'required',
118
                'string',
119
                Rule::unique('device_groups')->where(function ($query) use ($deviceGroup) {
120
                    $query->where('id', '!=', $deviceGroup->id);
121
                }),
122
            ],
123
            'type' => 'required|in:dynamic,static',
124
            'devices' => 'array|required_if:type,static',
125
            'devices.*' => 'integer',
126
            'rules' => 'json|required_if:type,dynamic',
127
        ]);
128
129
        $deviceGroup->fill($request->only(['name', 'desc', 'type']));
130
131
        $devices_updated = false;
132
        if ($deviceGroup->type == 'static') {
133
            // sync device_ids from input
134
            $devices_updated = array_sum($deviceGroup->devices()->sync($request->get('devices', [])));
135
        } else {
136
            $deviceGroup->rules = json_decode($request->rules);
137
        }
138
139
        if ($deviceGroup->isDirty() || $devices_updated) {
140
            try {
141
                if ($deviceGroup->save() || $devices_updated) {
142
                    Toastr::success(__('Device Group :name updated', ['name' => $deviceGroup->name]));
143
                } else {
144
                    Toastr::error(__('Failed to save'));
145
                    return redirect()->back()->withInput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back()->withInput() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
146
                }
147
            } catch (\Illuminate\Database\QueryException $e) {
148
                return redirect()->back()->withInput()->withErrors([
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...') . $e->getMessage())) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
149
                    'rules' => __('Rules resulted in invalid query: ') . $e->getMessage()
150
                ]);
151
            }
152
        } else {
153
            Toastr::info(__('No changes made'));
154
        }
155
156
        return redirect()->route('device-groups.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('device-groups.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
157
    }
158
159
    /**
160
     * Remove the specified resource from storage.
161
     *
162
     * @param \App\Models\DeviceGroup $deviceGroup
163
     * @return \Illuminate\Http\Response
164
     */
165
    public function destroy(DeviceGroup $deviceGroup)
166
    {
167
        $deviceGroup->delete();
168
169
        Toastr::success(__('Device Group :name deleted', ['name' => $deviceGroup->name]));
170
171
        return redirect()->route('device-groups.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('device-groups.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
172
    }
173
}
174