Passed
Push — develop ( 95decf...daf980 )
by Septianata
06:53 queued 33s
created

UserController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 81.13%

Importance

Changes 0
Metric Value
wmc 9
eloc 46
c 0
b 0
f 0
dl 0
loc 148
ccs 43
cts 53
cp 0.8113
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 8 1
A datatable() 0 23 2
A update() 0 15 1
A edit() 0 3 1
A create() 0 3 1
A index() 0 3 1
A store() 0 17 1
A destroyMultiple() 0 8 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\User\StoreRequest;
7
use App\Http\Requests\User\UpdateRequest;
8
use App\Http\Resources\DataTables\UserResource;
9
use App\Models\User;
10
use Illuminate\Auth\Events\Registered;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Event;
13
use Illuminate\Support\Str;
14
use Yajra\DataTables\Facades\DataTables;
15
16
class UserController extends Controller
17
{
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @return \Illuminate\Contracts\Support\Renderable
22
     */
23 1
    public function index()
24
    {
25 1
        return view('admin.user.index');
26
    }
27
28
    /**
29
     * Return datatable server side response.
30
     *
31
     * @return \Illuminate\Http\JsonResponse
32
     */
33 1
    public function datatable()
34
    {
35 1
        return DataTables::eloquent(User::query()->with('branch:id,name'))
36 1
            ->setTransformer(fn ($model) => UserResource::make($model)->resolve())
37 1
            ->orderColumn('branch_name', function ($query, $direction) {
38
                $query->join('branches', 'users.branch_id', '=', 'branches.id')
39
                    ->select('users.*', 'branches.id as branch_id', 'branches.name as branch_name')
40
                    ->orderBy('branches.name', $direction);
41 1
            })
42 1
            ->filterColumn('branch_name', function ($query, $keyword) {
43
                $query->whereHas('branch', function ($query) use ($keyword) {
44
                    $query->where('name', 'like', '%' . $keyword . '%');
45
                });
46 1
            })
47 1
            ->filterColumn('is_active', function ($query, $keyword) {
48
                $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

48
                $active = Str::lower(/** @scrutinizer ignore-type */ trans('Active'));
Loading history...
49
                $notActive = Str::lower(trans('Not Active'));
50
51
                if (in_array($keyword, [$active, $notActive])) {
52
                    $query->where('is_active', $keyword === $active);
53
                }
54 1
            })
55 1
            ->toJson();
56
    }
57
58
    /**
59
     * Show the form for creating a new resource.
60
     *
61
     * @return \Illuminate\Contracts\Support\Renderable
62
     */
63 1
    public function create()
64
    {
65 1
        return view('admin.user.create');
66
    }
67
68
    /**
69
     * Store a newly created resource in storage.
70
     *
71
     * @param  \App\Http\Requests\User\StoreRequest  $request
72
     * @return \Illuminate\Http\RedirectResponse
73
     */
74 2
    public function store(StoreRequest $request)
75
    {
76
        /** @var \App\Models\User $user */
77 2
        $user = User::make($request->validated())->setBranchRelationValue(
78 2
            $request->getBranch()
79
        );
80
81 2
        $user->save();
82
83 2
        $user->syncRoles($request->input('role'));
84
85 2
        Event::dispatch(new Registered($user));
86
87 2
        return redirect()->route('admin.user.index')->with([
88
            'alert' => [
89 2
                'type' => 'alert-success',
90 2
                'message' => trans('The :resource was created!', ['resource' => trans('admin-lang.user')]),
91
            ],
92
        ]);
93
    }
94
95
    /**
96
     * Show the form for editing the specified resource.
97
     *
98
     * @param  \App\Models\User  $user
99
     * @return \Illuminate\Contracts\Support\Renderable
100
     */
101 1
    public function edit(User $user)
102
    {
103 1
        return view('admin.user.edit', compact('user'));
104
    }
105
106
    /**
107
     * Update the specified resource in storage.
108
     *
109
     * @param  \App\Http\Requests\User\UpdateRequest  $request
110
     * @param  \App\Models\User  $user
111
     * @return \Illuminate\Http\RedirectResponse
112
     */
113 1
    public function update(UpdateRequest $request, User $user)
114
    {
115
        /** @var \App\Models\User $user */
116 1
        $user = $user->fill($request->validated())->setBranchRelationValue(
117 1
            $request->getBranch()
118
        );
119
120 1
        $user->save();
121
122 1
        $user->syncRoles($request->input('role'));
123
124 1
        return redirect()->route('admin.user.index')->with([
125
            'alert' => [
126 1
                'type' => 'alert-success',
127 1
                'message' => trans('The :resource was updated!', ['resource' => trans('admin-lang.user')]),
128
            ],
129
        ]);
130
    }
131
132
    /**
133
     * Remove the specified resource from storage.
134
     *
135
     * @param  \App\Models\User  $user
136
     * @return \Illuminate\Http\RedirectResponse
137
     */
138 1
    public function destroy(User $user)
139
    {
140 1
        $user->delete();
141
142 1
        return redirect()->route('admin.user.index')->with([
143
            'alert' => [
144 1
                'type' => 'alert-success',
145 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.user')]),
146
            ],
147
        ]);
148
    }
149
150
    /**
151
     * Remove the specified list of resource from storage.
152
     *
153
     * @param  \Illuminate\Http\Request  $request
154
     * @return \Illuminate\Http\RedirectResponse
155
     */
156 1
    public function destroyMultiple(Request $request)
157
    {
158 1
        User::destroy($request->input('checkbox', []));
159
160 1
        return redirect()->route('admin.user.index')->with([
161
            'alert' => [
162 1
                'type' => 'alert-success',
163 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.user')]),
164
            ],
165
        ]);
166
    }
167
}
168