DeviceController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 23.08%

Importance

Changes 0
Metric Value
dl 0
loc 111
ccs 6
cts 26
cp 0.2308
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 10 2
A create() 0 5 1
A store() 0 18 1
A show() 0 12 1
A edit() 0 4 1
A update() 0 4 1
A destroy() 0 4 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\DataTables\DeviceDataTable;
6
use App\Models\Device;
7
use App\Models\DeviceGroup;
8
use Dingo\Api\Routing\Helpers;
9
use Illuminate\Http\Request;
10
11
class DeviceController extends Controller
12
{
13
    use Helpers;
14
15
    /**
16
     * Display a listing of the resource.
17
     *
18
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
19
     */
20 3
    public function index(DeviceDataTable $dataTable, $group_id = -1)
21
    {
22 3
        $group_name = "";
23 3
        if ($group_id >= 0) {
24 1
            $dataTable->addScope(new \App\DataTables\Scopes\DeviceGroup($group_id));
25 1
            $group_name = DeviceGroup::find($group_id)->name;
26
        }
27
28 3
        return $dataTable->render('devices.list', compact('group_name'));
29
    }
30
31
    /**
32
     * Show the form for creating a new resource.
33
     *
34
     * @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...
35
     */
36
    public function create()
37
    {
38
        // add new device form
39
        return view('devices.create');
40
    }
41
42
    /**
43
     * Store a newly created resource in storage.
44
     *
45
     * @param  \Illuminate\Http\Request  $request
46
     * @return \Illuminate\Http\RedirectResponse
47
     */
48
    public function store(Request $request)
49
    {
50
        // save device
51
52
        $this->validate($request, [
53
            'hostname'        => 'required|unique:devices|max:128',
54
            'snmpver'         => 'required|alpha_num|max:4',
55
            'transport'       => 'required|alpha_num|max:16',
56
            'port_assoc_mode' => 'required|alpha',
57
            'community'       => 'required_if:snmpver,v1,v2c|max:255',
58
            'authlevel'       => 'required_if:snmpver,v3|alpha|max:15',
59
            'authname'        => 'required_if:authlevel,authNoPriv|max:64',
60
            'authalgo'        => 'required_if:authlevel,authNoPriv|in:MD5,SHA|max:3',
61
            'cryptopass'      => 'required_if:authlevel,authPriv|max:64',
62
            'cryptoalgo'      => 'required_if:authlevel,authPriv|in:AES,DES|max:3',
63
        ]);
64
        return redirect()->route('devices.index');
65
    }
66
67
    /**
68
     * Display the specified resource.
69
     *
70
     * @param  \Illuminate\Http\Request $request
71
     * @param  int $id
72
     * @param string $page
73
     * @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...
74
     */
75
    public function show(Request $request, $id, $page = 'overview')
76
    {
77
        $device = Device::find($id);
78
        $device_url = url('devices/'.$device->device_id);
79
80
        $page_setup['navbar'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$page_setup was never initialized. Although not strictly required by PHP, it is generally a good practice to add $page_setup = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
81
            'Overview' => $device_url,
82
            'Graphs'   => $device_url . '/graphs',
83
            'Health'   => $device_url . '/health',
84
        ];
85
        return view('devices.show', compact(['device', 'page_setup', 'request', 'page']));
86
    }
87
88
    /**
89
     * Show the form for editing the specified resource.
90
     *
91
     * @param  int  $id
92
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
93
     */
94
    public function edit($id)
95
    {
96
        //edit device form??
97
    }
98
99
    /**
100
     * Update the specified resource in storage.
101
     *
102
     * @param  \Illuminate\Http\Request  $request
103
     * @param  int  $id
104
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
105
     */
106
    public function update(Request $request, $id)
107
    {
108
        //process device modify
109
    }
110
111
    /**
112
     * Remove the specified resource from storage.
113
     *
114
     * @param  int  $id
115
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
116
     */
117
    public function destroy($id)
118
    {
119
        // delete device
120
    }
121
}
122