Completed
Push — master ( 5c55d7...79e99a )
by Fèvre
28s queued 15s
created

UserController::search()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 27
nc 5
nop 1
dl 0
loc 37
rs 9.1768
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A UserController::showUpdateForm() 0 23 2
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\User;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Hash;
8
use Illuminate\View\View;
9
use Xetaio\Mentions\Parser\MentionParser;
10
use Xetaravel\Http\Controllers\Admin\Controller;
11
use Xetaravel\Models\Repositories\UserRepository;
12
use Xetaravel\Models\Repositories\AccountRepository;
13
use Xetaravel\Models\User;
14
use Xetaravel\Models\Role;
15
use Xetaravel\Models\Validators\UserValidator;
16
17
class UserController extends Controller
18
{
19
    /**
20
     * Show the search page.
21
     *
22
     * @return \Illuminate\View\View
23
     */
24
    public function index(): View
25
    {
26
        $breadcrumbs = $this->breadcrumbs->addCrumb(
27
            '<i class="fa-solid fa-users mr-2"></i> Manage Users',
28
            route('admin.user.user.index')
29
        );
30
31
        return view('Admin::User.user.index', compact('breadcrumbs'));
32
    }
33
34
    /**
35
     * Show the update form.
36
     *
37
     * @param \Illuminate\Http\Request $request
38
     * @param string $slug The slug of the user.
39
     * @param int $id The id of the user.
40
     *
41
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
42
     */
43
    public function showUpdateForm(Request $request, string $slug, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $slug is not used and could be removed. ( Ignorable by Annotation )

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

43
    public function showUpdateForm(Request $request, /** @scrutinizer ignore-unused */ string $slug, int $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

43
    public function showUpdateForm(/** @scrutinizer ignore-unused */ Request $request, string $slug, int $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        $user = User::findOrFail($id);
46
47
        $roles = Role::pluck('name', 'id');
48
        $attributes = Role::pluck('id')->toArray();
49
50
        $optionsAttributes = [];
51
        foreach ($attributes as $attribute) {
52
            $optionsAttributes[$attribute] = [
53
                'style' => Role::where('id', $attribute)->select('css')->first()->css
0 ignored issues
show
Bug introduced by
The property css does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
Bug Best Practice introduced by
The property css does not exist on Xetaravel\Models\Role. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The property css does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
54
            ];
55
        }
56
57
        $breadcrumbs = $this->breadcrumbs
58
            ->setListElementClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0')
59
            ->addCrumb('<i class="fa-solid fa-users mr-2"></i> Manage Users', route('admin.user.user.index'))
60
            ->addCrumb(
61
                '<i class="fa-solid fa-pen-to-square mr-2"></i> Edit ' . e($user->username),
62
                route('admin.user.user.update', $user->slug, $user->id)
63
            );
64
65
        return view('Admin::User.user.update', compact('user', 'roles', 'optionsAttributes', 'breadcrumbs'));
66
    }
67
68
    /**
69
     * Handle an user update request for the application.
70
     *
71
     * @param \Illuminate\Http\Request $request
72
     * @param int $id The id of the user to update.
73
     *
74
     * @return \Illuminate\Http\RedirectResponse
75
     */
76
    public function update(Request $request, int $id): RedirectResponse
77
    {
78
        $user = User::findOrFail($id);
79
80
        UserValidator::update($request->all(), $user->id)->validate();
81
        UserRepository::update($request->all(), $user);
82
        $account = AccountRepository::update($request->get('account'), $user->id);
83
84
        $parser = new MentionParser($account, ['mention' => false]);
85
        $signature = $parser->parse($account->signature);
86
        $biography = $parser->parse($account->biography);
87
88
        $account->signature = $signature;
89
        $account->biography = $biography;
90
        $account->save();
91
92
        $user->roles()->sync($request->get('roles'));
93
94
        return redirect()
95
            ->route('admin.user.user.index')
96
            ->with('success', 'This user has been updated successfully !');
97
    }
98
99
    /**
100
     * Handle the delete request for the user.
101
     *
102
     * @param \Illuminate\Http\Request $request
103
     * @param int $id The id of the user to delete.
104
     *
105
     * @return \Illuminate\Http\RedirectResponse
106
     */
107
    public function delete(Request $request, int $id): RedirectResponse
108
    {
109
        $user = User::findOrFail($id);
110
111
        if (!Hash::check($request->input('password'), Auth::user()->password)) {
112
            return redirect()
113
                ->back()
114
                ->with('danger', 'Your Password does not match !');
115
        }
116
117
        if ($user->delete()) {
118
            return redirect()
119
                ->route('admin.user.user.index')
120
                ->with('success', 'This user has been deleted successfully !');
121
        }
122
123
        return redirect()
124
            ->route('admin.user.user.index')
125
            ->with('danger', 'An error occurred while deleting this user !');
126
    }
127
128
    /**
129
     * Delete the avatar for the specified user.
130
     *
131
     * @param int $id The id of the user.
132
     *
133
     * @return \Illuminate\Http\RedirectResponse
134
     */
135
    public function deleteAvatar(int $id): RedirectResponse
136
    {
137
        $user = User::findOrFail($id);
138
139
        $user->clearMediaCollection('avatar');
140
        $user->addMedia(public_path('images/avatar.png'))
141
            ->preservingOriginal()
142
            ->setName(substr(md5($user->username), 0, 10))
143
            ->setFileName(substr(md5($user->username), 0, 10) . '.png')
144
            ->toMediaCollection('avatar');
145
146
        return redirect()
147
            ->back()
148
            ->with('success', 'The avatar has been deleted successfully !');
149
    }
150
}
151