Completed
Pull Request — master (#133)
by
unknown
05:13
created

SensorController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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 Charts;
0 ignored issues
show
Bug introduced by
The type Charts was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class SensorController extends Controller 
11
{
12
    /**
13
     * Create a new controller instance.
14
     *
15
     * @return void
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
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
27
     */
28
    public function index(SensorDataTable $dataTable)
29
    {
30
        return $dataTable->render('sensor.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $dataTable->render('sensor.index') also could return the type Illuminate\View\View|callable which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('sensor.create') returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
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|digits_between:1,10|exists:devices,id',
53
            'name' => 'required|min:2|max:190|name',
54
            'type' => 'required|max:190|type_name'
55
        ]);
56
57
        $query = Sensor::create($request->all());
58
59
        return redirect()->route('sensor.show', $query->id)
0 ignored issues
show
Bug introduced by
$query->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        return redirect()->route('sensor.show', /** @scrutinizer ignore-type */ $query->id)
Loading history...
Bug Best Practice introduced by
The expression return redirect()->route... created successfully') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
    public function show(/** @scrutinizer ignore-unused */ Request $request, $id)

This check looks for 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
        $latestData = $sensor->latestData;
0 ignored issues
show
Bug introduced by
The property latestData does not seem to exist on App\Sensor. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
74
        $sensordata = $sensor->data()->orderBy('id', 'desc')->paginate(25);
75
        $chartSensorData = $sensordata->reverse();
76
        $chart = Charts::create('line', 'highcharts')
77
            ->title($sensor->name)
78
            ->elementLabel($sensor->type)
79
            ->labels($chartSensorData->pluck('created_at'))
80
            ->values($chartSensorData->pluck('value'))
81
            ->responsive(true);
82
83
        return view('sensor.show', [ 'sensor' => $sensor, 'latestData' => $latestData, 'sensordata' => $sensordata, 'chart' => $chart ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('sensor.show...ta, 'chart' => $chart)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
84
    }
85
86
    /**
87
     * Show the form for editing the specified resource.
88
     *
89
     * @param  Request  $request
90
     * @param  int  $id
91
     * @return Response
92
     */
93
    public function edit(Request $request, $id)
94
    {
95
        $sensor = Sensor::findOrFail($id);
96
        
97
        return view('sensor.edit', [ 'sensor' => $sensor ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('sensor.edit...y('sensor' => $sensor)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
98
    }
99
100
    /**
101
     * Update the specified resource in storage.
102
     *
103
     * @param  Request  $request
104
     * @param  int  $id
105
     * @return Response
106
     */
107
    public function update(Request $request, $id)
108
    {
109
        request()->validate([
110
            'device_id' => 'required|integer|digits_between:1,10|exists:devices,id',
111
            'name' => 'required|min:2|max:190|name',
112
            'type' => 'required|max:190|type_name'
113
        ]);
114
        $query = Sensor::findOrFail($id)->update($request->all());
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
115
        return redirect()->route('sensor.show', $id)
0 ignored issues
show
Bug introduced by
$id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        return redirect()->route('sensor.show', /** @scrutinizer ignore-type */ $id)
Loading history...
Bug Best Practice introduced by
The expression return redirect()->route... updated successfully') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
116
            ->with('success', 'Sensor updated successfully');
117
    }
118
119
    /**
120
     * Remove the specified resource from storage.
121
     *
122
     * @param  int  $id
123
     * @return Response
124
     */
125
    public function destroy($id)
126
    {
127
        Sensor::findOrFail($id)->delete();
128
        return redirect()->route('sensor.index')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route... deleted successfully') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
129
            ->with('success', 'Sensor deleted successfully');
130
    }
131
132
}
133