Completed
Push — fixes ( 6830e4...798510 )
by Tony
03:34
created

WidgetDataController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 9
Bugs 3 Features 1
Metric Value
wmc 13
c 9
b 3
f 1
lcom 0
cbo 9
dl 0
loc 139
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A eventlog() 0 5 1
A alerts() 0 5 1
A syslog() 0 5 1
B availabilitymap() 0 23 6
B devicesummary() 0 33 2
A worldmap() 0 4 1
A notes() 0 5 1
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 Illuminate\Http\Request;
37
use Settings;
38
39
class WidgetDataController extends Controller
40
{
41
    /**
42
     * Display the eventlog widget.
43
     *
44
     * @param EventlogDataTable $EventlogDataTable
45
     * @param null $action
46
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
47
     */
48
    public function eventlog(EventlogDataTable $EventlogDataTable, $action = null)
49
    {
50
        $tableName = ['id' => 'eventlogDT'];
51
        return $EventlogDataTable->render('widgets.eventlog', compact(['tableName', 'action']));
52
    }
53
54
    /**
55
     * Display the alerts widget.
56
     *
57
     * @param AlertsDataTable $dataTable
58
     * @param null $action
59
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
60
     */
61
    public function alerts(AlertsDataTable $dataTable, $action = null)
62
    {
63
        $tableName = ['id' => 'alertlogDT'];
64
        return $dataTable->render('widgets.alertlog', compact(['tableName', 'action']));
65
    }
66
67
    /**
68
     * Display the syslog widget.
69
     *
70
     * @param SyslogDataTable $dataTable
71
     * @param null $action
72
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
73
     */
74
    public function syslog(SyslogDataTable $dataTable, $action = null)
75
    {
76
        $tableName = ['id' => 'syslogDT'];
77
        return $dataTable->render('widgets.syslog', compact(['tableName', 'action']));
78
    }
79
80
    /**
81
     * Display the availability-map widget.
82
     *
83
     * @param Request $request
84
     * @param string $action
85
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
86
     */
87
    public function availabilitymap(Request $request, $action = null)
88
    {
89
        $uptime = Settings::get('uptime_warning', 84600);
90
        if ($request->user()->hasGlobalRead()) {
91
            $devices = Device::where('ignore', '=', 0)->get();
92
        } else {
93
            $devices = User::find($request->user()->user_id)->devices()->where('ignore', '=', 0)->get();
94
        }
95
        $count = ['warn' => 0, 'up' => 0, 'down' => 0];
96
        foreach ($devices as $device) {
97
            if ($device->status == 1) {
98
                if (($device->uptime < $uptime) && ($device->uptime != '0')) {
99
                    $count['warn']++;
100
                } else {
101
                    $count['up']++;
102
                }
103
            } else {
104
                $count['down']++;
105
            }
106
        }
107
        $widget_settings = json_decode(UsersWidgets::getSettings($request)->value('settings'));
0 ignored issues
show
Bug introduced by
The method value does only exist in Illuminate\Database\Query\Builder, but not in App\Models\UsersWidgets.

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...
108
        return view('widgets.availability-map', compact(['devices', 'uptime', 'count', 'action', 'widget_settings']));
109
    }
110
111
    /**
112
     * Display the device-summary widget.
113
     *
114
     * @param Request $request
115
     * @param null $action
116
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
117
     */
118
    public function devicesummary(Request $request, $action = null)
119
    {
120
        $type = $request->route()->getAction()['type'];
121
        $count = [];
122
        if ($request->user()->hasGlobalRead()) {
123
            $count['devices']['total'] = Device::all()->count();
124
            $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\Device.

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...
125
            $count['devices']['down'] = Device::isDown()->count();
126
            $count['devices']['ignored'] = Device::isIgnored()->count();
127
            $count['devices']['disabled'] = Device::isDisabled()->count();
128
129
            $count['ports']['total'] = Port::notDeleted()->count();
0 ignored issues
show
Bug introduced by
The method count does only exist in Illuminate\Database\Query\Builder, but not in App\Models\Port.

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...
130
            $count['ports']['up'] = Port::isUp()->count();
131
            $count['ports']['down'] = Port::isDown()->count();
132
            $count['ports']['ignored'] = Port::isIgnored()->count();
133
            $count['ports']['disabled'] = Port::isDisabled()->count();
134
        } else {
135
            $user = User::find($request->user()->user_id);
136
137
            $count['devices']['total'] = $user->devices()->count();
138
            $count['devices']['up'] = $user->devices()->isUp()->count();
139
            $count['devices']['down'] = $user->devices()->isDown()->count();
140
            $count['devices']['ignored'] = $user->devices()->isIgnored()->count();
141
            $count['devices']['disabled'] = $user->devices()->isDisabled()->count();
142
143
            $count['ports']['total'] = $user->ports()->count();
144
            $count['ports']['up'] = $user->ports()->isUp()->count();
145
            $count['ports']['down'] = $user->ports()->isDown()->count();
146
            $count['ports']['ignored'] = $user->ports()->isIgnored()->count();
147
            $count['ports']['disabled'] = $user->ports()->isDisabled()->count();
148
        }
149
        return view('widgets.device-summary', compact(['count', 'type', 'action']));
150
    }
151
152
    /**
153
     * Display the Worldmap widget.
154
     *
155
     * @param Request $request
156
     * @param null $action
157
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
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 Request $request
168
     * @param string $action
169
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
170
     */
171
    public function notes(Request $request, $action = null)
172
    {
173
        $widget_settings = json_decode(UsersWidgets::getSettings($request)->value('settings'));
0 ignored issues
show
Bug introduced by
The method value does only exist in Illuminate\Database\Query\Builder, but not in App\Models\UsersWidgets.

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