Passed
Push — develop ( 6dcea6...5ae9a6 )
by Septianata
16:24
created

UserController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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\DB;
13
use Illuminate\Support\Facades\Event;
14
use Illuminate\Support\Str;
15
use Yajra\DataTables\Facades\DataTables;
16
17
class UserController extends Controller
18
{
19
    /**
20
     * Create a new instance class.
21
     *
22
     * @return void
23
     */
24 9
    public function __construct()
25
    {
26 9
        $this->authorizeResource(User::class, 'user');
27 9
    }
28
29
    /**
30
     * Display a listing of the resource.
31
     *
32
     * @return \Illuminate\Contracts\Support\Renderable
33
     */
34 1
    public function index()
35
    {
36 1
        return view('admin.user.index');
37
    }
38
39
    /**
40
     * Return datatable server side response.
41
     *
42
     * @return \Illuminate\Http\JsonResponse
43
     */
44 1
    public function datatable()
45
    {
46 1
        $this->authorize('viewAny', User::class);
47
48 1
        return DataTables::eloquent(User::query()->with('branch:id,name'))
49 1
            ->setTransformer(fn ($model) => UserResource::make($model)->resolve())
50 1
            ->orderColumn('branch_name', function ($query, $direction) {
51
                $query->join('branches', 'users.branch_id', '=', 'branches.id')
52
                    ->select('users.*', 'branches.id as branch_id', 'branches.name as branch_name')
53
                    ->orderBy('branches.name', $direction);
54 1
            })
55 1
            ->filterColumn('branch_name', function ($query, $keyword) {
56
                $query->whereHas('branch', function ($query) use ($keyword) {
57
                    $query->where('name', 'like', '%' . $keyword . '%');
58
                });
59 1
            })
60 1
            ->filterColumn('is_active', function ($query, $keyword) {
61
                $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

61
                $active = Str::lower(/** @scrutinizer ignore-type */ trans('Active'));
Loading history...
62
                $notActive = Str::lower(trans('Not Active'));
63
64
                if (in_array($keyword, [$active, $notActive])) {
65
                    $query->where('is_active', $keyword === $active);
66
                }
67 1
            })
68 1
            ->toJson();
69
    }
70
71
    /**
72
     * Show the form for creating a new resource.
73
     *
74
     * @return \Illuminate\Contracts\Support\Renderable
75
     */
76 1
    public function create()
77
    {
78 1
        return view('admin.user.create');
79
    }
80
81
    /**
82
     * Store a newly created resource in storage.
83
     *
84
     * @param  \App\Http\Requests\User\StoreRequest  $request
85
     * @return \Illuminate\Http\RedirectResponse
86
     */
87 2
    public function store(StoreRequest $request)
88
    {
89
        /** @var \App\Models\User $user */
90 2
        $user = User::make($request->validated())->setBranchRelationValue(
91 2
            $request->getBranch()
92
        );
93
94 2
        $user->save();
95
96 2
        $user->syncRoles($request->input('role'));
97
98 2
        Event::dispatch(new Registered($user));
99
100 2
        return redirect()->route('admin.user.index')->with([
101
            'alert' => [
102 2
                'type' => 'alert-success',
103 2
                'message' => trans('The :resource was created!', ['resource' => trans('admin-lang.user')]),
104
            ],
105
        ]);
106
    }
107
108
    /**
109
     * Display the specified resource.
110
     *
111
     * @param  \App\Models\User  $user
112
     * @return \Illuminate\Contracts\Support\Renderable
113
     */
114
    public function show(User $user)
115
    {
116
        return view('admin.user.show', compact('user'));
117
    }
118
119
    /**
120
     * Show the form for editing the specified resource.
121
     *
122
     * @param  \App\Models\User  $user
123
     * @return \Illuminate\Contracts\Support\Renderable
124
     */
125 1
    public function edit(User $user)
126
    {
127 1
        $user->append('role');
128
129 1
        return view('admin.user.edit', compact('user'));
130
    }
131
132
    /**
133
     * Update the specified resource in storage.
134
     *
135
     * @param  \App\Http\Requests\User\UpdateRequest  $request
136
     * @param  \App\Models\User  $user
137
     * @return \Illuminate\Http\RedirectResponse
138
     */
139 1
    public function update(UpdateRequest $request, User $user)
140
    {
141 1
        $user = $user->fill($request->validated())->setBranchRelationValue(
142 1
            $request->getBranch()
143
        );
144
145 1
        $user->save();
146
147 1
        $user->syncRoles($request->input('role'));
148
149 1
        return redirect()->route('admin.user.edit', $user)->with([
150
            'alert' => [
151 1
                'type' => 'alert-success',
152 1
                'message' => trans('The :resource was updated!', ['resource' => trans('admin-lang.user')]),
153
            ],
154
        ]);
155
    }
156
157
    /**
158
     * Manually verifiy user's email address.
159
     *
160
     * @param  \App\Models\User  $user
161
     * @return \Illuminate\Http\RedirectResponse
162
     */
163
    public function verifyEmailAddress(User $user)
164
    {
165
        $this->authorize('update', $user);
166
167
        $user->markEmailAsVerified();
168
169
        return redirect()->route('admin.user.edit', $user)->with([
170
            'alert' => [
171
                'type' => 'alert-success',
172
                'message' => trans('The :resource was updated!', ['resource' => trans('Verify Email Address')]),
173
            ],
174
        ]);
175
    }
176
177
    /**
178
     * Remove the specified resource from storage.
179
     *
180
     * @param  \App\Models\User  $user
181
     * @return \Illuminate\Http\RedirectResponse
182
     */
183 1
    public function destroy(User $user)
184
    {
185 1
        $user->delete();
186
187 1
        return redirect()->route('admin.user.index')->with([
188
            'alert' => [
189 1
                'type' => 'alert-success',
190 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.user')]),
191
            ],
192
        ]);
193
    }
194
195
    /**
196
     * Remove the specified list of resource from storage.
197
     *
198
     * @param  \Illuminate\Http\Request  $request
199
     * @return \Illuminate\Http\RedirectResponse
200
     */
201 1
    public function destroyMultiple(Request $request)
202
    {
203 1
        DB::transaction(function () use ($request) {
204 1
            foreach ($request->input('checkbox', []) as $id) {
205 1
                $user = User::find($id, 'id');
206
207 1
                $this->authorize('delete', $user);
208
209 1
                $user->delete();
210
            }
211 1
        });
212
213 1
        return redirect()->route('admin.user.index')->with([
214
            'alert' => [
215 1
                'type' => 'alert-success',
216 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.user')]),
217
            ],
218
        ]);
219
    }
220
}
221