DashboardController::dev_layout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Gate;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Http\Request;
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
     * @param  Request  $request
28
     * @return \Illuminate\Http\Response||\Illuminate\Http\JsonResponse
29
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment \Illuminate\Http\Respons...inate\Http\JsonResponse at position 2 could not be parsed: Unknown type name '|' at position 2 in \Illuminate\Http\Response||\Illuminate\Http\JsonResponse.
Loading history...
30
    public function index(Request $request)
31
    {
32 View Code Duplication
        if (Gate::denies('index-dashboard'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
33
        {
34
            if (\Request::ajax()) {
35
                return response()->json("Please contact [email protected] to request access.", 403);
36
            } else {
37
                return redirect()->route('home')
38
                    ->with('failure', 'Please contact [email protected] to request access.');
39
            }
40
        }
41
        
42
        $request->validate([
43
            'site_id' => 'sometimes|required|integer|digits_between:1,10',
44
            'location_id' => 'sometimes|required|integer|digits_between:1,10',
45
            'device_id' => 'sometimes|required|integer|digits_between:1,10',
46
            'page' => 'sometimes|required|integer|digits_between:1,10',
47
        ]);
48
    
49
        if (count($request->all()) == 0)
50
        {
51
            //Get the device, location, and site ids based on the user's preferred device
52
            $device = Auth::user()->preferredDevice ?? null;
53
            $location = $device->location ?? null;
54
            
55
            //If the location exists then set the pagination page to be where the device is located
56
            if ($location != null)
57
                $request->merge([ 'page' => $device->dashPageNum(4) ]);
0 ignored issues
show
Bug introduced by
The method dashPageNum() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
                $request->merge([ 'page' => $device->/** @scrutinizer ignore-call */ dashPageNum(4) ]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
            $site_id = $location->site_id ?? 0;
59
            $location_id = $location->id ?? 0;
60
            $device_id = $device->id ?? 0;
61
        }
62
        else
63
        {
64
            //Get the active site, location, and device ids
65
            $site_id = $request->site_id ?? 0;
66
            $location_id = $request->location_id ?? 0;
67
            $device_id = $request->device_id ?? 0;
68
        }
69
    
70
        //Get all sites with the selected site first
71
        $sites = Site::select('id', 'name')
72
            ->orderByRaw("id = ? DESC", $site_id)
0 ignored issues
show
Bug introduced by
It seems like $site_id can also be of type integer; however, parameter $bindings of Illuminate\Database\Query\Builder::orderByRaw() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            ->orderByRaw("id = ? DESC", /** @scrutinizer ignore-type */ $site_id)
Loading history...
73
            ->orderBy('name', 'ASC')
74
            ->get();
75
        //Get all locations for the selected site with the selected location first
76
        $locations = Location::select('id', 'name', 'site_id')
77
            ->where('site_id', '=', $sites[ 0 ]->id ?? 0)
78
            ->orderByRaw("id = ? DESC", $location_id)
79
            ->orderBy('name', 'ASC')
80
            ->get();
81
        //Get all devices for the selected location
82
        $devices = Device::publicDashData()
83
            ->leftJoin('deviceimages as image', 'devices.id', '=', 'image.device_id')
84
            ->where('location_id', '=', $locations[ 0 ]->id ?? 0)
85
            ->orderBy('name', 'ASC')
86
            ->paginate(4);
87
    
88
        //Set the active device to the device defined by the given device id
89
        $active_device = $devices->where('id', $device_id)->first();
90
        
91
        //Check if there were devices found
92
        if (!$devices->isEmpty())
93
        {
94
            //If the original device with the given device id was not found
95
            //Then assign the the first device in the device list as the active device
96
            if ($active_device == null)
97
                $active_device = $devices[ 0 ];
98
        
99
            //Add an attribute to each device defining if it is stale
100
            $devices->transform(function($item)
101
            {
102
                //Mark the device as stale if the device has missed three updates plus a minute
103
                $deviceStaleMins = ceil(($item->update_rate * 3) / 60) + 1;
104
                $item[ 'isDeviceStale' ] = ($item->last_network_update_at <= Carbon::now()->subMinute($deviceStaleMins)) ? true : false;
0 ignored issues
show
Bug introduced by
$deviceStaleMins of type double is incompatible with the type integer expected by parameter $value of Carbon\Carbon::subMinute(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
                $item[ 'isDeviceStale' ] = ($item->last_network_update_at <= Carbon::now()->subMinute(/** @scrutinizer ignore-type */ $deviceStaleMins)) ? true : false;
Loading history...
105
    
106
                //Mark the device image as stale if the device has missed three image updates plus a minute
107
                if ($item->image_updated_at != null)
108
                {
109
                    $imageStaleMins = ceil(($item->image_rate * 3) / 60) + 1;
110
                    $item[ 'isImageStale' ] = ($item->image_updated_at <= Carbon::now()->subMinute($imageStaleMins)) ? true : false;
111
                }
112
                else
113
                    $item[ 'isImageStale' ] = false;
114
                
115
                return $item;
116
            });
117
        }
118
    
119
        //Store the active site, location, and device in a collection
120
        $active_data = collect([ 'device' => $active_device, 'location' => $locations[ 0 ] ?? null, 'site' => $sites[ 0 ] ?? null ]);
121
    
122
        //Use the device_list.blade.php to generate the device table html
123
        $html_device_table = view('dashboard.device_list', [ 'devices' => $devices, 'active_data' => $active_data ])->render();
124
        
125
        if (\Request::ajax()) {
126
                    return response()->json([ 'active_data' => $active_data, 'devices' => $devices, 'locations' => $locations,
127
                'sites' => $sites, 'html_device_table' => $html_device_table ]);
128
        } else {
129
                    return view('dashboard.index', [ 'active_data' => $active_data, 'devices' => $devices,
130
                'locations' => $locations, 'sites' => $sites ]);
131
        }
132
    }
133
    
134
    /**
135
     * Show the development layouts for dashboard.
136
     *
137
     * @return \Illuminate\Http\Response
138
     */
139
    public function dev_layout()
140
    {
141
        return view('dashboard.dev_layout');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('dashboard.dev_layout') returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
142
    }
143
}
144