Completed
Pull Request — master (#26)
by
unknown
02:01
created

DeviceController::edit()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 16
cp 0
rs 8.8571
cc 2
eloc 19
nc 2
nop 2
crap 6
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Http\Request;
7
use App\DataTables\DevicesDataTable;
8
use App\Device;
9
use App\Site;
10
use App\Location;
11
use Illuminate\Support\Facades\DB;
12
13
class DeviceController extends Controller
14
{
15
    /**
16
     * Create a new controller instance.
17
     *
18
     */
19
    public function __construct()
20
    {
21
        $this->middleware('auth');
22
        // TODO: Setup logging
23
        // $this->middleware('log')->only('index');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
24
    }
25
26
    /**
27
     * Display index page and process dataTable ajax request.
28
     *
29
     * @param \App\DataTables\DevicesDataTable $dataTable
30
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
31
     */
32
    public function index(DevicesDataTable $dataTable)
33
    {
34
        return $dataTable->render('device.index');
35
    }
36
37
    /**
38
     * Show create device page.
39
     *
40
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
41
     */
42
    public function create()
43
    {
44
        return view('device.create');
45
    }
46
47
    /**
48
     * Show the given device.
49
     *
50
     * @param  Request  $request
51
     * @param  string  $id
52
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
53
     */
54
    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...
55
    {
56
        $device = Device::findOrFail($id);
57
58
        return view('device.show', [ 'device' => $device ]);
59
    }
60
61
    /**
62
     * View the edit device page.
63
     *
64
     * @param  Request  $request
65
     * @param  string  $id
66
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
67
     */
68
    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...
69
    {
70
        $device = Device::findOrFail($id);
71
        
72
        $location = Location::where('id', $device->location_id)->first();
73
        if ($location)
74
        {
75
            $sites = Site::query()->select([
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
76
                                        'id',
77
                                        'name',
78
                                    ])
79
                                    ->orderByRaw(DB::raw("(id = " . $location->site_id . ") DESC"))
80
                                    ->get();
81
            $locations = Location::query()->select([
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
82
                                                'id',
83
                                                'name',
84
                                            ])
85
                                            ->where('site_id', '=', $location->site_id)
86
                                            ->orderByRaw(DB::raw("(id = " . $device->location_id . ") DESC"))
87
                                            ->get();
88
        }
89
        else
90
        {
91
            $locations = null;
92
            $sites = Site::all();
93
        }
94
95
        return view('device.edit', [ 'device' => $device, 'locations' => $locations, 'sites' => $sites ]);
96
    }
97
    
98
    /**
99
     * View the edit device page.
100
     *
101
     * @param  string  $site_id
102
     * @return
103
     */
104
    public function locations($site_id)
105
    {
106
        $locations = Location::where('site_id', $site_id)->get();
107
    
108
        return $locations;
109
    }
110
111
    /**
112
     * Update the given device.
113
     *
114
     * @param  Request  $request
115
     * @param  string  $id
116
     * @return \Illuminate\Http\RedirectResponse
117
     */
118
    public function update(Request $request, $id)
119
    {
120
        // TODO: Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need
121
        // to add a hidden  _method field to spoof these HTTP verbs. The
122
        // method_field helper can create this field for you:
123
        // {{ method_field('PUT') }}
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...
124
125
        $device = Device::findOrFail($id);
126
        $oldLocationID = $device->location_id;
127
        $location = null;
0 ignored issues
show
Unused Code introduced by
$location 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...
128
        $site = null;
0 ignored issues
show
Unused Code introduced by
$site 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...
129
130
        // TODO figure out way for unique location names for each specific site
131
        $validator = Validator::make($request->all(), [
132
            'name' => 'required|string|max:255',
133
            'site' => 'required_without:new_site_name|int|max:255|nullable',
134
            'new_site_name' => 'required_without:site|unique:sites,name|nullable',
135
            'location' => 'required_without:new_location_name|int|max:255|nullable',
136
            'new_location_name' => 'required_without:location|unique:locations,name|string|max:255|nullable',
137
        ]);
138
        
139
        if ($validator->fails()) {
140
            return redirect('device/'.$id.'/edit')->withErrors($validator)->withInput();
141
        }
142
        
143
        $siteNew = !empty($request->input('new_site_name'));
144
        //Get the site id of the old or newly created site
145
        if ($siteNew)
146
        {
147
            //Create a new site
148
            $site = new Site;
149
            $siteName = $request->input('new_site_name');
150
            $site->name = $siteName;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Site>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
151
            $site->save();
152
            $site_id = $site->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Site>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
        }
154
        else
155
        {
156
            $site_id = $request->input('site');
157
        }
158
        
159
        $locationNew = !empty($request->input('new_location_name'));
160
        //Get the location id of the old or newly created location
161
        if ($locationNew)
162
        {
163
            //Create a new location
164
            $location = new Location;
165
            $locationName = $request->input('new_location_name');
166
            $location->name = $locationName;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Location>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
167
            $location->site_id = $site_id;
0 ignored issues
show
Documentation introduced by
The property site_id does not exist on object<App\Location>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
            $location->save();
169
            $location_id = $location->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Location>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
170
        }
171
        else
172
        {
173
            $location_id = $request->input('location');
174
        }
175
        
176
        //Update the devices name and location_id
177
        $location = Location::where('id', $location_id)
178
                                    ->where('site_id', $site_id)->first();
179
        $device->location_id = $location->id;
180
        $device->name = $request->input('name');
181
        $device->save();
182
183
        //If the old site isn't connected to a device then remove it
184
        $this->removeUnusedSite($oldLocationID);
185
        
186
        return redirect('device');
187
    }
188
189
    /**
190
     * Deletes a device.
191
     *
192
     * @param  Request  $request
193
     * @param  string  $id
194
     * @return \Illuminate\Http\RedirectResponse
195
     */
196
    public function destroy(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...
197
    {
198
        $device = Device::findOrFail($id);
199
200
        if ($device->trashed())
201
        {
202
            // if the user was already deleted then permananetly delete it
203
            Device::destroy($id);
204
        } else
205
        {
206
            //Remove the location from the device
207
            $oldLocation_id = $device->location_id;
208
            $device->location_id = null;
209
            $device->save();
210
            //Remove unused location and site if applicable
211
            $this->removeUnusedSite($oldLocation_id);
212
            
213
            // soft delete the user the first time
214
            $device->delete();
215
        }
216
217
        return redirect('device');
218
    }
219
    
220
    /**
221
     * Confirms deletion of a device.
222
     *
223
     * @param  Request  $request
224
     * @param  string  $id
225
     * @return Response
226
     */
227
    public function remove(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...
228
    {
229
        $device = Device::findOrFail($id);
230
        
231
        return view('device.remove', [ 'device' => $device ]);
232
    }
233
    
234
    /**
235
     * If a site is not connected to a device then delete the site
236
     *
237
     * @param  int $oldLocationID
238
     */
239
    private function removeUnusedSite($oldLocationID)
240
    {
241
        //Cleanup left over sites and locations
242
        $deviceExist = Device::where('location_id', '=', $oldLocationID)->first();
243
        if (!$deviceExist && $oldLocationID != null)
244
        {
245
            $site_id = Location::where('id', '=', $oldLocationID)->firstOrFail()->site_id;
246
            $locations = Location::where('site_id', '=', $site_id)->get();
247
            if (sizeof($locations) == 1)
248
            {
249
                //Get the site connected to the location and delete it
250
                Site::where('id', '=', $site_id)->delete();
251
            }
252
            else
253
            {
254
                //Delete the location that isn't used anymore
255
                Location::where('id', '=', $oldLocationID)->delete();
256
            }
257
        }
258
    }
259
}
260