Completed
Pull Request — master (#63)
by Brandon
02:14
created

DashboardController::ajaxRefreshAll()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 44
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
ccs 0
cts 18
cp 0
rs 8.5806
cc 4
eloc 20
nc 4
nop 1
crap 20
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Http\Request;
7
use Validator;
8
use App\Device;
9
use App\Site;
10
use App\Location;
11
use Carbon\Carbon;
12
13
class DashboardController extends Controller
14
{
15
    /**
16
     * Create a new controller instance.
17
     *
18
     */
19
    public function __construct()
20
    {
21
        $this->middleware('auth');
22
    }
23
24
    /**
25
     * Show the application dashboard.
26
     *
27
     * @return \Illuminate\Http\Response
28
     */
29 View Code Duplication
    public function index()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        //Todo use the users preferred device
32
        //Get the very first device
33
        $device = Device::publicDashData()->first();
0 ignored issues
show
Bug introduced by
The method publicDashData() does not exist on App\Device. Did you maybe mean scopePublicDashData()?

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...
34
        //Get the devices location
35
        $location = $device->location()->select('id', 'name', 'site_id')->firstOrFail();
36
        //Get the devices site
37
        $site = $location->site()->select('id', 'name')->firstOrFail();
38
    
39
        $data = $this->dashData($site, $location, $device);
40
        
41
        return view('dashboard.index', [ 'active_device' => $data[0], 'devices' => $data[1], 'locations' => $data[2], 'sites' => $data[3] ]);
42
    }
43
    
44
    /**
45
     * Show the development layouts for dashboard.
46
     *
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function dev_layout()
50
    {
51
        return view('dashboard.dev_layout');
52
    }
53
    
54
    /**
55
     * Refresh all the data on the dashboard except the image and sensor graphs
56
     *
57
     * @param  Request  $request
58
     * @return \Illuminate\Http\JsonResponse
59
     */
60
    public function ajaxRefreshAll(Request $request)
61
    {
62
        Validator::make($request->all(), [
63
            'site_id' => 'required|int|max:255',
64
            'location_id' => 'required|int|max:255',
65
            'device_id' => 'required|int|max:255',
66
        ])->validate();
67
        
68
        //Todo limit the amount of queries based on if there is changes
69
        //Check for changes in database since last update
70
    
71
        //Get the given site
72
        $site = Site::select('id', 'name')->find($request->site_id);
73
        //Get the given location
74
        $location = Location::select('id', 'name', 'site_id')->find($request->location_id);
75
        //Get the given device if it is still at the same location
76
        $device = Device::publicDashData()->where('location_id', '=', $request->location_id)->find($request->device_id);
0 ignored issues
show
Bug introduced by
The method publicDashData() does not exist on App\Device. Did you maybe mean scopePublicDashData()?

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...
77
        
78
        if (empty($site))                 //Check if the site still exists
79
        {
80
            //Get the first site and if no sites exist throw an error
81
            $site = Site::select('id', 'name')->firstOrFail();
82
            //Get the first location from the site
83
            $location = $site->locations()->select('id', 'name', 'site_id')->firstOrFail();
84
            //Get the first device from the location
85
            $device = $location->devices()->publicDashData()->firstOrFail();
86
        }
87
        else if (empty($location))        //Check if the location still exists
88
        {
89
            //Get the first location from the given site
90
            $location = $site->locations()->select('id', 'name', 'site_id')->firstOrFail();
91
            //Get the first device from the location
92
            $device = $location->devices()->publicDashData()->firstOrFail();
93
        }
94
        else if (empty($device))        //Check if the device still exists at the given location
95
        {
96
            //Get the first device from the location
97
            $device = $location->devices()->publicDashData()->firstOrFail();
98
        }
99
        
100
        $data = $this->dashData($site, $location, $device);
101
        
102
        return response()->json([ 'active_device' => $data[0], 'devices' => $data[1], 'locations' => $data[2], 'sites' => $data[3] ]);
103
    }
104
    
105
    /**
106
     * Return database data about devices, sites, and locations
107
     *
108
     * @param int $site_id
109
     * @return \Illuminate\Http\JsonResponse
110
     */
111
    public function siteChange($site_id)
112
    {
113
        //Get the given site
114
        $site = Site::select('id', 'name')->findOrFail($site_id);
115
        //Get the given location
116
        $location = Location::select('id', 'name', 'site_id')->where('site_id', '=', $site->id)->firstOrFail();
117
        //Get the given device if it is still at the same location
118
        $device = Device::publicDashData()->where('location_id', '=', $location->id)->firstOrFail();
0 ignored issues
show
Bug introduced by
The method publicDashData() does not exist on App\Device. Did you maybe mean scopePublicDashData()?

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...
119
        
120
        $data = $this->dashData($site, $location, $device);
121
        
122
        return response()->json([ 'active_device' => $data[0], 'devices' => $data[1], 'locations' => $data[2], 'sites' => $data[3] ]);
123
    }
124
    
125
    /**
126
     * Return database data about devices, sites, and locations
127
     *
128
     * @param int $location_id
129
     * @return \Illuminate\Http\JsonResponse
130
     */
131 View Code Duplication
    public function locationChange($location_id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        //Get the given location
134
        $location = Location::select('id', 'name', 'site_id')->findOrFail($location_id);
135
        //Get the given site
136
        $site = $location->site()->select('id', 'name')->firstOrFail();
137
        //Get the given device if it is still at the same location
138
        $device = Device::publicDashData()->where('location_id', '=', $location_id)->firstOrFail();
0 ignored issues
show
Bug introduced by
The method publicDashData() does not exist on App\Device. Did you maybe mean scopePublicDashData()?

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...
139
        
140
        $data = $this->dashData($site, $location, $device);
141
        
142
        return response()->json([ 'active_device' => $data[0], 'devices' => $data[1], 'locations' => $data[2], 'sites' => $data[3] ]);
143
    }
144
    
145
    /**
146
     * Return database data about devices, sites, and locations
147
     *
148
     * @param \Illuminate\Database\Eloquent\Model $site
149
     * @param \Illuminate\Database\Eloquent\Model $location
150
     * @param \Illuminate\Database\Eloquent\Model $device
151
     * @return \Illuminate\Database\Eloquent\Collection
152
     */
153
    public function dashData($site, $location, $device)
154
    {
155
        //Get all the sites except for the current site ordered by name
156
        $sites = Site::select('id', 'name')->orderBy('name', 'ASC')->get()->except($site->id);
157
        
158
        //Get all the locations for the given site except for the new current location ordered by name
159
        $locations = $site->locations()->select('id', 'name', 'site_id')->orderBy('name', 'ASC')->get()->except($location->id);
160
        
161
        //Get all the devices that belong to the given location ordered by name
162
        $devices = $location->devices()->publicDashData()->orderBy('name', 'ASC')->get();
163
        
164
        $active_device = collect([$device, $location, $site]);
165
        
166
        return collect([$active_device, $devices, $locations, $sites]);
167
    }
168
    
169
    /**
170
     * Open or close the given device
171
     *
172
     * @param  Request  $request
173
     * @param Device $device
174
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
175
     */
176
    public function updateCommand(Request $request, Device $device)
177
    {
178
        Validator::make($request->all(), [
179
            'command' => 'required|string|max:1',
180
        ])->validate();
181
        
182
        //Check that device isn't currently in use or has an error
183
        if ($device->cover_status === 'opening' || $device->cover_status === 'closing' || $device->cover_status === 'error')
0 ignored issues
show
Documentation introduced by
The property cover_status does not exist on object<App\Device>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
184
            return response()->json("Device is currently in use.", 403);
185
        
186
        //1 = open, 2 = close, 3 = lock
187
        switch($request->command)
188
        {
189
            case 1:
190
                $device->cover_command = 'open';
0 ignored issues
show
Documentation introduced by
The property cover_command does not exist on object<App\Device>. 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...
191
                break;
192
            case 2:
193
                $device->cover_command = 'close';
0 ignored issues
show
Documentation introduced by
The property cover_command does not exist on object<App\Device>. 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...
194
                break;
195
            case 3:
196
                $device->cover_command = $this->disableCommand($device);
0 ignored issues
show
Documentation introduced by
The property cover_command does not exist on object<App\Device>. 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...
197
                break;
198
        }
199
        
200
        $device->save();
201
        
202
        return response()->json("Success");
203
    }
204
    
205
    /**
206
     * Get the devices command based on the device being locked or unlocked
207
     *
208
     * @param Device $device
209
     * @return string
210
     */
211
    public function disableCommand(Device $device)
212
    {
213
        //Check if the device is already locked or not
214
        if ($device->cover_command === 'lock')
0 ignored issues
show
Documentation introduced by
The property cover_command does not exist on object<App\Device>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
215
        {
216
            //Get the open, close, and current time in the users timezone
217
            $open_time = new Carbon($device->open_time, Auth::user()->timezone);
0 ignored issues
show
Documentation introduced by
The property open_time does not exist on object<App\Device>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
218
            $close_time = new Carbon($device->close_time, Auth::user()->timezone);
0 ignored issues
show
Documentation introduced by
The property close_time does not exist on object<App\Device>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
219
            $time_now = Carbon::now(Auth::user()->timezone);
0 ignored issues
show
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
220
            
221
            //Check if the current time is during the open schedule or not
222
            if (($time_now > $open_time) && ($time_now < $close_time))
223
                $command =  'open';
224
            else
225
                $command =  'close';
226
        }
227
        else
228
            $command =  'lock';
229
        
230
        return $command;
231
    }
232
}
233