UserDataTable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dataTable() 0 34 2
A query() 0 4 1
A getColumns() 0 16 1
1
<?php
2
3
namespace Microboard\DataTables;
4
5
use App\User;
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Builder;
8
use Microboard\Traits\DataTable as MicroboardDataTable;
9
use Yajra\DataTables\DataTableAbstract;
10
use Yajra\DataTables\Html\Column;
11
use Yajra\DataTables\Services\DataTable;
12
13
class UserDataTable extends DataTable
14
{
15
    use MicroboardDataTable;
16
17
    /**
18
     * Build DataTable class.
19
     *
20
     * @param mixed $query Results from query() method.
21
     * @return DataTableAbstract
22
     */
23
    public function dataTable($query)
24
    {
25
        return $this->build($query)
26
            ->filterColumn('role_id', function(Builder $query, $keyword) {
27
                return $query->whereHas('role', function(Builder $query) use($keyword) {
28
                    return $query->where('name', 'LIKE', "%{$keyword}%")
29
                        ->orWhere('display_name', 'LIKE', "%{$keyword}%");
30
                });
31
            })
32
            ->editColumn('role_id', function (User $user) {
33
                if (auth()->user()->can('view', $user->role)) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\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...
34
                    $route = route('microboard.roles.show', $user->role);
35
                    return "<a href='{$route}'>{$user->role->display_name}</a>";
36
                }
37
38
                return $user->role->display_name;
39
            })
40
            ->editColumn('name', function (User $user) {
41
                return '<div class="media align-items-center">' .
42
                    '<span class="avatar avatar-sm rounded-circle mr-3">' .
43
                    '<img alt="' . $user->name . '" src="' . $user->avatar . '" width="36" height="36"></span>' .
44
                    '<div class="media-body">' .
45
                    '<span class="mb-0 d-block">' . $user->name . '</span>' .
46
                    '<small class="mb-0" style="font-size: 10px;"><i class="fa fa-clock"></i> ' .
47
                    '<time datetime="' . $user->updated_at . '">' .
48
                    trans('microboard::users.fields.updated_at') . ' ' .
49
                    $user->updated_at->diffForHumans() . '</time></small>' .
50
                    '</div></div>';
51
            })
52
            ->editColumn('created_at', function (User $user) {
53
                return "<time datetime='{$user->created_at}'>{$user->created_at->format('d/m/Y')}</time>";
54
            })
55
            ->rawColumns(['action', 'created_at', 'name', 'role_id']);
56
    }
57
58
    /**
59
     * Get query source of dataTable.
60
     *
61
     * @param User $model
62
     * @return \Illuminate\Database\Eloquent\Builder
63
     */
64
    public function query(User $model)
65
    {
66
        return $model->newQuery()->with(['role']);
67
    }
68
69
    /**
70
     * Get columns.
71
     *
72
     * @return array
73
     */
74
    protected function getColumns()
75
    {
76
        return [
77
            Column::make('id')->title(trans('microboard::users.fields.id'))->width('1%'),
78
            Column::make('name')->title(trans('microboard::users.fields.name'))->width('25%'),
79
            Column::make('role_id')->title(trans('microboard::users.fields.role_id')),
80
            Column::make('email')->title(trans('microboard::users.fields.email')),
81
            Column::make('created_at')->title(trans('microboard::users.fields.created_at'))
82
                ->searchable(false),
83
            Column::computed('action', '')
84
                ->exportable(false)
85
                ->printable(false)
86
                ->width('1%')
87
                ->addClass('text-right')
88
        ];
89
    }
90
}
91