Completed
Push — master ( ffe767...90fa06 )
by Brandon
02:24
created

DeviceController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
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
    /**
15
     * Create a new controller instance.
16
     *
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
        // TODO: Setup logging
22
        // $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...
23
    }
24
25
    /**
26
     * Display index page and process dataTable ajax request.
27
     *
28
     * @param \App\DataTables\DevicesDataTable $dataTable
29
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
30
     */
31
    public function index(DevicesDataTable $dataTable)
32
    {
33
        return $dataTable->render('device.index');
34
    }
35
36
    /**
37
     * Show create device page.
38
     *
39
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
40
     */
41
    public function create()
42
    {
43
        return view('device.create');
44
    }
45
46
    /**
47
     * Show the given device.
48
     *
49
     * @param  Request  $request
50
     * @param  string  $id
51
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
52
     */
53
    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...
54
    {
55
        $device = Device::findOrFail($id);
56
57
        return view('device.show', ['device' => $device]);
58
    }
59
60
    /**
61
     * View the edit device page.
62
     *
63
     * @param  Request  $request
64
     * @param  string  $id
65
     * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
66
     */
67
    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...
68
    {
69
        $device = Device::findOrFail($id);
70
        $location = Location::where('id', $device->location_id)->first();
71
        if ($location)
72
            $site = Site::where('id', $location->site_id)->first();
73
        else
74
            $site = null;
75
76
        return view('device.edit', ['device' => $device, 'location' => $location, 'site' => $site]);
77
    }
78
79
    /**
80
     * Update the given device.
81
     *
82
     * @param  Request  $request
83
     * @param  string  $id
84
     * @return Response
85
     */
86
    public function update(Request $request, $id)
87
    {
88
        // TODO: Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need
89
        // to add a hidden  _method field to spoof these HTTP verbs. The
90
        // method_field helper can create this field for you:
91
        // {{ 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...
92
93
        $device = Device::findOrFail($id);
94
        $oldLocationID = $device->location_id;
95
        $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...
96
        $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...
97
98
        $validator = Validator::make($request->all(), [
99
            'name' => 'required|string|max:255',
100
            'site' => 'string|max:255',
101
            'location' => 'string|max:255',
102
        ]);
103
        
104
        if ($validator->fails()) {
105
            return redirect('device/'.$id.'/edit')->withErrors($validator)->withInput();
106
        }
107
    
108
        //Check if the site entered by the user is already created
109
        $siteExist = Site::where('name', $request->input('site'))->first();
110
        if (!$siteExist)
111
        {
112
            //Create a new site
113
            $site = new Site;
114
            $site->name = $request->input('site');
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...
115
            $site->save();
116
        }
117
    
118
        //Check if the location entered by the user is already created and connected to the same site
119
        $site = Site::where('name', $request->input('site'))->first();
120
        $locationExist = Location::where('name', $request->input('location'))->first();
121
        if (!$locationExist || !$siteExist)
122
        {
123
            //Create a new location
124
            $location = new Location;
125
            $location->name = $request->input('location');
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...
126
            $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...
127
            $location->save();
128
        }
129
        
130
        //Update the devices name and location_id
131
        $location = Location::where('name', $request->input('location'))
132
                                    ->where('site_id', $site->id)->first();
133
        $device->location_id = $location->id;
134
        $device->name = $request->input('name');
135
        $device->save();
136
137
        //If the old site isn't connected to a device then remove it
138
        $this->removeUnusedSite($oldLocationID);
139
        
140
        return redirect('device');
141
    }
142
143
    /**
144
     * Deletes a device.
145
     *
146
     * @param  Request  $request
147
     * @param  string  $id
148
     * @return Response
149
     */
150
    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...
151
    {
152
        $device = Device::findOrFail($id);
153
154
        if ($device->trashed())
155
        {
156
            // if the user was already deleted then permananetly delete it
157
            Device::destroy($id);
158
        }
159
        else
160
        {
161
            //Remove the location from the device
162
            $oldLocation_id = $device->location_id;
163
            $device->location_id = null;
164
            $device->save();
165
            //Remove unused location and site if applicable
166
            $this->removeUnusedSite($oldLocation_id);
167
            
168
            // soft delete the user the first time
169
            $device->delete();
170
        }
171
172
        return redirect('device');
173
    }
174
    
175
    /**
176
     * Confirms deletion of a device.
177
     *
178
     * @param  Request  $request
179
     * @param  string  $id
180
     * @return Response
181
     */
182
    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...
183
    {
184
        $device = Device::findOrFail($id);
185
        
186
        return view('device.remove', ['device' => $device]);
187
    }
188
    
189
    /**
190
     * If a site is not connected to a device then delete the site
191
     *
192
     * @param  int $oldLocationID
193
     */
194
    private function removeUnusedSite($oldLocationID)
195
    {
196
        //Cleanup left over sites and locations
197
        $deviceExist = Device::where('location_id', $oldLocationID)->first();
198
        if (!$deviceExist && $oldLocationID != null)
199
        {
200
            $oldLocation = Location::where('id', $oldLocationID)->firstOrFail()->site_id;
201
            Site::where('id', $oldLocation)->delete();
202
        }
203
    }
204
}
205