Passed
Push — develop ( 6302a9...3db2fb )
by Septianata
04:45
created

UserController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
eloc 24
dl 0
loc 100
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 5 1
A datatable() 0 22 2
A update() 0 5 1
A edit() 0 3 1
A create() 0 3 1
A index() 0 3 1
A store() 0 5 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Resources\DataTables\UserResource;
7
use App\Models\User;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Str;
10
use Yajra\DataTables\Facades\DataTables;
11
12
class UserController extends Controller
13
{
14
    /**
15
     * Display a listing of the resource.
16
     *
17
     * @return \Illuminate\Contracts\Support\Renderable
18
     */
19
    public function index()
20
    {
21
        return view('admin.user.index');
22
    }
23
24
    /**
25
     * Return datatable server side response.
26
     *
27
     * @return \Illuminate\Http\JsonResponse
28
     */
29
    public function datatable()
30
    {
31
        return DataTables::eloquent(User::query()->with('branch:id,name'))
32
            ->setTransformer(fn ($model) => UserResource::make($model)->resolve())
33
            ->orderColumn('branch_name', function ($query, $direction) {
34
                $query->join('branches', 'users.branch_id', '=', 'branches.id')
35
                    ->orderBy('branches.name', $direction);
36
            })
37
            ->filterColumn('branch_name', function ($query, $keyword) {
38
                $query->whereHas('branch', function ($query) use ($keyword) {
39
                    $query->where('name', 'like', '%' . $keyword . '%');
40
                });
41
            })
42
            ->filterColumn('is_active', function ($query, $keyword) {
43
                $active = Str::lower(trans('Active'));
0 ignored issues
show
Bug introduced by
It seems like trans('Active') can also be of type array and array; however, parameter $value of Illuminate\Support\Str::lower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
                $active = Str::lower(/** @scrutinizer ignore-type */ trans('Active'));
Loading history...
44
                $notActive = Str::lower(trans('Not Active'));
45
46
                if (in_array($keyword, [$active, $notActive])) {
47
                    $query->where('is_active', $keyword === $active);
48
                }
49
            })
50
            ->toJson();
51
    }
52
53
    /**
54
     * Show the form for creating a new resource.
55
     *
56
     * @return \Illuminate\Contracts\Support\Renderable
57
     */
58
    public function create()
59
    {
60
        return view('admin.user.create');
61
    }
62
63
    /**
64
     * Store a newly created resource in storage.
65
     *
66
     * @param  \Illuminate\Http\Request  $request
67
     * @return \Illuminate\Http\RedirectResponse
68
     */
69
    public function store(Request $request)
70
    {
71
        User::create($request->all());
72
73
        return redirect()->route('admin.user.index');
74
    }
75
76
    /**
77
     * Show the form for editing the specified resource.
78
     *
79
     * @param  \App\Models\User  $user
80
     * @return \Illuminate\Contracts\Support\Renderable
81
     */
82
    public function edit(User $user)
83
    {
84
        return view('admin.user.edit', compact('user'));
85
    }
86
87
    /**
88
     * Update the specified resource in storage.
89
     *
90
     * @param  \Illuminate\Http\Request  $request
91
     * @param  \App\Models\User  $user
92
     * @return \Illuminate\Http\RedirectResponse
93
     */
94
    public function update(Request $request, User $user)
95
    {
96
        $user->update($request->all());
97
98
        return redirect()->route('admin.user.index');
99
    }
100
101
    /**
102
     * Remove the specified resource from storage.
103
     *
104
     * @param  \App\Models\User  $user
105
     * @return \Illuminate\Http\RedirectResponse
106
     */
107
    public function destroy(User $user)
108
    {
109
        $user->delete();
110
111
        return redirect()->route('admin.user.index');
112
    }
113
}
114