Completed
Pull Request — master (#96)
by
unknown
05:29
created

DashboardController::refreshPage()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 46
ccs 0
cts 25
cp 0
rs 8.9411
cc 3
eloc 28
nc 2
nop 1
crap 12

1 Method

Rating   Name   Duplication   Size   Complexity  
A DashboardController::dev_layout() 0 4 1
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 Illuminate\Support\HtmlString;
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
0 ignored issues
show
Documentation introduced by
The doc-type \Illuminate\Http\Respons...inate\Http\JsonResponse could not be parsed: Unknown type name "|" at position 26. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     */
30
    public function index(Request $request)
31
    {
32
        $request->validate([
33
            'site_id' => 'sometimes|required|integer|digits_between:1,10',
34
            'location_id' => 'sometimes|required|integer|digits_between:1,10',
35
            'device_id' => 'sometimes|required|integer|digits_between:1,10',
36
            'page' => 'sometimes|required|integer|digits_between:1,10',
37
        ]);
38
    
39
        if (count($request->all()) == 0)
40
        {
41
            //Get the device, location, and site ids based on the user's preferred device
42
            //TODO get the user's preferred device
43
            $device = Device::find(3);
44
    
45
            $location = $device->location ?? null;
46
            
47
            //If the location exists then set the pagination page to be where the device is located
48
            if ($location != null)
49
                $request->merge(['page' => $device->dashPageNum(4)]);
50
            $site_id = $location->site_id ?? 0;
51
            $location_id = $location->id ?? 0;
52
            $device_id = $device->id ?? 0;
53
        }
54
        else
55
        {
56
            //Get the active site, location, and device ids
57
            $site_id = $request->site_id ?? 0;
58
            $location_id = $request->location_id ?? 0;
59
            $device_id = $request->device_id ?? 0;
60
        }
61
    
62
        //Get all sites with the selected site first
63
        $sites = Site::select('id', 'name')
64
            ->orderByRaw("id = ? DESC", $site_id)
65
            ->orderBy('name', 'ASC')
66
            ->get();
67
        //Get all locations for the selected site with the selected location first
68
        $locations = Location::select('id', 'name', 'site_id')
69
            ->where('site_id', '=', $sites[0]->id ?? 0)
70
            ->orderByRaw("id = ? DESC", $location_id)
71
            ->orderBy('name', 'ASC')
72
            ->get();
73
        //Get all devices for the selected location
74
        $devices = Device::publicDashData()
75
            ->where('location_id', '=', $locations[0]->id ?? 0)
76
            ->orderBy('name', 'ASC')
77
            ->paginate(4);
78
    
79
        //Get the active device
80
        $active_device = $devices->where('id', $device_id)->first();
81
        //Set the active device to the first device in $devices if it is not empty and the original active device wasn't found
82
        if (!$devices->isEmpty() && $active_device == null)
83
            $active_device = $devices[0];
84
    
85
        //Store the active site, location, and device in a collection
86
        $active_data = collect(['device' => $active_device, 'location' => $locations[0] ?? null, 'site' => $sites[0] ?? null]);
87
    
88
        //Use the device_list.blade.php to generate the device table html
89
        $html_device_table = view('dashboard.device_list', ['devices' => $devices, 'active_data' => $active_data])->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
90
        
91
        if (\Request::ajax())
92
            return response()->json([ 'active_data' => $active_data, 'devices' => $devices, 'locations' => $locations,
93
                'sites' => $sites, 'html_device_table' => $html_device_table ]);
94
        else
95
            return view('dashboard.index', [ 'active_data' => $active_data, 'devices' => $devices,
96
                'locations' => $locations, 'sites' => $sites ]);
97
    }
98
    
99
    /**
100
     * Show the development layouts for dashboard.
101
     *
102
     * @return \Illuminate\Http\Response
103
     */
104
    public function dev_layout()
105
    {
106
        return view('dashboard.dev_layout');
107
    }
108
}
109