Completed
Push — user-management ( d87e21 )
by Tony
03:32
created

UserDeviceController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 6
dl 43
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 5 5 1
A store() 15 15 3
A destroy() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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;
29
use App\Http\Requests\AdminOnlyRequest;
30
use App\Models\Device;
31
use App\Models\User;
32
33 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...
34
{
35
    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...
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
        if(count($device_ids) == 0) {
52
            return redirect()->back();
53
        }
54
        
55
        foreach($device_ids as $device_id) {
0 ignored issues
show
Bug introduced by
The expression $device_ids of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
56
            $device = Device::find($device_id);
57
            $user->devices()->attach($device);
58
        }
59
        return redirect()->back()->with(['type' => 'success', 'message' => "Devices added."]);
60
    }
61
62
    /**
63
     * @param AdminOnlyRequest $request
64
     * @param $user_id
65
     * @param $device_id
66
     * @return mixed
67
     */
68
    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...
69
    {
70
        $user = User::find($user_id);
71
        $device = Device::find($device_id);
72
        $user->devices()->detach($device);
73
        return redirect()->back()->with(['type' => 'success', 'message' => "Device ".$device->hostname." removed."]);
74
    }
75
}
76