SensorController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 131
ccs 0
cts 45
cp 0
rs 10
c 1
b 1
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 5 1
A show() 0 16 1
A index() 0 5 1
A store() 0 14 1
A update() 0 12 1
A edit() 0 6 1
A destroy() 0 7 1
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
        $this->authorize('index', Sensor::class);
31
        
32
        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...
33
    }
34
35
    /**
36
     * Show the form for creating a new resource.
37
     *
38
     * @return Response
39
     */
40
    public function create()
41
    {
42
        $this->authorize('create', Sensor::class);
43
    
44
        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...
45
    }
46
47
    /**
48
     * Store a newly created resource in storage.
49
     *
50
     * @param Request $request
51
     * @return Response
52
     */
53
    public function store(Request $request)
54
    {
55
        $this->authorize('store', Sensor::class);
56
    
57
        request()->validate([
58
            'device_id' => 'required|integer|digits_between:1,10|exists:devices,id',
59
            'name' => 'required|min:2|max:190|name',
60
            'type' => 'required|max:190|type_name'
61
        ]);
62
63
        $sensor = Sensor::create($request->all());
64
65
        return redirect()->route('sensor.show', $sensor->id)
0 ignored issues
show
Bug introduced by
$sensor->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

65
        return redirect()->route('sensor.show', /** @scrutinizer ignore-type */ $sensor->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...
66
            ->with('success', 'Sensor created successfully');
67
    }
68
69
    /**
70
     * Display the specified resource.
71
     *
72
     * @param  String  $id
73
     * @return Response
74
     */
75
    public function show($id)
76
    {
77
        $this->authorize('show', Sensor::class);
78
    
79
        $sensor = Sensor::findOrFail($id);
80
        $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...
81
        $sensordata = $sensor->data()->orderBy('id', 'desc')->paginate(25);
82
        $chartSensorData = $sensordata->reverse();
83
        $chart = Charts::create('line', 'highcharts')
84
            ->title($sensor->name)
85
            ->elementLabel($sensor->type)
86
            ->labels($chartSensorData->pluck('created_at'))
87
            ->values($chartSensorData->pluck('value'))
88
            ->responsive(true);
89
90
        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...
91
    }
92
93
    /**
94
     * Show the form for editing the specified resource.
95
     *
96
     * @param  String  $id
97
     * @return Response
98
     */
99
    public function edit($id)
100
    {
101
        $this->authorize('edit', Sensor::class);
102
    
103
        $sensor = Sensor::findOrFail($id);
104
        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...
105
    }
106
107
    /**
108
     * Update the specified resource in storage.
109
     *
110
     * @param  Request  $request
111
     * @param  String  $id
112
     * @return Response
113
     */
114
    public function update(Request $request, $id)
115
    {
116
        $this->authorize('update', Sensor::class);
117
    
118
        request()->validate([
119
            'device_id' => 'required|integer|digits_between:1,10|exists:devices,id',
120
            'name' => 'required|min:2|max:190|name',
121
            'type' => 'required|max:190|type_name'
122
        ]);
123
        $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...
124
        return redirect()->route('sensor.show', $id)
0 ignored issues
show
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...
Bug introduced by
$id of type string 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

124
        return redirect()->route('sensor.show', /** @scrutinizer ignore-type */ $id)
Loading history...
125
            ->with('success', 'Sensor updated successfully');
126
    }
127
128
    /**
129
     * Remove the specified resource from storage.
130
     *
131
     * @param  String  $id
132
     * @return Response
133
     */
134
    public function destroy($id)
135
    {
136
        $this->authorize('destroy', Sensor::class);
137
    
138
        Sensor::findOrFail($id)->delete();
139
        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...
140
            ->with('success', 'Sensor deleted successfully');
141
    }
142
143
}
144