Completed
Push — develop ( 7ad1ba...7fbe1f )
by Neil
12:07
created

DeviceController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use App\Models\Device;
6
use App\Models\User;
7
use Illuminate\Http\Request;
8
9
class DeviceController extends Controller
10
{
11 2
    public function __construct() {
12
13 2
    }
14
15
    /**
16
     * Display a listing of all authorized devices
17
     *
18
     * @return \Illuminate\Http\Response
19
     */
20 2
    public function index(Request $request)
21
    {
22
        // fetch devices from the database
23 2
        if ($request->user()->hasGlobalRead()) {
24 1
            $devices = Device::all();
25 1
        }
26
        else {
27 2
            $devices = User::find($request->user()->user_id)->devices()->get();
28
        }
29
        // morph the data as required
30 2
        if ($request->query('displayFormat') == 'human') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
31
        }
32
33 2
        return $devices;
34
    }
35
36
    /**
37
     * Show the form for creating a new resource.
38
     *
39
     * @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...
40
     */
41
    public function create()
42
    {
43
        //
44
    }
45
46
    /**
47
     * Store a newly created resource in storage.
48
     *
49
     * @param  \Illuminate\Http\Request  $request
50
     * @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...
51
     */
52
    public function store(Request $request)
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...
53
    {
54
        //
55
    }
56
57
    /**
58
     * Display the specified resource.
59
     *
60
     * @param  int  $id
61
     * @return \Illuminate\Http\Response
62
     */
63 2
    public function show(Request $request, $id)
64
    {
65
        if ($request->user()->hasGlobalRead()) {
66
            $device = Device::find($id);
67
        }
68
        else {
69
            $user = User::find($request->user()->user_id);
70
            $device = $user->devices()->find($id);
71
        }
72
        // morph the data as required
73
        if ($request->query('displayFormat') == 'link') {
74
            return '<a href="'.url('/devices/').$device->deviceId.'">'.$device->hostname.'</a>';
75
        }
76
77 2
        return $device;
78
    }
79
80
    /**
81
     * Show the form for editing the specified resource.
82
     *
83
     * @param  int  $id
84
     * @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...
85
     */
86
    public function edit($id)
1 ignored issue
show
Unused Code introduced by
The parameter $id 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...
87
    {
88
        //
89
    }
90
91
    /**
92
     * Update the specified resource in storage.
93
     *
94
     * @param  \Illuminate\Http\Request  $request
95
     * @param  int  $id
96
     * @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...
97
     */
98
    public function update(Request $request, $id)
2 ignored issues
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...
Unused Code introduced by
The parameter $id 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...
99
    {
100
        //
101
    }
102
103
    /**
104
     * Remove the specified resource from storage.
105
     *
106
     * @param  int  $id
107
     * @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...
108
     */
109
    public function destroy($id)
1 ignored issue
show
Unused Code introduced by
The parameter $id 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...
110
    {
111
        //
112
    }
113
114
}
115