Completed
Push — master ( 8ba09e...3ad61c )
by
unknown
28s
created

ActivityLogDataTable   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 133
Duplicated Lines 15.04 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 17
c 3
b 1
f 0
lcom 1
cbo 4
dl 20
loc 133
ccs 0
cts 51
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B dataTable() 0 28 4
B getRouteFromType() 0 20 8
A query() 0 5 1
A html() 0 7 1
A getColumns() 0 10 1
A getBuilderParameters() 20 20 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 App\User;
6
use Illuminate\Support\Facades\Auth;
7
use Yajra\DataTables\Services\DataTable;
8
use Spatie\Activitylog\Models\Activity;
9
10
class ActivityLogDataTable extends DataTable
11
{
12
    /**
13
     * Build DataTable class.
14
     *
15
     * @return \Yajra\DataTables\DataTableAbstract
16
     */
17
    public function dataTable($query)
18
    {
19
        return datatables($query)
20
            ->editColumn('causer_id', function ($activity) {
21
                if ($activity->causer_id) {
22
                    return '<a href="' . route($this->getRouteFromType($activity->causer_type), $activity->causer_id) . '">' . $activity->causer->name ?? '' . '</a>';
23
                } else {
24
                    return '';
25
                }
26
            })
27
            ->editColumn('subject_id', function ($activity) {
28
                if ($activity->subject_id) {
29
                    return '<a href="' . route($this->getRouteFromType($activity->subject_type), $activity->subject_id) . '">' . $activity->subject->name ?? '' . '</a>';
30
                } else {
31
                    return '';
32
                }
33
            })
34
            ->editColumn('properties', function ($activity) {
35
                return $activity->properties;
36
            })
37
            ->editColumn('created_at', function ($activity) {
38
                if ($activity->created_at->diffInDays() > 0)
39
                    return $activity->created_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a');
0 ignored issues
show
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
40
                else
41
                    return $activity->created_at->diffForHumans();
42
            })
43
            ->rawColumns(['causer_id', 'subject_id', 'properties']);
44
    }
45
    
46
    public function getRouteFromType($type) {
47
        switch ($type) {
48
            case "App\Device":
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
49
                return 'device.show';
50
            case "App\Deviceimage":
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
51
                return 'image.device';
52
            case "App\Location":
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53
                return 'location.show';
54
            case "App\Sensor":
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
55
                return 'sensor.show';
56
            case "App\SensorData":
57
                return 'sensordata.show';
58
            case "App\Site":
59
                return 'site.show';
60
            case "App\User":
61
                return 'user.show';
62
            default:
63
                return 'logs';
64
        }
65
    }
66
67
    /**
68
     * Get query source of dataTable.
69
     *
70
     * @return \Illuminate\Database\Eloquent\Builder
71
     */
72
    public function query()
73
    {
74
        $query = Activity::query();
75
        return $this->applyScopes($query);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->applyScopes($query); of type Illuminate\Database\Quer...tabase\Eloquent\Builder adds the type Illuminate\Database\Query\Builder to the return on line 75 which is incompatible with the return type documented by App\DataTables\ActivityLogDataTable::query of type Illuminate\Database\Eloquent\Builder.
Loading history...
76
    }
77
78
    /**
79
     * Optional method if you want to use html builder.
80
     *
81
     * @return \Yajra\DataTables\Html\Builder
82
     */
83
    public function html()
84
    {
85
        return $this->builder()
86
            ->columns($this->getColumns())
87
            ->minifiedAjax()
88
            ->parameters($this->getBuilderParameters());
89
    }
90
91
    /**
92
     * Get columns.
93
     *
94
     * @return array
95
     */
96
    protected function getColumns()
97
    {
98
        return [
99
            'created_at',
100
            [ 'name' => 'causer_id', 'data' => 'causer_id', 'title' => 'Actor', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ],
101
            [ 'name' => 'description', 'data' => 'description', 'title' => 'Action', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ],
102
            [ 'name' => 'subject_id', 'data' => 'subject_id', 'title' => 'Subject', 'render' => null, 'searchable' => true, 'orderable' => true, 'exportable' => true, 'printable' => true, 'footer' => '' ],
103
            [ 'name' => 'properties', 'data' => 'properties', 'title' => 'Changes', 'render' => null, 'searchable' => true, 'orderable' => false, 'exportable' => true, 'printable' => true, 'footer' => '' ]
104
        ];
105
    }
106
    
107
    /**
108
     * Get builder parameters.
109
     *
110
     * @return array
111
     */
112 View Code Duplication
    protected function getBuilderParameters()
113
    {
114
        return [
115
            'dom'     => 'Bfrtip',
116
            'order'   => [ [ 0, 'desc' ] ],
117
            'buttons' => [
118
                [ 'extend' => 'collection', 'text' => '<i class="fa fa-file-excel-o"></i> Export', 'buttons' => [ 
119
                    [ 'extend' => 'csv', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
120
                    [ 'extend' => 'excel', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
121
                ] ],
122
                [ 'extend' => 'print', 'exportOptions' => [ 'modifier' => [ 'search' => true ] ] ],
123
                'reset',
124
                'reload',
125
            ],
126
            'paging' => true,
127
            'searching' => true,
128
            'info' => true,
129
            'searchDelay' => 500,
130
        ];
131
    }
132
133
    /**
134
     * Get filename for export.
135
     *
136
     * @return string
137
     */
138
    protected function filename()
139
    {
140
        return 'activitylog_' . time();
141
    }
142
}
143