Completed
Pull Request — master (#109)
by
unknown
02:13
created

ApiController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 216
ccs 0
cts 76
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A show() 0 4 1
B update() 0 45 2
B sensor() 0 46 2
B register() 0 46 3
B image() 0 28 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Http\Request;
7
use App\Device;
8
use App\Deviceimage;
9
use App\User;
10
use App\Sensor;
11
use App\SensorData;
12
use Illuminate\Support\Facades\Cache;
13
use Illuminate\Support\Facades\Storage;
14
use Carbon\Carbon;
15
16
class ApiController extends Controller
17
{
18
    /**
19
     * Creates a json response for all the devices.
20
     *
21
     * @return Response
22
     */    
23
    public function index()
24
    {
25
        return response()->json(['data' => 'SmartSettia API - Bad request type.'], 400);
26
    }
27
28
    /**
29
     * Creates a json response for a specifc device.
30
     *
31
     * @param  Device  $device
32
     * @return Response
33
     */    
34
    public function show(Device $device)
35
    {
36
        return response()->json($device, 200);
37
    }
38
39
    /**
40
     * Updates the status of a device.
41
     *
42
     * @param  Request  $request
43
     * @return Response
44
     */
45
    public function update(Request $request)
46
    {
47
        // Validate the request
48
        $validator = Validator::make($request->all(), [
0 ignored issues
show
Unused Code introduced by
$validator 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...
49
            'uuid'          => 'required|string|max:255|exists:devices,uuid',
50
            'token'         => 'required|string|max:60|exists:devices,token',
51
            'version'       => 'nullable|string|max:32',
52
            'hostname'      => 'nullable|string|max:255',
53
            'ip'            => 'nullable|ip',
54
            'mac_address'   => 'nullable|string|min:12|max:12',
55
            'time'          => 'nullable|date',
56
            'cover_status'  => 'nullable|string|max:32',
57
            'cover_command'  => 'nullable|alpha|max:5|in:open,close',
58
            'error_msg'     => 'nullable|string',
59
            'limitsw_open'   => 'nullable|boolean',
60
            'limitsw_closed' => 'nullable|boolean',
61
        ])->validate();
62
        
63
        // Get the device record.
64
        $device = Device::getDeviceByUUID($request->input('uuid'));
65
        
66
        // Update the device.
67
        $device->version = $request->input('version');
68
        $device->hostname = $request->input('hostname');
69
        $device->ip = $request->input('ip');
70
        $device->mac_address = $request->input('mac_address');
71
        $device->time = $request->input('time');
72
        $device->cover_status = $request->input('cover_status');
73
        if ($request->input('cover_command') != null)
74
            $device->cover_command = $request->input('cover_command');
75
        $device->error_msg = $request->input('error_msg');
76
        $device->limitsw_open = $request->input('limitsw_open');
77
        $device->limitsw_closed = $request->input('limitsw_closed');
78
        $device->last_network_update_at = Carbon::now();
79
        
80
        $device->save();
81
        
82
        // A 'Registered' event is created and will trigger any relevant
83
        // observers, such as sending a confirmation email or any 
84
        // code that needs to be run as soon as the device is created.
85
        //event(new Registered(true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
        
87
        // Return the new device info including the token.
88
        return response()->json(['data' => $device->toArray()], 201);
89
    }
90
    
91
    /**
92
     * Updates the sensors of a device.
93
     *
94
     * @param  Request  $request
95
     * @return Response
96
     */
97
    public function sensor(Request $request)
98
    {
99
        // Validate the request
100
        $validator = Validator::make($request->all(), [
0 ignored issues
show
Unused Code introduced by
$validator 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...
101
            'uuid'          => 'required|string|max:255|exists:devices,uuid',
102
            'token'         => 'required|string|max:60|exists:devices,token',
103
            'sensor_data.*.name'   => 'required|max:190',
104
            'sensor_data.*.type'   => 'required|max:190',
105
            'sensor_data.*.value'  => 'required|max:190',
106
        ])->validate();
107
        
108
        // Get the device record.
109
        $device = Device::getDeviceByUUID($request->input('uuid'));
110
        
111
        // Update the device.
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
// 		"sensor_data": {
113
// 			{ "name": "cpu", "type": "cpu_temperature", "value": cpu_temp() },
114
// 			{ "name": "temperature", "type": "temperature", "value": temperature() },
115
// 			{ "name": "humidity", "type": "humidity", "value": humidity() },
116
// 			{ "name": "moisture_01", "type": "moisture", "value": 0.00 },
117
// 			{ "name": "moisture_02", "type": "moisture", "value": 0.00 },
118
// 			{ "name": "light_in", "type": "light", "value": 0.00 },
119
// 			{ "name": "light_out", "type": "light", "value": 0.00 }
120
// 		}
121
        $sensor_datas = $request->input('sensor_data');
122
        foreach ($sensor_datas as $sensor_data) {
0 ignored issues
show
Bug introduced by
The expression $sensor_datas of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
123
            $sensor = Sensor::firstOrCreate([
124
                "device_id" => $device->id,
125
                "name" => $sensor_data['name'], 
126
                "type" => $sensor_data['type']
127
            ]);
128
            
129
            SensorData::create([
130
                "sensor_id" => $sensor->id,
131
                "value" => $sensor_data['value']
132
            ]);
133
        }
134
        
135
        // A 'Registered' event is created and will trigger any relevant
136
        // observers, such as sending a confirmation email or any 
137
        // code that needs to be run as soon as the device is created.
138
        //event(new Registered(true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
        
140
        // Return the new device info including the token.
141
        return response()->json(['data' => $device->toArray()], 201);
142
    }
143
    
144
    /**
145
     * Registers a new device.
146
     *
147
     * @param  Request  $request
148
     * @return Response
149
     */
150
    public function register(Request $request)
151
    {
152
        // Validate the request.
153
        $validator = Validator::make($request->all(), [
0 ignored issues
show
Unused Code introduced by
$validator 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...
154
            'uuid' => 'required|string|max:255',
155
            'challenge' => 'required|string|min:6',
156
        ])->validate();
157
        
158
        // If challenge string doesnt match then send 401 unauthorized.
159
        if ($request->input('challenge') != env('API_CHALLENGE', 'temppass')) {
160
            return response()->json(['data' => 'Bad challenge.'], 401);
161
        }
162
        
163
        // If the uuid already exists then just send them the record.
164
        if ($device = Device::getDeviceByUUID($request->input('uuid'))) {
165
            return response()->json([ 'data' => [ 
166
                'name' => $device->name,
167
                'uuid' => $device->uuid,
168
                'id' => $device->id,
169
                'token' => $device->token,
170
            ]], 200);
171
        }
172
        
173
        // Create the new device.
174
        $device = new Device;
175
        $device->name = 'New Device';
176
        $device->uuid = $request->input('uuid');
177
        $device->save();
178
        
179
        // Create an api token for the new device.
180
        $device->generateToken();
181
        
182
        // A 'Registered' event is created and will trigger any relevant
183
        // observers, such as sending a confirmation email or any 
184
        // code that needs to be run as soon as the device is created.
185
        //event(new Registered(true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186
        //Notification::send(User::managers(), new DeviceRegister($device));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
187
        
188
        // Return the new device info including the token.
189
        return response()->json([ 'data' => [ 
190
            'name' => $device->name,
191
            'uuid' => $device->uuid,
192
            'id' => $device->id,
193
            'token' => $device->token,
194
        ]], 201);
195
    }
196
    
197
    /**
198
     * Updates the image for a device.
199
     *
200
     * @param  Request  $request
201
     * @return Response
202
     */
203
    public function image(Request $request) {
204
        // Validate the request.
205
        $validator = Validator::make($request->all(), [
0 ignored issues
show
Unused Code introduced by
$validator 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...
206
            'uuid'          => 'required|string|max:255|exists:devices,uuid',
207
            'token'         => 'required|string|max:60|exists:devices,token',
208
            'image'         => 'required|image|mimes:jpeg,jpg,png,gif|max:2048',
209
        ])->validate();
210
        
211
        // Get the device record.
212
        $device = Device::getDeviceByUUID($request->input('uuid'));
213
        
214
        // Save the image to disk.
215
        $path = $request->file('image')->storeAs('deviceimage', $device['id'], 'private');
216
        
217
        // Update the url for the image.
218
        $deviceimage = Deviceimage::updateOrCreate(
219
            ['device_id' => $device['id']],
220
            ['url' => $path]
221
        );
222
        
223
        // Force the updated_at timestamp to update as the url may not change.
224
        $deviceimage->touch();
225
        
226
        return response()->json([ 'data' => [ 
227
            'id' => $deviceimage['id'],
228
            'url' => $path,
229
        ]], 201);
230
    }
231
}
232
233
// HTTP STATUS CODES:
234
// 200: OK. The standard success code and default option.
235
// 201: Object created. Useful for the store actions.
236
// 204: No content. When an action was executed successfully, but there is no content to return.
237
// 206: Partial content. Useful when you have to return a paginated list of resources.
238
// 400: Bad request. The standard option for requests that fail to pass validation.
239
// 401: Unauthorized. The user needs to be authenticated.
240
// 403: Forbidden. The user is authenticated, but does not have the permissions to perform an action.
241
// 404: Not found. This will be returned automatically by Laravel when the resource is not found.
242
// 500: Internal server error. Ideally you're not going to be explicitly returning this, but if something unexpected breaks, this is what your user is going to receive.
243
// 503: Service unavailable. Pretty self explanatory, but also another code that is not going to be returned explicitly by the application.
244