MenuItemsDataTable::getColumns()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\DataTables;
4
5
use Yajra\CMS\Entities\Menu;
6
use Yajra\CMS\Entities\Navigation;
7
use Yajra\Datatables\Services\DataTable;
8
9
class MenuItemsDataTable extends DataTable
10
{
11
    /**
12
     * @var \Yajra\CMS\Entities\Navigation
13
     */
14
    protected $navigation;
15
16
    /**
17
     * Display ajax response.
18
     *
19
     * @return \Illuminate\Http\JsonResponse
20
     */
21
    public function ajax()
22
    {
23
        return $this->datatables
24
            ->eloquent($this->query())
25
            ->editColumn('alias', function (Menu $menu) {
26
                return '<span class="label bg-primary">' . $menu->alias . '</span>';
27
            })
28
            ->editColumn('published', function (Menu $menu) {
29
                return dt_check($menu->published);
30
            })
31
            ->editColumn('authenticated', function (Menu $menu) {
32
                return dt_check($menu->authenticated);
33
            })
34
            ->editColumn('title', function (Menu $menu) {
35
                return view('administrator.navigation.menu.datatables.title', compact('menu'))->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...
36
            })
37
            ->editColumn('lft', '<i class="fa fa-dot-circle-o"></i>')
38
            ->addColumn('action', 'administrator.navigation.menu.datatables.action')
39
            ->rawColumns(['alias', 'published', 'authenticated', 'title', 'lft', 'action'])
40
            ->make(true);
41
    }
42
43
    /**
44
     * Get the query object to be processed by datatables.
45
     *
46
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
47
     */
48
    public function query()
49
    {
50
        $users = Menu::query()->where('navigation_id', $this->navigation->id);
51
52
        return $this->applyScopes($users);
53
    }
54
55
    /**
56
     * @param \Yajra\CMS\Entities\Navigation $navigation
57
     * @return $this
58
     */
59
    public function forNavigation(Navigation $navigation)
60
    {
61
        $this->navigation = $navigation;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Optional method if you want to use html builder.
68
     *
69
     * @return \Yajra\Datatables\Html\Builder
70
     */
71 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...
72
    {
73
        return $this->builder()
74
                    ->columns($this->getColumns())
75
                    ->ajax('')
76
                    ->addAction(['width' => '104px', 'className' => 'text-center'])
77
                    ->parameters($this->getBuilderParameters());
78
    }
79
80
    /**
81
     * Get columns.
82
     *
83
     * @return array
84
     */
85
    private function getColumns()
86
    {
87
        return [
88
            'lft'           => [
89
                'width' => '20px',
90
                'title' => '<i class="fa fa-tree" data-toggle="tooltip" data-title="' . trans('cms::menu.datatable.columns.lft') . '"></i>',
91
            ],
92
            'id'            => ['width' => '20px'],
93
            'title',
94
            'url'           => ['visible' => false],
95
            'published'     => [
96
                'width' => '20px',
97
                'title' => '<i class="fa fa-check-circle" data-toggle="tooltip" data-title="' . trans('cms::menu.datatable.columns.published') . '"></i>',
98
            ],
99
            'authenticated' => [
100
                'width' => '20px',
101
                'title' => '<i class="fa fa-key" data-toggle="tooltip" data-title="' . trans('cms::menu.datatable.columns.authenticated') . '"></i>',
102
            ],
103
            'order'         => [
104
                'width' => '20px',
105
                'title' => '<i class="fa fa-list" data-toggle="tooltip" data-title="' . trans('cms::menu.datatable.columns.order') . '"></i>',
106
            ],
107
            'depth'         => [
108
                'width' => '20px',
109
                'title' => '<i class="fa fa-sitemap" data-toggle="tooltip" data-title="' . trans('cms::menu.datatable.columns.depth') . '"></i>',
110
            ],
111
            'created_at'    => ['width' => '100px'],
112
            'updated_at'    => ['width' => '100px'],
113
        ];
114
    }
115
116
    /**
117
     * @return array
118
     */
119 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...
120
    {
121
        return [
122
            'stateSave' => true,
123
            'order'     => [[0, 'asc']],
124
            'buttons'   => [
125
                [
126
                    'extend' => 'create',
127
                    'text'   => '<i class="fa fa-plus"></i>&nbsp;&nbsp;' . trans('cms::menu.datatable.buttons.create'),
128
                ],
129
                'export',
130
                'print',
131
                'reset',
132
                'reload',
133
            ],
134
        ];
135
    }
136
137
    /**
138
     * Get filename for export.
139
     *
140
     * @return string
141
     */
142
    protected function filename()
143
    {
144
        return 'menu_' . time();
145
    }
146
}
147