Completed
Push — master ( c6ea24...dc6eef )
by Arjay
24:50 queued 10:05
created

ArticlesDataTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 168
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 16
loc 168
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
B ajax() 0 28 1
A query() 0 7 1
A html() 0 7 1
A getColumns() 0 70 1
A getBuilderParameters() 16 16 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 Yajra\CMS\DataTables;
4
5
use Yajra\CMS\Entities\Article;
6
use Yajra\Datatables\Services\DataTable;
7
8
class ArticlesDataTable extends DataTable
9
{
10
    /**
11
     * Display ajax response.
12
     *
13
     * @return \Illuminate\Http\JsonResponse
14
     */
15
    public function ajax()
16
    {
17
        return $this->datatables
18
            ->eloquent($this->query())
19
            ->addColumn('action', 'administrator.articles.datatables.action')
20
            ->editColumn('published', function (Article $article) {
21
                return dt_check($article->published);
22
            })
23
            ->editColumn('authenticated', function (Article $article) {
24
                return dt_check($article->authenticated);
25
            })
26
            ->editColumn('is_page', function (Article $article) {
27
                return dt_check($article->is_page);
28
            })
29
            ->editColumn('hits', function (Article $article) {
30
                return '<span class="label bg-purple">' . $article->hits . '</span>';
31
            })
32
            ->editColumn('title', function (Article $article) {
33
                return view('administrator.articles.datatables.title', compact('article'))->render();
34
            })
35
            ->addColumn('plain_title', function (Article $article) {
36
                return $article->title;
37
            })
38
            ->addColumn('slug', function (Article $article) {
39
                return $article->present()->slug;
40
            })
41
            ->make(true);
42
    }
43
44
    /**
45
     * Get the query object to be processed by datatables.
46
     *
47
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
48
     */
49
    public function query()
50
    {
51
        $articles = Article::select('articles.*', 'categories.title as category_title')
52
                           ->join('categories', 'categories.id', '=', 'articles.category_id');
53
54
        return $this->applyScopes($articles);
55
    }
56
57
    /**
58
     * Optional method if you want to use html builder.
59
     *
60
     * @return \Yajra\Datatables\Html\Builder
61
     */
62
    public function html()
63
    {
64
        return $this->builder()
65
                    ->columns($this->getColumns())
66
                    ->ajax('')
67
                    ->parameters($this->getBuilderParameters());
68
    }
69
70
    /**
71
     * Get columns.
72
     *
73
     * @return array
74
     */
75
    private function getColumns()
76
    {
77
        return [
78
            [
79
                'data'  => 'id',
80
                'name'  => 'articles.id',
81
                'title' => trans('cms::article.datatable.columns.id'),
82
                'width' => '20px',
83
            ],
84
            [
85
                'data'  => 'title',
86
                'name'  => 'articles.title',
87
                'title' => trans('cms::article.datatable.columns.title'),
88
            ],
89
            [
90
                'data'    => 'alias',
91
                'name'    => 'articles.alias',
92
                'visible' => false,
93
            ],
94
            [
95
                'data'    => 'categories.title',
96
                'title'   => trans('cms::article.datatable.columns.categories.title'),
97
                'visible' => false,
98
                'data'    => 'category_title',
99
            ],
100
            [
101
                'data'  => 'published',
102
                'name'  => 'articles.published',
103
                'width' => '20px',
104
                'title' => '<i class="fa fa-check-circle" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.published') . '"></i>',
105
            ],
106
            [
107
                'data'  => 'authenticated',
108
                'name'  => 'articles.authenticated',
109
                'width' => '20px',
110
                'title' => '<i class="fa fa-key" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.authenticated') . '"></i>',
111
            ],
112
            [
113
                'data'  => 'order',
114
                'name'  => 'articles.order',
115
                'width' => '20px',
116
                'title' => '<i class="fa fa-list" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.order') . '"></i>',
117
            ],
118
            [
119
                'data'  => 'hits',
120
                'name'  => 'articles.hits',
121
                'width' => '20px',
122
                'title' => '<i class="fa fa-eye" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.hits') . '"></i>',
123
            ],
124
            [
125
                'data'  => 'is_page',
126
                'name'  => 'articles.is_page',
127
                'title' => trans('cms::article.datatable.columns.is_page'),
128
            ],
129
            [
130
                'data'  => 'updated_at',
131
                'name'  => 'articles.updated_at',
132
                'title' => trans('cms::article.datatable.columns.updated_at'),
133
                'width' => '100px',
134
            ],
135
            [
136
                'data'       => 'action',
137
                'title'      => trans('cms::article.datatable.columns.action'),
138
                'width'      => '134px',
139
                'searchable' => false,
140
                'orderable'  => false,
141
                'className'  => 'text-center',
142
            ],
143
        ];
144
    }
145
146
    /**
147
     * @return array
148
     */
149 View Code Duplication
    protected function getBuilderParameters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        return [
152
            'stateSave' => true,
153
            'buttons'   => [
154
                [
155
                    'extend' => 'create',
156
                    'text'   => '<i class="fa fa-plus"></i>&nbsp;&nbsp;' . trans('cms::article.datatable.buttons.create'),
157
                ],
158
                'export',
159
                'print',
160
                'reset',
161
                'reload',
162
            ],
163
        ];
164
    }
165
166
    /**
167
     * Get filename for export.
168
     *
169
     * @return string
170
     */
171
    protected function filename()
172
    {
173
        return 'articles';
174
    }
175
}
176