Completed
Push — master ( 8f08c7...7bd638 )
by Mohamed
02:25
created

UserDataTable::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Microboard\DataTables;
4
5
use App\User;
6
use Microboard\Traits\DataTable as MicroboardDataTable;
7
use Yajra\DataTables\DataTableAbstract;
8
use Yajra\DataTables\Html\Column;
9
use Yajra\DataTables\Services\DataTable;
10
11
class UserDataTable extends DataTable
12
{
13
    use MicroboardDataTable;
14
15
    /**
16
     * Build DataTable class.
17
     *
18
     * @param mixed $query Results from query() method.
19
     * @return DataTableAbstract
20
     */
21
    public function dataTable($query)
22
    {
23
        return $this->build($query)
24
            ->editColumn('role_id', function (User $user) {
25
                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...
26
                    $route = route('microboard.roles.show', $user->role);
27
                    return "<a href='{$route}'>{$user->role->display_name}</a>";
28
                }
29
30
                return $user->role->display_name;
31
            })
32
            ->editColumn('name', function (User $user) {
33
                return '<div class="media align-items-center">' .
34
                    '<span class="avatar avatar-sm rounded-circle mr-3">' .
35
                    '<img alt="' . $user->name . '" src="' . $user->avatar . '"></span>' .
36
                    '<div class="media-body">' .
37
                    '<span class="mb-0 d-block">' . $user->name . '</span>' .
38
                    '<small class="mb-0" style="font-size: 10px;"><i class="fa fa-clock"></i> ' .
39
                    '<time datetime="' . $user->updated_at . '">' .
40
                    trans('microboard::users.fields.updated_at') . ' ' .
41
                    $user->updated_at->diffForHumans() . '</time></small>' .
42
                    '</div></div>';
43
            })
44
            ->editColumn('created_at', function (User $user) {
45
                return "<time datetime='{$user->created_at}'>{$user->created_at->format('d/m/Y')}</time>";
46
            })
47
            ->rawColumns(['action', 'created_at', 'name', 'role_id']);
48
    }
49
50
    /**
51
     * Get query source of dataTable.
52
     *
53
     * @param User $model
54
     * @return \Illuminate\Database\Eloquent\Builder
55
     */
56
    public function query(User $model)
57
    {
58
        return $model->newQuery()->with(['role']);
59
    }
60
61
    /**
62
     * Get columns.
63
     *
64
     * @return array
65
     */
66
    protected function getColumns()
67
    {
68
        return [
69
            Column::make('id')->title(trans('microboard::users.fields.id'))->width('1%'),
70
            Column::make('name')->title(trans('microboard::users.fields.name'))->width('25%'),
71
            Column::make('role_id')->title(trans('microboard::users.fields.role_id')),
72
            Column::make('email')->title(trans('microboard::users.fields.email')),
73
            Column::make('created_at')->title(trans('microboard::users.fields.created_at')),
74
            Column::computed('action', '')
75
                ->exportable(false)
76
                ->printable(false)
77
                ->width('1%')
78
                ->addClass('text-right')
79
        ];
80
    }
81
}
82