Completed
Pull Request — develop (#98)
by Neil
22:06
created

AlertsDataTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 55%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 2
cbo 6
dl 0
loc 112
ccs 22
cts 40
cp 0.55
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B ajax() 0 25 1
A query() 0 5 1
A html() 0 8 1
A getColumns() 0 19 1
A filename() 0 4 1
A getBuilderParameters() 0 10 1
1
<?php
2
/*
3
 * Copyright (C) 2016 Neil Lathwood <[email protected]>
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
 
18
namespace App\DataTables;
19
20
use App\Models\Alerting\Alerts;
21
use Yajra\Datatables\Services\DataTable;
22
23
class AlertsDataTable extends DataTable
24
{
25
    // 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...
26
27
    /**
28
     * Display ajax response.
29
     *
30
     * @return \Illuminate\Http\JsonResponse
31
     */
32
    public function ajax()
33
    {
34
        return $this->datatables
35
            ->eloquent($this->query())
36
            ->editColumn('state', '@if ($state == 0)
37
                                        <div class="label label-success">SUCCESS</div>
38
                                    @elseif ($state == 1)
39
                                        <div class="label label-danger">FAILED</div>
40
                                    @elseif ($state == 2)
41
                                        <div class="label label-warning">MUTED</div>
42
                                    @else
43
                                        <div class="label label-primary">UNKNOWN</div>
44
                                    @endif')
45
            ->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...
46
                return '<a href="' . url("alerting/rules/".$this['rule']['id']) . '">' . $this['rule']['name'] . '</a>';
47
            })
48
            ->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...
49
                return '<a href="' . url("devices/".$this['device']['device_id']) . '">' . $this['device']['hostname'] . '</a>';
50
            })
51
            ->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...
52
                return '';
53
            })
54
            ->make(true);
55
            //            ->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...
56
    }
57
58
    /**
59
     * Get the query object to be processed by datatables.
60
     *
61
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
62
     */
63
    public function query()
64
    {
65
        $alerts = Alerts::query()->where('state', '!=', '0')->with('device')->with('user')->with('rule')->select('alerts.*');
66
        return $this->applyScopes($alerts);
67
    }
68
69
    /**
70
     * Optional method if you want to use html builder.
71
     *
72
     * @return \Yajra\Datatables\Html\Builder
73
     */
74 1
    public function html()
75
    {
76 1
        return $this->builder()
77 1
                    ->columns($this->getColumns())
78 1
                    ->parameters($this->getBuilderParameters());
79
                    //                    ->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...
80
                    //                    ->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...
81
    }
82
83
    /**
84
     * Get columns.
85
     *
86
     * @return array
87
     */
88 1
    private function getColumns()
89
    {
90
        return [
91
            'state'     => [
92 1
                'title' => trans('alerting.alerts.text.state'),
93 1
            ],
94
            'rule.name' => [
95 1
                'title' => trans('alerting.alerts.text.rule'),
96 1
            ],
97
            'device.hostname' => [
98 1
                'title'       => trans('devices.label.hostname'),
99 1
            ],
100 1
            'timestamp',
101
            'rule.severity' => [
102 1
                'title'     => trans('alerting.alerts.text.severity'),
103 1
            ],
104 1
            'actions',
105 1
        ];
106
    }
107
108
    /**
109
     * Get filename for export.
110
     *
111
     * @return string
112
     */
113
    protected function filename()
114
    {
115
        return 'alerts';
116
    }
117
118
    /**
119
     * Get Builder Params
120
     *
121
     * @return array
122
     */
123 1
    protected function getBuilderParameters()
124
    {
125
        return [
126 1
            'dom' => 'Blfrtip',
127 1
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
128
            'buttons' => [
129 1
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
130 1
            ],
131 1
        ];
132
    }
133
134
}
135