Test Setup Failed
Push — vue-test ( ca5b50...c87041 )
by Tony
04:05
created

DeviceController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
/**
10
 * Device resource representation.
11 1
 *
12
 * @Resource("Device", uri="/api/devices")
13 1
 */
14
class DeviceController extends Controller
15
{
16
    /**
17
     * Get a list of all devices
18
     *
19
     * @Get("/")
20 1
     * @Versions({"v1"})
21
     * @Transaction({
22
     *      @Request("/"),
23 1
     *      @Response(200, body={"devices":{{"id":34,"os":"linux","icon":"https://hostname/images/os/linux.svg","status":1,"uptime":423452},{"id":38,"os":"ios","icon":"https://hostname/images/os/cisco.svg","status":0,"uptime":452}}})
24 1
     * })
25 1
     * @Parameters({
26
     *      @Parameter("fields", type="sting", required=false, description="Comma separated list of fields to return"),
27
     *      @Parameter("per_page", type="integer", required=false, description="Pagination per-page count")
28
     * })
29 1
     */
30
    public function index(Request $request)
31
    {
32
        if ($request->user()->hasGlobalRead()) {
33
            $devices = Device::query();
34
        } else {
35
            $devices = $request->user()->devices();
36
        }
37
38
        $fields = isset($request->fields) ? explode(',', $request->fields) : ['*'];
39
40
        // paginate if requested (is this needed/documented?)
41
        if (isset($request->per_page)) {
42
            return $devices->paginate($request->per_page, $fields);
43
        }
44
45
        return $devices->get($fields);
46
    }
47
48
    /**
49
     * Store a newly created resource in storage.
50
     *
51
     * @param  \Illuminate\Http\Request $request
52
     * @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...
53
     */
54
    public function store(Request $request)
55
    {
56
        //
57
    }
58
59
    /**
60
     * Fetch a device
61
     *
62
     * @Get("/{id}")
63
     * @Versions({"v1"})
64
     * @Transaction({
65
     *      @Request("/34"),
66
     *      @Response(200, body={"device":{"id":34,"os":"linux","icon":"https://hostname/images/os/linux.svg","status":1,"uptime":423452}})
67
     * })
68
     * @Parameters({
69
     *      @Parameter("id", type="integer", required=true, description="The id of the device to show")
70
     * })
71
     */
72
    public function show(Request $request, $id)
73
    {
74
        if ($request->user()->hasGlobalRead()) {
75
            $device = Device::find($id);
76
        } else {
77
            $user = User::find($request->user()->user_id);
78
            $device = $user->devices()->find($id);
79
        }
80
        // morph the data as required
81
        if ($request->query('displayFormat') == 'link') {
82
            return '<a href="'.url('/devices/').$device->deviceId.'">'.$device->hostname.'</a>';
83
        }
84
85
        return $device;
86
    }
87
88
    /**
89
     * Update the specified resource in storage.
90
     *
91
     * @param  \Illuminate\Http\Request $request
92
     * @param  int $id
93
     * @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...
94
     */
95
    public function update(Request $request, $id)
0 ignored issues
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...
96
    {
97
        //
98
    }
99
100
    /**
101
     * Remove the specified resource from storage.
102
     *
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 destroy($id)
0 ignored issues
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...
107
    {
108
        //
109
    }
110
}
111