CategoriesDataTable::query()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\DataTables;
4
5
use Yajra\CMS\Entities\Category;
6
use Yajra\Datatables\Services\DataTable;
7
8
class CategoriesDataTable 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
            ->editColumn('title', function (Category $category) {
20
                return $category->present()->indentedTitle();
21
            })
22
            ->editColumn('lft', '<i class="fa fa-dot-circle-o"></i>')
23
            ->editColumn('status', function (Category $category) {
24
                if ($category->isPublished()) {
25
                    $attr = 'label-danger" title="Unpublished" ><i class="fa fa-remove">';
26
                } else {
27
                    $attr = 'label-success" title="Published" ><i class="fa fa-check">';
28
                }
29
30
                return '<span data-toggle="tooltip" data-placement="right" class="badge ' . $attr . '</i></span>';
31
            })
32
            ->addColumn('action', 'administrator.categories.datatables.action')
33
            ->editColumn('authenticated', function (Category $category) {
34
                return dt_check($category->authenticated);
35
            })
36
            ->addColumn('pub', function (Category $category) {
37
                return $category->countPublished();
38
            })
39
            ->addColumn('unpub', function (Category $category) {
40
                return $category->countUnpublished();
41
            })
42
            ->editColumn('hits', function (Category $category) {
43
                return '<span class="label bg-purple">' . $category->hits . '</span>';
44
            })
45
            ->editColumn('title', function (Category $category) {
46
                return view('administrator.categories.datatables.title', compact('category'))->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
            })
48
            ->rawColumns(['lft', 'status', 'authenticated', 'hits', 'title', 'action'])
49
            ->make(true);
50
    }
51
52
    /**
53
     * Get the query object to be processed by datatables.
54
     *
55
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
56
     */
57
    public function query()
58
    {
59
        $category = Category::select([
60
            'categories.id',
61
            'categories.lft',
62
            \DB::raw('categories.published as status'),
63
            'categories.title',
64
            'categories.depth',
65
            'categories.alias',
66
            'categories.hits',
67
            'categories.authenticated',
68
            'categories.published',
69
            'categories.created_at',
70
            'categories.updated_at',
71
        ])->whereNotNull('parent_id');
72
73
        return $this->applyScopes($category);
74
    }
75
76
    /**
77
     * Optional method if you want to use html builder.
78
     *
79
     * @return \Yajra\Datatables\Html\Builder
80
     */
81 View Code Duplication
    public function html()
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...
82
    {
83
        return $this->builder()
84
                    ->columns($this->getColumns())
85
                    ->ajax('')
86
                    ->addAction(['width' => '110px'])
87
                    ->parameters($this->getBuilderParameters());
88
    }
89
90
    /**
91
     * Get columns.
92
     *
93
     * @return array
94
     */
95
    private function getColumns()
96
    {
97
        return [
98
            'lft'           => [
99
                'width' => '20px',
100
                'title' => '<i class="fa fa-tree" data-toggle="tooltip" data-title="' . trans('cms::categories.datatable.columns.lft') . '"></i>',
101
            ],
102
            'id'            => ['width' => '20px'],
103
            'title',
104
            'alias'         => ['visible' => false],
105
            'status'        => [
106
                'width'      => '20px',
107
                'searchable' => false,
108
                'title'      => '<i class="fa fa-check-circle" data-toggle="tooltip" data-title="' . trans('cms::categories.datatable.columns.status') . '"></i>',
109
            ],
110
            'authenticated' => [
111
                'width' => '20px',
112
                'title' => '<i class="fa fa-key" data-toggle="tooltip" data-title="' . trans('cms::categories.datatable.columns.authenticated') . '"></i>',
113
            ],
114
            'pub'           => [
115
                'width'      => '20px',
116
                'title'      => '<i class="fa fa-check-circle-o" data-toggle="tooltip" data-title="' . trans('cms::categories.datatable.columns.pub') . '"></i>',
117
                'orderable'  => false,
118
                'searchable' => false,
119
            ],
120
            'unpub'         => [
121
                'width'      => '20px',
122
                'title'      => '<i class="fa fa-close" data-toggle="tooltip" data-title="' . trans('cms::categories.datatable.columns.unpub') . '"></i>',
123
                'orderable'  => false,
124
                'searchable' => false,
125
            ],
126
            'hits'          => [
127
                'width' => '20px',
128
                'title' => '<i class="fa fa-eye" data-toggle="tooltip" data-title="' . trans('cms::categories.datatable.columns.hits') . '"></i>',
129
            ],
130
            'created_at'    => ['width' => '100px'],
131
            'updated_at'    => ['width' => '100px'],
132
        ];
133
    }
134
135
    /**
136
     * @return array
137
     */
138 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...
139
    {
140
        return [
141
            'stateSave' => true,
142
            'order'     => [[0, 'asc']],
143
            'buttons'   => [
144
                [
145
                    'extend' => 'create',
146
                    'text'   => '<i class="fa fa-plus"></i>&nbsp;&nbsp;' . trans('cms::categories.datatable.buttons.create'),
147
                ],
148
                'export',
149
                'print',
150
                'reset',
151
                'reload',
152
            ],
153
        ];
154
    }
155
156
    /**
157
     * Get filename for export.
158
     *
159
     * @return string
160
     */
161
    protected function filename()
162
    {
163
        return 'categories_' . time();
164
    }
165
}
166