SensorDataDataTable::getColumns()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 2
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\DataTables;
4
5
use Yajra\DataTables\Services\DataTable;
6
use App\SensorData;
7
8
class SensorDataDataTable 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
            ->addColumn('sensor', function($sensordata) {
19
                return '<a href="'.route('sensor.show', $sensordata->sensor_id).'">'.($sensordata->sensor->name ?? 'null').'</a>';
20
            })
21
            ->addColumn('device', function($sensordata) {
22
                return '<a href="'.route('device.show', $sensordata->sensor->device->id ?? '0').'">'.($sensordata->sensor->device->name ?? 'null').'</a>';
0 ignored issues
show
Bug introduced by
It seems like $sensordata->sensor->device->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

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