Test Failed
Push — develop ( 55b248...7d9761 )
by Tony
21:25
created

WidgetDataController::graph()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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 \Illuminate\Http\Request $request
46
     * @param null $action
47
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
48
     */
49
    public function eventlog(EventlogDataTable $EventlogDataTable, Request $request, $action = null)
50
    {
51
        $tableName = ['id' => 'eventlogDT'];
52
        if ($request->device_id) {
53
            return $EventlogDataTable->forDevice($request->device_id)
54
                ->render('widgets.eventlog', compact(['tableName', 'action']));
55
        } else {
56
            return $EventlogDataTable->render('widgets.eventlog', compact(['tableName', 'action']));
57
        }
58
    }
59
60
    /**
61
     * Display the alerts widget.
62
     *
63
     * @param AlertsDataTable $dataTable
0 ignored issues
show
Documentation introduced by
There is no parameter named $dataTable. Did you maybe mean $AlertsDataTable?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
64
     * @param null $action
65
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
66
     */
67
    public function alerts(AlertsDataTable $AlertsDataTable, $action = null)
68
    {
69
        $tableName = ['id' => 'alertlogDT'];
70
        return $AlertsDataTable->render('widgets.alertlog', compact(['tableName', 'action']));
71
    }
72
73
    /**
74
     * Display the syslog widget.
75
     *
76
     * @param SyslogDataTable $dataTable
77
     * @param null $action
78
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
79
     */
80
    public function syslog(SyslogDataTable $dataTable, $action = null)
81
    {
82
        $tableName = ['id' => 'syslogDT'];
83
        return $dataTable->render('widgets.syslog', compact(['tableName', 'action']));
84
    }
85
86
    /**
87
     * Display the availability-map widget.
88
     *
89
     * @param Request $request
90
     * @param string $action
91
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
92
     */
93
    public function availabilitymap(Request $request, $action = null)
94
    {
95
        $uptime = Settings::get('uptime_warning', 84600);
96
        if ($request->user()->hasGlobalRead()) {
97
            $devices = Device::where('ignore', '=', 0)->get();
98
        } else {
99
            $devices = User::find($request->user()->user_id)->devices()->where('ignore', '=', 0)->get();
100
        }
101
        $count = ['warn' => 0, 'up' => 0, 'down' => 0];
102
        foreach ($devices as $device) {
103
            if ($device->status == 1) {
104
                if (($device->uptime < $uptime) && ($device->uptime != '0')) {
105
                    $count['warn']++;
106
                } else {
107
                    $count['up']++;
108
                }
109
            } else {
110
                $count['down']++;
111
            }
112
        }
113
        $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...
114
        return view('widgets.availability-map', compact(['devices', 'uptime', 'count', 'action', 'widget_settings']));
115
    }
116
117
    /**
118
     * Display the device-summary widget.
119
     *
120
     * @param Request $request
121
     * @param null $action
122
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
123
     */
124
    public function devicesummary(Request $request, $action = null)
125
    {
126
        $type = $request->route()->getAction()['type'];
127
        $count = [];
128
        if ($request->user()->hasGlobalRead()) {
129
            $count['devices']['total'] = Device::all()->count();
130
            $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...
131
            $count['devices']['down'] = Device::isDown()->count();
132
            $count['devices']['ignored'] = Device::isIgnored()->count();
133
            $count['devices']['disabled'] = Device::isDisabled()->count();
134
135
            $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...
136
            $count['ports']['up'] = Port::isUp()->count();
137
            $count['ports']['down'] = Port::isDown()->count();
138
            $count['ports']['ignored'] = Port::isIgnored()->count();
139
            $count['ports']['disabled'] = Port::isDisabled()->count();
140
        } else {
141
            $user = User::find($request->user()->user_id);
142
143
            $count['devices']['total'] = $user->devices()->count();
144
            $count['devices']['up'] = $user->devices()->isUp()->count();
145
            $count['devices']['down'] = $user->devices()->isDown()->count();
146
            $count['devices']['ignored'] = $user->devices()->isIgnored()->count();
147
            $count['devices']['disabled'] = $user->devices()->isDisabled()->count();
148
149
            $count['ports']['total'] = $user->ports()->count();
150
            $count['ports']['up'] = $user->ports()->isUp()->count();
151
            $count['ports']['down'] = $user->ports()->isDown()->count();
152
            $count['ports']['ignored'] = $user->ports()->isIgnored()->count();
153
            $count['ports']['disabled'] = $user->ports()->isDisabled()->count();
154
        }
155
        return view('widgets.device-summary', compact(['count', 'type', 'action']));
156
    }
157
158
    /**
159
     * Display the Worldmap widget.
160
     *
161
     * @param Request $request
162
     * @param null $action
163
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
164
     */
165
    public function worldmap(Request $request, $action = null)
166
    {
167
        return view('widgets.worldmap', compact(['action']));
168
    }
169
170
    /**
171
     * Display the notes widget.
172
     *
173
     * @param Request $request
174
     * @param string $action
175
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
176
     */
177
    public function notes(Request $request, $action = null)
178
    {
179
        $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...
180
        return view('widgets.notes', compact(['action', 'widget_settings']));
181
    }
182
183
    /**
184
     * Display the graph widget.
185
     *
186
     * @param string $action
187
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
188
     */
189
    public function graph(Settings $settings, Request $request, $action = null)
190
    {
191
        $div_id = mt_rand();
192
        $request->params = '{"content-type": "text/plain", "data-source": "rrd-csv"}';
193
        $params = json_decode($request->params);
194
        $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...
Unused Code introduced by
$widget_settings is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
195
        return view('widgets.graph', compact(['action', 'params', 'div_id']));
196
    }
197
198
}
199