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

SensorDataController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

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 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php 
2
3
namespace App\Http\Controllers;
4
5
use App\SensorData;
6
use App\DataTables\SensorDataDataTable;
7
use Illuminate\Http\Request;
8
9
class SensorDataController extends Controller 
10
{
11
    /**
12
    * Create a new controller instance.
13
    *
14
    * @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...
15
    */
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
    }
20
21
    /**
22
    * Display a listing of the resource.
23
    *
24
    * @param SensorDataDataTable $dataTable
25
    * @return Response
26
    */
27
    public function index(SensorDataDataTable $dataTable)
28
    {
29
        return $dataTable->render('sensordata.index');
30
    }
31
32
    /**
33
    * Show the form for creating a new resource.
34
    *
35
    * @return Response
36
    */
37
    public function create()
38
    {
39
        return view('sensordata.create');
40
    }
41
42
    /**
43
    * Store a newly created resource in storage.
44
    *
45
    * @return Response
46
    */
47 View Code Duplication
    public function store()
48
    {
49
        request()->validate([
50
            'sensor_id' => 'required|integer|exists:sensors,id',
51
            'value' => 'required|string|max:190'
52
        ]);
53
54
        $query = SensorData::create($request->all());
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
55
56
        return redirect()->route('sensordata.show', $query->id)
57
            ->with('success', 'SensorData created successfully');
58
    }
59
60
    /**
61
    * Display the specified resource.
62
    *
63
    * @param  Request  $request
64
    * @param  int  $id
65
    * @return Response
66
    */
67
    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...
68
    {
69
        $sensordata = SensorData::findOrFail($id);
70
71
        return view('sensordata.show', [ 'sensordata' => $sensordata ]);
72
    }
73
74
    /**
75
    * Show the form for editing the specified resource.
76
    *
77
    * @param  int  $id
78
    * @return Response
79
    */
80
    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...
81
    {
82
        $sensordata = SensorData::findOrFail($id);
83
        
84
        return view('sensordata.edit', [ 'sensordata' => $sensordata ]);
85
    }
86
87
    /**
88
    * Update the specified resource in storage.
89
    *
90
    * @param  int  $id
91
    * @return Response
92
    */
93 View Code Duplication
    public function update($id)
94
    {
95
        request()->validate([
96
            'sensor_id' => 'required|integer|exists:sensors,id',
97
            'value' => 'required|string|max:190'
98
        ]);
99
        $query = SensorData::findOrFail($id)->update($request->all());
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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...
100
        return redirect()->route('sensordata.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...
101
            ->with('success', 'SensorData updated successfully');    
102
    }
103
104
    /**
105
    * Remove the specified resource from storage.
106
    *
107
    * @param  int  $id
108
    * @return Response
109
    */
110
    public function destroy($id)
111
    {
112
        SensorData::find($id)->delete();
113
        return redirect()->route('sensordata.index')
114
            ->with('success','SensorData deleted successfully');
115
    }
116
117
}
118