Completed
Pull Request — develop (#121)
by Neil
03:13
created

WidgetDataController::syslog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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\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 Availability_Map(Settings $settings, Request $request)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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 Device_Summary(Request $request)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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::deviceup()->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...
Bug introduced by
The call to DeviceUp() misses a required argument $query.

This check looks for function calls that miss required arguments.

Loading history...
121
            $count['devices']['down']     = Device::devicedown()->count();
0 ignored issues
show
Bug introduced by
The call to DeviceDown() misses a required argument $query.

This check looks for function calls that miss required arguments.

Loading history...
122
            $count['devices']['ignored']  = Device::deviceignored()->count();
0 ignored issues
show
Bug introduced by
The call to DeviceIgnored() misses a required argument $query.

This check looks for function calls that miss required arguments.

Loading history...
123
            $count['devices']['disabled'] = Device::devicedisabled()->count();
0 ignored issues
show
Bug introduced by
The call to DeviceDisabled() misses a required argument $query.

This check looks for function calls that miss required arguments.

Loading history...
124
125
            $count['ports']['total']      = Port::portnotdeleted()->count();
0 ignored issues
show
Bug introduced by
The call to PortNotDeleted() misses a required argument $query.

This check looks for function calls that miss required arguments.

Loading history...
126
            $count['ports']['up']         = Port::with('device')->portup()->count();
127
            $count['ports']['down']       = Port::with('device')->portdown()->count();
128
            $count['ports']['ignored']    = Port::with('device')->portignored()->count();
129
            $count['ports']['disabled']   = Port::with('device')->portdisabled()->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()->deviceup()->count();
135
            $count['devices']['down']     = User::find($request->user()->user_id)->devices()->devicedown()->count();
136
            $count['devices']['ignored']  = User::find($request->user()->user_id)->devices()->deviceignored()->count();
137
            $count['devices']['disabled'] = User::find($request->user()->user_id)->devices()->devicedisabled()->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')->portup()->count();
141
            $count['ports']['down']       = User::find($request->user()->user_id)->ports()->with('device')->portdown()->count();
142
            $count['ports']['ignored']    = User::find($request->user()->user_id)->ports()->with('device')->portignored()->count();
143
            $count['ports']['disabled']   = User::find($request->user()->user_id)->ports()->with('device')->portdisabled()->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