Completed
Pull Request — develop (#98)
by Neil
03:54
created

AlertsDataTable::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
/**
3
 * app/DataTables/AlertsDataTable.php
4
 *
5
 * Datatable for alerts
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\Alerting\Alert;
29
use Yajra\Datatables\Services\DataTable;
30
31
class AlertsDataTable extends DataTable
32
{
33
    // protected $printPreview  = 'path.to.print.preview.view';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
35
    /**
36
     * Display ajax response.
37
     *
38
     * @return \Illuminate\Http\JsonResponse
39
     */
40
    public function ajax()
41
    {
42
        return $this->datatables
43
            ->eloquent($this->query())
44
            ->editColumn('state', '@if ($state == 0)
45
                                        <div class="label label-success">SUCCESS</div>
46
                                    @elseif ($state == 1)
47
                                        <div class="label label-danger">FAILED</div>
48
                                    @elseif ($state == 2)
49
                                        <div class="label label-warning">MUTED</div>
50
                                    @else
51
                                        <div class="label label-primary">UNKNOWN</div>
52
                                    @endif')
53
            ->editColumn('rule.name', function ($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { r...e']['name'] . '</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...
54
                return '<a href="' . url("alerting/rules/".$this['rule']['id']) . '">' . $this['rule']['name'] . '</a>';
55
            })
56
            ->editColumn('device.hostname', function ($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { r...'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...
57
                return '<a href="' . url("devices/".$this['device']['device_id']) . '">' . $this['device']['hostname'] . '</a>';
58
            })
59
            ->addColumn('actions', function ($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { return ''; } 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...
Unused Code introduced by
The parameter $this is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
                return '';
61
            })
62
            ->make(true);
63
            //            ->addColumn('action', 'path.to.action.view')
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
    }
65
66
    /**
67
     * Get the query object to be processed by datatables.
68
     *
69
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
70
     */
71
    public function query()
72
    {
73
        $alerts = Alert::query()->where('state', '!=', '0')->with('device')->with('user')->with('rule')->select('alerts.*');
74
        return $this->applyScopes($alerts);
75
    }
76
77
    /**
78
     * Optional method if you want to use html builder.
79
     *
80
     * @return \Yajra\Datatables\Html\Builder
81
     */
82
    public function html()
83
    {
84
        return $this->builder()
85
                    ->columns($this->getColumns())
86
                    ->parameters($this->getBuilderParameters());
87
                    //                    ->ajax('')
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
                    //                    ->addAction(['width' => '80px'])
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
    }
90
91
    /**
92
     * Get columns.
93
     *
94
     * @return array
95
     */
96
    private function getColumns()
97
    {
98
        return [
99
            'state'     => [
100
                'title' => trans('alerting.alerts.text.state'),
101
            ],
102
            'rule.name' => [
103
                'title' => trans('alerting.alerts.text.rule'),
104
            ],
105
            'device.hostname' => [
106
                'title'       => trans('devices.label.hostname'),
107
            ],
108
            'timestamp',
109
            'rule.severity' => [
110
                'title'     => trans('alerting.alerts.text.severity'),
111
            ],
112
            'actions',
113
        ];
114
    }
115
116
    /**
117
     * Get filename for export.
118
     *
119
     * @return string
120
     */
121
    protected function filename()
122
    {
123
        return 'alerts';
124
    }
125
126
    /**
127
     * Get Builder Params
128
     *
129
     * @return array
130
     */
131
    protected function getBuilderParameters()
132
    {
133
        return [
134
            'dom' => 'Blfrtip',
135
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
136
            'buttons' => [
137
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
138
            ],
139
        ];
140
    }
141
142
}
143