Completed
Push — master ( efae37...460051 )
by Brandon
19s
created

DeviceController::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

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 18
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
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
12
class DeviceController extends Controller
13
{
14
    private $device_m = null;
15
    private $site_m = null;
16
    private $location_m = null;
17
    
18
    /**
19
     * Create a new controller instance.
20
     *
21
     */
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
        // TODO: Setup logging
26
        // $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...
27
    
28
        $this->device_m = new Device();
29
        $this->site_m = new Site();
30
        $this->location_m = new Location();
31
    }
32
33
    /**
34
     * Display index page and process dataTable ajax request.
35
     *
36
     * @param \App\DataTables\DevicesDataTable $dataTable
37
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
38
     */
39
    public function index(DevicesDataTable $dataTable)
40
    {
41
        return $dataTable->render('device.index');
42
    }
43
44
    /**
45
     * Show create device page.
46
     *
47
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
48
     */
49
    public function create()
50
    {
51
        return view('device.create');
52
    }
53
54
    /**
55
     * Show the given device.
56
     *
57
     * @param  string  $id
58
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
59
     */
60
    public function show($id)
61
    {
62
        $device = $this->device_m->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<App\Device>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
64
        return view('device.show', [ 'device' => $device ]);
65
    }
66
67
    /**
68
     * View the edit device page.
69
     *
70
     * @param  string  $id
71
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
72
     */
73
    public function edit($id)
74
    {
75
        $device = $this->device_m->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<App\Device>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
76
        
77
        $location = $this->location_m->find($device->location_id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\Location>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
78
        if ($location)
79
        {
80
            $sites = $this->site_m->orderedSitesBy($location->site_id);
81
            $locations = $this->location_m->orderedSiteLocationsBy($location->site_id, $device->location_id);
82
        }
83
        else
84
        {
85
            $locations = null;
86
            $sites = $this->site_m->all();
87
        }
88
89
        return view('device.edit', [ 'device' => $device, 'locations' => $locations, 'sites' => $sites ]);
90
    }
91
    
92
    /**
93
     * Get the locations with the given site id
94
     *
95
     * @param  string  $site_id
96
     * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
97
     */
98
    public function locations($site_id)
99
    {
100
        $locations = $this->location_m->getLocationsBasedOnSite($site_id);
101
    
102
        return $locations;
103
    }
104
105
    /**
106
     * Update the given device.
107
     *
108
     * @param  Request  $request
109
     * @param  string  $id
110
     * @return \Illuminate\Http\RedirectResponse
111
     */
112
    public function update(Request $request, $id)
113
    {
114
        // TODO: Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need
115
        // to add a hidden  _method field to spoof these HTTP verbs. The
116
        // method_field helper can create this field for you:
117
        // {{ 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...
118
119
        $device = $this->device_m->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<App\Device>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
120
        $oldLocationID = $device->location_id;
121
        $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...
122
        $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...
123
124
        // TODO figure out way for unique location names for each specific site
125
        $validator = Validator::make($request->all(), [
126
            'name' => 'required|string|max:255',
127
            'site' => 'required_without:new_site_name|int|max:255|nullable',
128
            'new_site_name' => 'required_without:site|unique:sites,name|nullable',
129
            'location' => 'required_without:new_location_name|int|max:255|nullable',
130
            'new_location_name' => 'required_without:location|unique:locations,name|string|max:255|nullable',
131
        ]);
132
        
133
        if ($validator->fails()) {
134
            return redirect('device/'.$id.'/edit')->withErrors($validator)->withInput();
135
        }
136
        
137
        $siteNew = !empty($request->input('new_site_name'));
138
        //Get the site id of the old or newly created site
139
        if ($siteNew)
140
        {
141
            //Create a new site
142
            $siteName = $request->input('new_site_name');
143
            $site_id = $this->site_m->createSite($siteName);
0 ignored issues
show
Bug introduced by
It seems like $siteName defined by $request->input('new_site_name') on line 142 can also be of type array; however, App\Site::createSite() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
144
        }
145
        else
146
        {
147
            $site_id = $request->input('site');
148
        }
149
        
150
        $locationNew = !empty($request->input('new_location_name'));
151
        //Get the location id of the old or newly created location
152
        if ($locationNew)
153
        {
154
            //Create a new location
155
            $locationName = $request->input('new_location_name');
156
            $location_id = $this->location_m->createLocation($locationName, $site_id);
0 ignored issues
show
Bug introduced by
It seems like $locationName defined by $request->input('new_location_name') on line 155 can also be of type array; however, App\Location::createLocation() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
157
        }
158
        else
159
        {
160
            $location_id = $request->input('location');
161
        }
162
        
163
        //Update the devices name and location_id
164
        $device->location_id = $location_id;
165
        $device->name = $request->input('name');
166
        $device->save();
167
168
        //If the old site isn't connected to a device then remove it
169
        $this->removeUnusedSite($oldLocationID);
170
        
171
        return redirect('device');
172
    }
173
174
    /**
175
     * Deletes a device.
176
     *
177
     * @param  string  $id
178
     * @return \Illuminate\Http\RedirectResponse
179
     */
180
    public function destroy($id)
181
    {
182
        $device = $this->device_m->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<App\Device>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
183
184
        if ($device->trashed())
185
        {
186
            //If the user was already deleted then permanently delete it
187
            $this->device_m->destroy($id);
188
        }
189
        else
190
        {
191
            //Remove the location from the device
192
            $oldLocation_id = $device->location_id;
193
            $this->device_m->updateLocationID($id, null);
194
            //Remove unused location and site if applicable
195
            $this->removeUnusedSite($oldLocation_id);
196
            
197
            //Soft delete the user the first time
198
            $device->delete();
199
        }
200
201
        return redirect('device');
202
    }
203
    
204
    /**
205
     * Confirms deletion of a device.
206
     *
207
     * @param  string  $id
208
     * @return Response
209
     */
210
    public function remove($id)
211
    {
212
        $device = $this->device_m->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<App\Device>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
213
        
214
        return view('device.remove', [ 'device' => $device ]);
215
    }
216
    
217
    /**
218
     * Delete the location with the supplied id if it is not used by any devices
219
     *
220
     * @param  int $oldLocationID
221
     */
222
    private function removeUnusedSite($oldLocationID)
223
    {
224
        //Cleanup left over sites and locations
225
        $noDevice = empty($this->device_m->getFirstDeviceBasedOnLocation($oldLocationID));
226
        if ($noDevice && $oldLocationID != null)
227
        {
228
            $oldLocation = $this->location_m->findOrFail($oldLocationID);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<App\Location>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
229
            $site_id = $oldLocation->site_id;
230
            $locations = $this->location_m->getLocationsBasedOnSite($site_id);
231
            if (sizeof($locations) == 1)
232
            {
233
                //Get the site connected to the location and delete it
234
                $this->site_m->findOrFail($site_id)->delete();
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<App\Site>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
235
            }
236
            else
237
            {
238
                //Delete the location that isn't used anymore
239
                $oldLocation->delete();
240
            }
241
        }
242
    }
243
}
244