Completed
Pull Request — master (#82)
by Brandon
02:15
created

SensorController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php 
2
3
namespace App\Http\Controllers;
4
5
use App\Sensor;
6
use App\DataTables\SensorDataTable;
7
use Illuminate\Http\Request;
8
use Validator;
9
10
class SensorController extends Controller 
11
{
12
    /**
13
    * Create a new controller instance.
14
    *
15
    * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
16
    */
17
    public function __construct()
18
    {
19
        $this->middleware('auth');
20
    }
21
22
    /**
23
    * Display a listing of the resource.
24
    *
25
    * @param  SensorDataTable   $dataTable
26
    * @return Response
27
    */
28
    public function index(SensorDataTable $dataTable)
29
    {
30
        return $dataTable->render('sensor.index');
31
    }
32
33
    /**
34
    * Show the form for creating a new resource.
35
    *
36
    * @return Response
37
    */
38
    public function create()
39
    {
40
        return view('sensor.create');
41
    }
42
43
    /**
44
    * Store a newly created resource in storage.
45
    *
46
    * @param Request $request
47
    * @return Response
48
    */
49
    public function store(Request $request)
50
    {
51
        request()->validate([
52
            'device_id' => 'required|integer|exists:devices,id',
53
            'name' => 'required|string|max:190',
54
            'type' => 'required|string|max:190'
55
        ]);
56
57
        $query = Sensor::create($request->all());
58
59
        return redirect()->route('sensor.show', $query->id)
60
            ->with('success', 'Sensor created successfully');
61
    }
62
63
    /**
64
    * Display the specified resource.
65
    *
66
    * @param  Request  $request
67
    * @param  int  $id
68
    * @return Response
69
    */
70
    public function show(Request $request, $id)
0 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...
71
    {
72
        $sensor = Sensor::findOrFail($id);
73
74
        return view('sensor.show', [ 'sensor' => $sensor ]);
75
    }
76
77
    /**
78
    * Show the form for editing the specified resource.
79
    *
80
    * @param  Request  $request
81
    * @param  int  $id
82
    * @return Response
83
    */
84
    public function edit(Request $request, $id)
0 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...
85
    {
86
        $sensor = Sensor::findOrFail($id);
87
        
88
        return view('sensor.edit', [ 'sensor' => $sensor ]);
89
    }
90
91
    /**
92
    * Update the specified resource in storage.
93
    *
94
    * @param  Request  $request
95
    * @param  int  $id
96
    * @return Response
97
    */
98
    public function update(Request $request, $id)
99
    {
100
        request()->validate([
101
            'device_id' => 'required|integer|exists:devices,id',
102
            'name' => 'required|string|max:190',
103
            'type' => 'required|string|max:190'
104
        ]);
105
        $query = Sensor::findOrFail($id)->update($request->all());
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
        return redirect()->route('sensor.show', $id)
0 ignored issues
show
Documentation introduced by
$id is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
            ->with('success', 'Sensor updated successfully');
108
    }
109
110
    /**
111
    * Remove the specified resource from storage.
112
    *
113
    * @param  int  $id
114
    * @return Response
115
    */
116
    public function destroy($id)
117
    {
118
        Sensor::findOrFail($id)->delete();
119
        return redirect()->route('sensor.index')
120
            ->with('success','Sensor deleted successfully');
121
    }
122
123
}
124