Completed
Pull Request — master (#133)
by
unknown
05:13
created

SensorDataTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 100
Duplicated Lines 32 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 32
loc 100
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dataTable() 0 15 1
A getColumns() 9 9 1
A filename() 0 3 1
A getBuilderParameters() 19 19 1
A html() 0 6 1
A query() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\DataTables;
4
5
use Yajra\DataTables\Services\DataTable;
6
use App\Sensor;
7
8
class SensorDataTable extends DataTable
9
{
10
    /**
11
     * Build DataTable class.
12
     *
13
     * @return \Yajra\DataTables\Engines\BaseEngine
0 ignored issues
show
Bug introduced by
The type Yajra\DataTables\Engines\BaseEngine was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
     */
15
    public function dataTable($query)
16
    {
17
        return datatables($query)
18
            ->editColumn('name', function($sensor) {
19
                return '<a href="'.route('sensor.show', $sensor->id).'">'.$sensor->name.'</a>';
20
            })
21
            ->editColumn('device_id', function($sensor) {
22
                return '<a href="'.route('device.show', $sensor->device_id).'">'.($sensor->device->name ?? '').'</a>';
23
            })
24
            ->addColumn('value', function($sensor) {
25
                return '<a href="'.route('sensordata.show', $sensor->latest_data->id ?? '0').'">'.($sensor->latest_data->value ?? 'null').'</a>';
0 ignored issues
show
Bug introduced by
It seems like $sensor->latest_data->id ?? '0' can also be of type string; however, parameter $parameters of route() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
                return '<a href="'.route('sensordata.show', /** @scrutinizer ignore-type */ $sensor->latest_data->id ?? '0').'">'.($sensor->latest_data->value ?? 'null').'</a>';
Loading history...
26
            })
27
            ->addColumn('action', 'sensor.action')
28
            ->blacklist([ 'value', 'action' ])
29
            ->rawColumns([ 'device_id', 'name', 'value', 'action' ]);
30
    }
31
32
    /**
33
     * Get the query object to be processed by dataTables.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
36
     */
37
    public function query()
38
    {
39
        $query = Sensor::query();
40
        return $this->applyScopes($query);
41
    }
42
43
    /**
44
     * Optional method if you want to use html builder.
45
     *
46
     * @return \Yajra\DataTables\Html\Builder
47
     */
48
    public function html()
49
    {
50
        return $this->builder()
51
            ->columns($this->getColumns())
52
            ->minifiedAjax()
53
            ->parameters($this->getBuilderParameters());
54
    }
55
56
    /**
57
     * Get columns.
58
     *
59
     * @return array
60
     */
61 View Code Duplication
    protected function getColumns()
62
    {
63
        return [
64
            'id',
65
            'name',
66
            [ 'data' => 'device_id', 'name' => 'device_id', 'title' => 'Device' ],
67
            'type',
68
            'value',
69
            [ 'data' => 'action', 'name' => 'action', 'title' => 'Action', 'searchable' => false, 'orderable' => false, 'exportable' => false, 'printable' => false ]
70
        ];
71
    }
72
73
    /**
74
     * Get builder parameters.
75
     *
76
     * @return array
77
     */
78 View Code Duplication
    protected function getBuilderParameters()
79
    {
80
        return [
81
            'dom'     => 'Bfrtip',
82
            'order'   => [ [ 0, 'desc' ] ],
83
            'buttons' => [
84
                'create',
85
                [ 'extend' => 'collection', 'text' => '<i class="fa fa-file-excel-o"></i> Export', 'buttons' => [ 
86
                    [ 'extend' => 'csv', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
87
                    [ 'extend' => 'excel', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
88
                ] ],
89
                [ 'extend' => 'print', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
90
                'reset',
91
                'reload',
92
            ],
93
            'paging' => true,
94
            'searching' => true,
95
            'info' => true,
96
            'searchDelay' => 500,
97
        ];
98
    }
99
100
    /**
101
     * Get filename for export.
102
     *
103
     * @return string
104
     */
105
    protected function filename()
106
    {
107
        return 'sensor_'.time();
108
    }
109
}
110