Completed
Push — develop ( fa7d09...bed248 )
by Tony
10s
created

WidgetDataController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 12
lcom 0
cbo 10
dl 0
loc 121
rs 10
c 6
b 3
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A eventlog() 0 4 1
A alerts() 0 4 1
A syslog() 0 4 1
A worldmap() 0 4 1
B availabilitymap() 0 32 6
B devicesummary() 0 34 2
1
<?php
2
/**
3
 * app/Http/Controllers/Widgets/WidgetDataController.php
4
 *
5
 * HTTP Controller for Widgets data
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Neil Lathwood
23
 * @author     Neil Lathwood <[email protected]>
24
 */
25
26
namespace App\Http\Controllers\Widgets;
27
28
use App\DataTables\Alerting\AlertsDataTable;
29
use App\DataTables\General\EventlogDataTable;
30
use App\DataTables\General\SyslogDataTable;
31
use App\Http\Controllers\Controller;
32
use App\Models\Device;
33
use App\Models\Port;
34
use App\Models\User;
35
use App\Settings;
36
use Illuminate\Http\Request;
37
38
class WidgetDataController extends Controller
39
{
40
    /**
41
     * Display the eventlog widget.
42
     *
43
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
44
     */
45
    public function eventlog(EventlogDataTable $dataTable)
46
    {
47
        return $dataTable->render('general.widget');
48
    }
49
50
    /**
51
     * Display the alerts widget.
52
     *
53
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
54
     */
55
    public function alerts(AlertsDataTable $dataTable)
56
    {
57
        return $dataTable->render('general.widget');
58
    }
59
60
    /**
61
     * Display the syslog widget.
62
     *
63
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
64
     */
65
    public function syslog(SyslogDataTable $dataTable)
66
    {
67
        return $dataTable->render('general.widget');
68
    }
69
70
    /**
71
     * Display the availability-map widget.
72
     *
73
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
74
     */
75
    public function availabilitymap(Settings $settings, Request $request)
76
    {
77
        $uptime = $settings->get('uptime_warning');
78
        if ($request->user()->hasGlobalRead())
79
        {
80
            $devices = Device::where('ignore', '=', 0)->get();
81
        }
82
        else
83
        {
84
            $devices = User::find($request->user()->user_id)->devices()->where('ignore', '=', 0)->get();
85
        }
86
        $count = ['warn' => 0, 'up' => 0, 'down' => 0];
87
        foreach ($devices as $device)
88
        {
89
            if ($device->status == 1)
90
            {
91
                if (($device->uptime < $uptime) && ($device->uptime != '0'))
92
                {
93
                    $count['warn']++;
94
                }
95
                else
96
                {
97
                    $count['up']++;
98
                }
99
            }
100
            else
101
            {
102
                $count['down']++;
103
            }
104
        }
105
        return view('widgets.availability-map', compact(['devices', 'uptime', 'count']));
106
    }
107
108
    /**
109
     * Display the device-summary widget.
110
     *
111
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
112
     */
113
    public function devicesummary(Request $request)
114
    {
115
        $type = $request->route()->getAction()['type'];
116
        $count = [];
117
        if ($request->user()->hasGlobalRead())
118
        {
119
            $count['devices']['total']    = Device::all()->count();
120
            $count['devices']['up']       = Device::isup()->count();
0 ignored issues
show
Bug introduced by
The method count does only exist in Illuminate\Database\Query\Builder, but not in App\Models\Notification.

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...
121
            $count['devices']['down']     = Device::isdown()->count();
122
            $count['devices']['ignored']  = Device::isignored()->count();
123
            $count['devices']['disabled'] = Device::isdisabled()->count();
124
125
            $count['ports']['total']      = Port::notdeleted()->count();
126
            $count['ports']['up']         = Port::with('device')->isup()->count();
127
            $count['ports']['down']       = Port::with('device')->isdown()->count();
128
            $count['ports']['ignored']    = Port::with('device')->isignored()->count();
129
            $count['ports']['disabled']   = Port::with('device')->isdisabled()->count();
130
        }
131
        else
132
        {
133
            $count['devices']['total']    = User::find($request->user()->user_id)->devices()->count();
134
            $count['devices']['up']       = User::find($request->user()->user_id)->devices()->iseup()->count();
135
            $count['devices']['down']     = User::find($request->user()->user_id)->devices()->isdown()->count();
136
            $count['devices']['ignored']  = User::find($request->user()->user_id)->devices()->isignored()->count();
137
            $count['devices']['disabled'] = User::find($request->user()->user_id)->devices()->isdisabled()->count();
138
139
            $count['ports']['total']      = User::find($request->user()->user_id)->ports()->with('device')->count();
140
            $count['ports']['up']         = User::find($request->user()->user_id)->ports()->with('device')->isup()->count();
141
            $count['ports']['down']       = User::find($request->user()->user_id)->ports()->with('device')->isdown()->count();
142
            $count['ports']['ignored']    = User::find($request->user()->user_id)->ports()->with('device')->isignored()->count();
143
            $count['ports']['disabled']   = User::find($request->user()->user_id)->ports()->with('device')->isdisabled()->count();
144
        }
145
        return view('widgets.device-summary', compact(['count', 'type']));
146
    }
147
148
    /**
149
     * Display the Worldmap widget.
150
     *
151
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
152
     */
153
    public function worldmap(Request $request)
154
    {
155
        return view('widgets.worldmap');
156
    }
157
158
}
159