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

DevicesDataTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 17.65 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 3
dl 18
loc 102
ccs 0
cts 30
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A filename() 0 4 1
A dataTable() 0 18 1
A query() 0 5 1
A html() 0 7 1
A getColumns() 0 12 1
A getBuilderParameters() 18 18 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 App\Device;
6
use App\Site;
7
use Yajra\DataTables\Services\DataTable;
8
9
class DevicesDataTable extends DataTable
10
{
11
    /**
12
     * Build DataTable class.
13
     *
14
     * @return \Yajra\DataTables\Engines\BaseEngine
15
     */
16
    public function dataTable($query)
17
    {
18
        return datatables($query)
19
            ->editColumn('name', function ($device) {
20
                return '<a href="' . route('device.show', $device->id) . '">' . $device->name . '</a>';
21
            })
22
            ->addColumn('location', function ($device) {
23
                return ($device->location->name ?? 'null');
24
            })
25
            ->addColumn('site', function ($device) {
26
                return ($device->location->site->name ?? 'null');
27
            })
28
            ->addColumn('rates', function ($device) {
29
                return $device->update_rate . '/' . $device->image_rate .'/' . $device->sensor_rate;
30
            })
31
            ->blacklist([ 'location', 'site', 'rates' ])
32
            ->rawColumns([ 'name' ]);
33
    }
34
35
    /**
36
     * Get the query object to be processed by dataTables.
37
     *
38
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
39
     */
40
    public function query()
41
    {
42
        $query = Device::query();
43
        return $this->applyScopes($query);
44
    }
45
46
    /**
47
     * Optional method if you want to use html builder.
48
     *
49
     * @return \Yajra\DataTables\Html\Builder
50
     */
51
    public function html()
52
    {
53
        return $this->builder()
54
            ->columns($this->getColumns())
55
            ->minifiedAjax()
56
            ->parameters($this->getBuilderParameters());
57
    }
58
59
    /**
60
     * Get columns.
61
     *
62
     * @return array
63
     */
64
    protected function getColumns()
65
    {
66
        return [
67
            'id',
68
            'name',
69
            'location',
70
            'site',
71
            'open_time',
72
            'close_time',
73
            'rates'
74
        ];
75
    }
76
77
    /**
78
     * Get builder parameters.
79
     *
80
     * @return array
81
     */
82 View Code Duplication
    protected function getBuilderParameters()
83
    {
84
        return [
85
            'dom'     => 'Bfrtip',
86
            'order'   => [ [ 0, 'asc' ] ],
87
            'buttons' => [
88
                'create',
89
                'export',
90
                'print',
91
                'reset',
92
                'reload',
93
            ],
94
            'paging' => true,
95
            'searching' => true,
96
            'info' => true,
97
            'searchDelay' => 500,
98
        ];
99
    }
100
101
    /**
102
     * Get filename for export.
103
     *
104
     * @return string
105
     */
106
    protected function filename()
107
    {
108
        return 'devices_'.time();
109
    }
110
}
111