Completed
Push — settings ( bea8d4...96499a )
by Tony
05:40
created

UserDeviceController::store()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 16
loc 16
rs 9.2
cc 4
eloc 9
nc 3
nop 2
1
<?php
2
/**
3
 * UserDeviceController.php
4
 *
5
 * Controls the user device relationship
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\Http\Requests\AdminOnlyRequest;
29
use App\Models\Device;
30
use App\Models\User;
31
32 View Code Duplication
class UserDeviceController extends Controller
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
{
34
    public function create(AdminOnlyRequest $request, $user_id)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        $user = User::with('devices')->find($user_id);
37
38
        return view('users.devices-create')->withUser($user);
39
    }
40
41
    /**
42
     * @param AdminOnlyRequest $request
43
     * @param $user_id
44
     * @return mixed
45
     */
46
    public function store(AdminOnlyRequest $request, $user_id)
47
    {
48
        $user = User::find($user_id);
49
50
        $device_ids = $request->input('devices');
51
52
        if (!is_array($device_ids) || count($device_ids) == 0) {
53
            return redirect()->back();
54
        }
55
56
        foreach ($device_ids as $device_id) {
57
            $device = Device::find($device_id);
58
            $user->devices()->attach($device);
59
        }
60
        return redirect()->back()->with(['type' => 'success', 'message' => trans('user.text.devicesadded')]);
61
    }
62
63
    /**
64
     * @param AdminOnlyRequest $request
65
     * @param $user_id
66
     * @param $device_id
67
     * @return mixed
68
     */
69
    public function destroy(AdminOnlyRequest $request, $user_id, $device_id)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        $user = User::find($user_id);
72
        $device = Device::find($device_id);
73
        $user->devices()->detach($device);
74
        return redirect()->back()->with(['type' => 'success', 'message' => trans('user.text.deviceremoved', ['hostname' => $device->hostname])]);
75
    }
76
}
77