Completed
Pull Request — master (#82)
by Brandon
02:15
created

SensorDataTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 29.29 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 29
loc 99
ccs 0
cts 28
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dataTable() 0 16 1
A query() 0 5 1
A html() 0 7 1
A getColumns() 11 11 1
A getBuilderParameters() 18 18 1
A filename() 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
14
     */
15
    public function dataTable($query)
16
    {
17
        return datatables($query)
18
            ->editColumn('device_id', function ($sensor) {
19
                return '<a href="/device/' . $sensor->device_id . '">'. ($sensor->device->name ?? '') . '</a>';
20
            })
21
            ->editColumn('name', function ($sensor) {
22
                return '<a href="/sensor/' . $sensor->id . '">'. $sensor->name . '</a>';
23
            })
24
            ->addColumn('value', function ($sensor) {
25
                return $sensor->latest_data->value;
26
            })
27
            ->addColumn('action', 'sensor.action')
28
            ->blacklist([ 'action', 'value'])
29
            ->rawColumns(['device_id', 'name', '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' => 'device_id', 'data' => 'device_id', 'title' => 'Device', 'render' => null, 'searchable' => true, 'orderable' => false, 'exportable' => true, 'printable' => true, 'footer' => '' ],
66
            'name',
67
            'type',
68
            'value',
69
            'action'
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
                'export',
86
                'print',
87
                'reset',
88
                'reload',
89
            ],
90
            'paging' => true,
91
            'searching' => true,
92
            'info' => true,
93
            'searchDelay' => 500,
94
        ];
95
    }
96
97
    /**
98
     * Get filename for export.
99
     *
100
     * @return string
101
     */
102
    protected function filename()
103
    {
104
        return 'sensor_'.time();
105
    }
106
}
107