Completed
Push — settings-ui ( dc66cd...0a0011 )
by Tony
03:22
created

DeviceDataTable::ajax()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
rs 8.8571
cc 3
eloc 25
nc 1
nop 0
1
<?php
2
/**
3
 * app/DataTables/DeviceDataTable.php
4
 *
5
 * Datatable for devices
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\DataTables;
27
28
use App\Models\Device;
29
30
class DeviceDataTable extends BaseDataTable
31
{
32
    /**
33
     * Display ajax response.
34
     *
35
     * @return \Illuminate\Http\JsonResponse
36
     */
37
    public function ajax()
38
    {
39
        return $this->datatables
40
            ->eloquent($this->query())
41
            ->editColumn('status_reason', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ...ue">up</span>'; } } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
                if ($device->status == 0)
43
                {
44
                    return '<span data-toggle="tooltip" title="down" class="badge bg-red">'.$device->status_reason.'</span>';
45
                }
46
                else
47
                {
48
                    return '<span data-toggle="tooltip" title="up" class="badge bg-light-blue">up</span>';
49
                }
50
            })
51
            ->editColumn('vendor', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { .... $device->os . '">'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
                return '<img src="'.$device->logo().'" border="0" alt="'.$device->os.'">';
53
            })
54
            ->editColumn('hostname', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { .... $hostname . '</a>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
                $hostname = is_null($device) ? trans('devices.text.deleted') : $device->hostname;
56
                return '<a href="'.url("devices/".$device->device_id).'">'.$hostname.'</a>';
57
            })
58
            ->editColumn('resources', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ...$sensors . '</span>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
                $ports   = $device->ports()->count();
60
                $sensors = $device->sensors()->count();
61
                return '<span data-toggle="tooltip" title="'.$ports.' Ports" class="badge bg-light-blue"><i class="fa fa-link"></i>&nbsp; '.$ports.'</span><br />
62
                        <span data-toggle="tooltip" title="'.$sensors.' Sensors" class="badge bg-light-blue"><i class="fa fa-dashboard"></i>&nbsp; '.$sensors.'</span>';
63
            })
64
            ->editColumn('hardware', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ... . $device->features; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
                return $device->hardware.'<br />'.$device->features;
66
            })
67
            ->editColumn('os', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ...' . $device->version; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
                return ucfirst($device->os).'<br />'.$device->version;
69
            })
70
            ->editColumn('location', function($device) {
0 ignored issues
show
Documentation introduced by
function ($device) { ... . $device->location; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
                return $device->formatUptime($device->uptime).'<br />'.$device->location;
72
            })
73
            ->make(true);
74
    }
75
76
    /**
77
     * Get the query object to be processed by datatables.
78
     *
79
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
80
     */
81
    public function query()
82
    {
83
        $device = Device::select('devices.*');
84
        return $this->applyScopes($device);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->applyScopes($device); of type Illuminate\Database\Quer...tabase\Eloquent\Builder adds the type Illuminate\Database\Query\Builder to the return on line 84 which is incompatible with the return type declared by the interface Yajra\Datatables\Contrac...ataTableContract::query of type Illuminate\Database\Eloquent\Builder.
Loading history...
85
    }
86
87
    /**
88
     * Get columns.
89
     *
90
     * @return array
91
     */
92
    public function getColumns()
93
    {
94
        return [
95
            'status'    => [
96
                'title' => trans('general.text.status'),
97
                'data'  => 'status_reason',
98
                'width' => '40px',
99
            ],
100
            'vendor'        => [
101
                'title' => trans('devices.text.vendor'),
102
                'width' => '20px',
103
            ],
104
            'hostname'  => [
105
                'title' => trans('devices.label.hostname'),
106
            ],
107
            'resources'  => [
108
                'title'  => '',
109
                'search' => false,
110
            ],
111
            'hardware'  => [
112
                'title' => trans('devices.text.platform'),
113
            ],
114
            'features'    => [
115
                'visible' => false,
116
            ],
117
            'os'        => [
118
                'title' => trans('devices.text.os'),
119
            ],
120
            'version'     => [
121
                'visible' => false,
122
            ],
123
            'location'  => [
124
                'title' => trans('devices.text.uptime_location'),
125
            ],
126
        ];
127
    }
128
129
    /**
130
     * Get filename for export.
131
     *
132
     * @return string
133
     */
134
    protected function filename()
135
    {
136
        return 'devices';
137
    }
138
}
139