Completed
Push — develop ( bed248...1eb861 )
by Tony
8s
created

WidgetDataController::notes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
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\Models\UsersWidgets;
36
use App\Settings;
37
use Illuminate\Http\Request;
38
39
class WidgetDataController extends Controller
40
{
41
    /**
42
     * Display the eventlog widget.
43
     *
44
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
45
     */
46
    public function eventlog(EventlogDataTable $EventlogDataTable, $action = null)
47
    {
48
        $tableName = ['id' => 'eventlogDT'];
49
        return $EventlogDataTable->render('widgets.eventlog', compact(['tableName', 'action']));
50
    }
51
52
    /**
53
     * Display the alerts widget.
54
     *
55
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
56
     */
57
    public function alerts(AlertsDataTable $dataTable, $action = null)
58
    {
59
        $tableName = ['id' => 'alertlogDT'];
60
        return $dataTable->render('widgets.alertlog', compact(['tableName', 'action']));
61
    }
62
63
    /**
64
     * Display the syslog widget.
65
     *
66
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
67
     */
68
    public function syslog(SyslogDataTable $dataTable, $action = null)
69
    {
70
        $tableName = ['id' => 'syslogDT'];
71
        return $dataTable->render('widgets.syslog', compact(['tableName', 'action']));
72
    }
73
74
    /**
75
     * Display the availability-map widget.
76
     *
77
     * @param string $action
78
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
79
     */
80
    public function availabilitymap(Settings $settings, Request $request, $action = null)
81
    {
82
        $uptime = $settings->get('uptime_warning');
83
        if ($request->user()->hasGlobalRead())
84
        {
85
            $devices = Device::where('ignore', '=', 0)->get();
86
        }
87
        else
88
        {
89
            $devices = User::find($request->user()->user_id)->devices()->where('ignore', '=', 0)->get();
90
        }
91
        $count = ['warn' => 0, 'up' => 0, 'down' => 0];
92
        foreach ($devices as $device)
93
        {
94
            if ($device->status == 1)
95
            {
96
                if (($device->uptime < $uptime) && ($device->uptime != '0'))
97
                {
98
                    $count['warn']++;
99
                }
100
                else
101
                {
102
                    $count['up']++;
103
                }
104
            }
105
            else
106
            {
107
                $count['down']++;
108
            }
109
        }
110
        $widget_settings = json_decode(UsersWidgets::getSettings($request)->value('settings'));
111
        return view('widgets.availability-map', compact(['devices', 'uptime', 'count', 'action', 'widget_settings']));
112
    }
113
114
    /**
115
     * Display the device-summary widget.
116
     *
117
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
118
     */
119
    public function devicesummary(Request $request, $action = null)
120
    {
121
        $type = $request->route()->getAction()['type'];
122
        $count = [];
123
        if ($request->user()->hasGlobalRead())
124
        {
125
            $count['devices']['total']    = Device::all()->count();
126
            $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...
127
            $count['devices']['down']     = Device::isdown()->count();
128
            $count['devices']['ignored']  = Device::isignored()->count();
129
            $count['devices']['disabled'] = Device::isdisabled()->count();
130
131
            $count['ports']['total']      = Port::notdeleted()->count();
132
            $count['ports']['up']         = Port::with('device')->isup()->count();
133
            $count['ports']['down']       = Port::with('device')->isdown()->count();
134
            $count['ports']['ignored']    = Port::with('device')->isignored()->count();
135
            $count['ports']['disabled']   = Port::with('device')->isdisabled()->count();
136
        }
137
        else
138
        {
139
            $count['devices']['total']    = User::find($request->user()->user_id)->devices()->count();
140
            $count['devices']['up']       = User::find($request->user()->user_id)->devices()->iseup()->count();
141
            $count['devices']['down']     = User::find($request->user()->user_id)->devices()->isdown()->count();
142
            $count['devices']['ignored']  = User::find($request->user()->user_id)->devices()->isignored()->count();
143
            $count['devices']['disabled'] = User::find($request->user()->user_id)->devices()->isdisabled()->count();
144
145
            $count['ports']['total']      = User::find($request->user()->user_id)->ports()->with('device')->count();
146
            $count['ports']['up']         = User::find($request->user()->user_id)->ports()->with('device')->isup()->count();
147
            $count['ports']['down']       = User::find($request->user()->user_id)->ports()->with('device')->isdown()->count();
148
            $count['ports']['ignored']    = User::find($request->user()->user_id)->ports()->with('device')->isignored()->count();
149
            $count['ports']['disabled']   = User::find($request->user()->user_id)->ports()->with('device')->isdisabled()->count();
150
        }
151
        return view('widgets.device-summary', compact(['count', 'type', 'action']));
152
    }
153
154
    /**
155
     * Display the Worldmap widget.
156
     *
157
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
158
     */
159
    public function worldmap(Request $request, $action = null)
160
    {
161
        return view('widgets.worldmap', compact(['action']));
162
    }
163
164
    /**
165
     * Display the notes widget.
166
     *
167
     * @param string $action
168
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
169
     */
170
    public function notes(Settings $settings, Request $request, $action = null)
171
    {
172
        $widget_settings = json_decode(UsersWidgets::getSettings($request)->value('settings'));
173
        return view('widgets.notes', compact(['action', 'widget_settings']));
174
    }
175
176
}
177