Test Failed
Pull Request — develop (#200)
by Tony
04:49
created

WidgetDataController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * WidgetDataController.php
4
 *
5
 * Provides data for widgets
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  2017 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Api\Controllers;
27
28
use App\Models\Device;
29
use App\Models\User;
30
use Illuminate\Http\Request;
31
use Settings;
32
33
class WidgetDataController extends Controller
34
{
35
    /**
36
     * Display a listing of the resource.
37
     *
38
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
39
     */
40
    public function index()
41
    {
42
        //
43
    }
44
45
    /**
46
     * Show the form for creating a new resource.
47
     *
48
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
49
     */
50
    public function create()
51
    {
52
        //
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param  \Illuminate\Http\Request $request
59
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
60
     */
61
    public function store(Request $request)
62
    {
63
        //
64
    }
65
66
    /**
67
     * Display the specified resource.
68
     *
69
     * @param Request $request
70
     * @param  int $id
71
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

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...
72
     */
73
    public function show(Request $request, $id)
74
    {
75
        switch ($id) {
76
            case 1:
77
                return $this->availabilityMap($request);
78
79
            default:
80
                return [];
81
        }
82
    }
83
84
    /**
85
     * Show the form for editing the specified resource.
86
     *
87
     * @param  int $id
88
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
89
     */
90
    public function edit($id)
91
    {
92
        //
93
    }
94
95
    /**
96
     * Update the specified resource in storage.
97
     *
98
     * @param  \Illuminate\Http\Request $request
99
     * @param  int $id
100
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
101
     */
102
    public function update(Request $request, $id)
103
    {
104
        //
105
    }
106
107
    /**
108
     * Remove the specified resource from storage.
109
     *
110
     * @param  int $id
111
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Response|null?

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...
112
     */
113
    public function destroy($id)
114
    {
115
        //
116
    }
117
118
119
    private function availabilityMap(Request $request)
120
    {
121
        $uptime = Settings::get('uptime_warning', 84600);
122
        if ($request->user()->hasGlobalRead()) {
123
            $devices = Device::where('ignore', '=', 0)->get();
124
        } else {
125
            $devices = User::find($request->user()->user_id)->devices()->where('ignore', '=', 0)->get();
126
        }
127
        $counts = ['warn' => 0, 'up' => 0, 'down' => 0];
128
        foreach ($devices as $device) {
129
            if ($device->status == 1) {
130
                if (($device->uptime < $uptime) && ($device->uptime != '0')) {
131
                    $counts['warn']++;
132
                } else {
133
                    $counts['up']++;
134
                }
135
            } else {
136
                $counts['down']++;
137
            }
138
        }
139
        return compact(['devices', 'counts']);
140
    }
141
}
142