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

WidgetDataController::eventlog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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\Settings;
33
use App\Models\Device;
34
use App\Models\Port;
35
use App\Models\User;
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\Http\Response|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
     * @param  string|null  $type
0 ignored issues
show
Bug introduced by
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
112
     * @return \Illuminate\Http\Response|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
113
     */
114
    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...
115
    {
116
        $type = $request->route()->getAction()['type'];
117
        if ($request->user()->hasGlobalRead())
118
        {
119
            $count['devices']['total']    = Device::all()->count();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$count was never initialized. Although not strictly required by PHP, it is generally a good practice to add $count = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
120
            $count['devices']['up']       = Device::deviceup()->count();
0 ignored issues
show
Bug introduced by
The method deviceup() does not exist on App\Models\Device. Did you maybe mean scopeDeviceUp()?

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...
121
            $count['devices']['down']     = Device::devicedown()->count();
0 ignored issues
show
Bug introduced by
The method devicedown() does not exist on App\Models\Device. Did you maybe mean scopeDeviceDown()?

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...
122
            $count['devices']['ignored']  = Device::deviceignored()->count();
0 ignored issues
show
Bug introduced by
The method deviceignored() does not exist on App\Models\Device. Did you maybe mean scopeDeviceIgnored()?

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...
123
            $count['devices']['disabled'] = Device::devicedisabled()->count();
0 ignored issues
show
Bug introduced by
The method devicedisabled() does not exist on App\Models\Device. Did you maybe mean scopeDeviceDisabled()?

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...
124
125
            $count['ports']['total']      = Port::portnotdeleted()->count();
0 ignored issues
show
Bug introduced by
The method portnotdeleted() does not exist on App\Models\Port. Did you maybe mean scopePortNotDeleted()?

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...
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();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$count was never initialized. Although not strictly required by PHP, it is generally a good practice to add $count = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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
     * @param  string|null  $type
0 ignored issues
show
Bug introduced by
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
152
     * @return \Illuminate\Http\Response|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
153
     */
154
    public function worldmap(Request $request)
155
    {
156
        return view('widgets.worldmap');
157
    }
158
159
}
160