Completed
Pull Request — develop (#123)
by Neil
08:55 queued 18s
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
use JWTAuth;
39
40
class WidgetDataController extends Controller
41
{
42
    /**
43
     * Display the eventlog widget.
44
     *
45
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
46
     */
47
    public function eventlog(EventlogDataTable $EventlogDataTable, $action = null)
48
    {
49
        $tableName = ['id' => 'eventlogDT'];
50
        return $EventlogDataTable->render('widgets.eventlog', compact(['tableName', 'action']));
51
    }
52
53
    /**
54
     * Display the alerts widget.
55
     *
56
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
57
     */
58
    public function alerts(AlertsDataTable $dataTable, $action = null)
59
    {
60
        $tableName = ['id' => 'alertlogDT'];
61
        return $dataTable->render('widgets.alertlog', compact(['tableName', 'action']));
62
    }
63
64
    /**
65
     * Display the syslog widget.
66
     *
67
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
68
     */
69
    public function syslog(SyslogDataTable $dataTable, $action = null)
70
    {
71
        $tableName = ['id' => 'syslogDT'];
72
        return $dataTable->render('widgets.syslog', compact(['tableName', 'action']));
73
    }
74
75
    /**
76
     * Display the availability-map widget.
77
     *
78
     * @param string $action
79
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
80
     */
81
    public function availabilitymap(Settings $settings, Request $request, $action = null)
82
    {
83
        $uptime = $settings->get('uptime_warning');
84
        if ($request->user()->hasGlobalRead())
85
        {
86
            $devices = Device::where('ignore', '=', 0)->get();
87
        }
88
        else
89
        {
90
            $devices = User::find($request->user()->user_id)->devices()->where('ignore', '=', 0)->get();
91
        }
92
        $count = ['warn' => 0, 'up' => 0, 'down' => 0];
93
        foreach ($devices as $device)
94
        {
95
            if ($device->status == 1)
96
            {
97
                if (($device->uptime < $uptime) && ($device->uptime != '0'))
98
                {
99
                    $count['warn']++;
100
                }
101
                else
102
                {
103
                    $count['up']++;
104
                }
105
            }
106
            else
107
            {
108
                $count['down']++;
109
            }
110
        }
111
        $widget_settings = json_decode(UsersWidgets::getSettings($request)->value('settings'));
0 ignored issues
show
Bug introduced by
The method getSettings() does not exist on App\Models\UsersWidgets. Did you maybe mean scopeGetSettings()?

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...
112
        return view('widgets.availability-map', compact(['devices', 'uptime', 'count', 'action', 'widget_settings']));
113
    }
114
115
    /**
116
     * Display the device-summary widget.
117
     *
118
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
119
     */
120
    public function devicesummary(Request $request, $action = null)
121
    {
122
        $type = $request->route()->getAction()['type'];
123
        $count = [];
124
        if ($request->user()->hasGlobalRead())
125
        {
126
            $count['devices']['total']    = Device::all()->count();
127
            $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...
128
            $count['devices']['down']     = Device::isdown()->count();
129
            $count['devices']['ignored']  = Device::isignored()->count();
130
            $count['devices']['disabled'] = Device::isdisabled()->count();
131
132
            $count['ports']['total']      = Port::notdeleted()->count();
133
            $count['ports']['up']         = Port::with('device')->isup()->count();
134
            $count['ports']['down']       = Port::with('device')->isdown()->count();
135
            $count['ports']['ignored']    = Port::with('device')->isignored()->count();
136
            $count['ports']['disabled']   = Port::with('device')->isdisabled()->count();
137
        }
138
        else
139
        {
140
            $count['devices']['total']    = User::find($request->user()->user_id)->devices()->count();
141
            $count['devices']['up']       = User::find($request->user()->user_id)->devices()->iseup()->count();
142
            $count['devices']['down']     = User::find($request->user()->user_id)->devices()->isdown()->count();
143
            $count['devices']['ignored']  = User::find($request->user()->user_id)->devices()->isignored()->count();
144
            $count['devices']['disabled'] = User::find($request->user()->user_id)->devices()->isdisabled()->count();
145
146
            $count['ports']['total']      = User::find($request->user()->user_id)->ports()->with('device')->count();
147
            $count['ports']['up']         = User::find($request->user()->user_id)->ports()->with('device')->isup()->count();
148
            $count['ports']['down']       = User::find($request->user()->user_id)->ports()->with('device')->isdown()->count();
149
            $count['ports']['ignored']    = User::find($request->user()->user_id)->ports()->with('device')->isignored()->count();
150
            $count['ports']['disabled']   = User::find($request->user()->user_id)->ports()->with('device')->isdisabled()->count();
151
        }
152
        return view('widgets.device-summary', compact(['count', 'type', 'action']));
153
    }
154
155
    /**
156
     * Display the Worldmap widget.
157
     *
158
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
159
     */
160
    public function worldmap(Request $request, $action = null)
161
    {
162
        return view('widgets.worldmap', compact(['action']));
163
    }
164
165
    /**
166
     * Display the notes widget.
167
     *
168
     * @param string $action
169
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
170
     */
171
    public function notes(Settings $settings, Request $request, $action = null)
172
    {
173
        $widget_settings = json_decode(UsersWidgets::getSettings($request)->value('settings'));
0 ignored issues
show
Bug introduced by
The method getSettings() does not exist on App\Models\UsersWidgets. Did you maybe mean scopeGetSettings()?

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...
174
        return view('widgets.notes', compact(['action', 'widget_settings']));
175
    }
176
177
}
178