Completed
Pull Request — master (#20)
by Fèvre
02:30
created

UserController::showUpdateForm()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 3
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 Ultraware\Roles\Models\Role;
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\Validators\UserValidator;
15
16
class UserController extends Controller
17
{
18
    /**
19
     * Show the search page.
20
     *
21
     * @return \Illuminate\View\View
22
     */
23
    public function index(): View
24
    {
25
        $latestUsers = User::with(['roles'])
26
            ->limit(5)
27
            ->latest()
28
            ->get();
29
30
        $breadcrumbs = $this->breadcrumbs->addCrumb('Manage Users', route('admin.user.user.index'));
0 ignored issues
show
Bug introduced by
The property breadcrumbs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
32
        return view('Admin::User.user.index', compact('latestUsers', 'breadcrumbs'));
33
    }
34
    /**
35
     * Search users related to the type.
36
     *
37
     * @param \Illuminate\Http\Request $request
38
     *
39
     * @return \Illuminate\View\View
40
     */
41
    public function search(Request $request): View
42
    {
43
        $query = User::with(['roles'])->select();
44
        $search = str_replace('%', '\\%', trim($request->input('search')));
45
        $type = trim($request->input('type'));
46
47
        switch ($type) {
48
            case 'username':
49
                $query->where('username', 'like', '%' . $search . '%');
50
                break;
51
52
            case 'email':
53
                $query->where('email', 'like', '%' . $search . '%');
54
                break;
55
56
            case 'register_ip':
57
                $query->where('register_ip', 'like', '%' . $search . '%');
58
                break;
59
60
            case 'last_login_ip':
61
                $query->where('last_login_ip', 'like', '%' . $search . '%');
62
                break;
63
64
            default:
65
                $query->where('username', 'like', '%' . $search . '%');
66
                $type = 'username';
67
                break;
68
        }
69
        $users = $query
70
            ->paginate(10)
71
            ->appends($request->except('page'));
72
73
        $breadcrumbs = $this->breadcrumbs
74
            ->addCrumb('Manage Users', route('admin.user.user.index'))
75
            ->addCrumb('Search an user', route('admin.user.user.search'));
76
77
        return view('Admin::User.user.search', compact('users', 'breadcrumbs', 'type', 'search'));
78
    }
79
80
    /**
81
     * Show the update form.
82
     *
83
     * @param \Illuminate\Http\Request $request
84
     * @param string $slug The slug of the user.
85
     * @param int $id The id of the user.
86
     *
87
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
0 ignored issues
show
Documentation introduced by
Should the return type not be View|\Illuminate\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
88
     */
89
    public function showUpdateForm(Request $request, string $slug, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from 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 $slug is not used and could be removed.

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

Loading history...
90
    {
91
        $user = User::findOrFail($id);
92
93
        $roles = Role::pluck('name', 'id');
94
        $attributes = Role::pluck('id')->toArray();
95
96
        $optionsAttributes = [];
97
        foreach ($attributes as $attribute) {
98
            $optionsAttributes[$attribute] = [
99
                'style' => Role::where('id', $attribute)->select('css')->first()->css
100
            ];
101
        }
102
103
        $breadcrumbs = $this->breadcrumbs
104
            ->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0')
105
            ->addCrumb('Manage Users', route('admin.user.user.index'))
106
            ->addCrumb(
107
                'Edit ' . e($user->username),
108
                route('admin.user.user.update', $user->slug, $user->id)
109
            );
110
111
        return view('Admin::User.user.update', compact('user', 'roles', 'optionsAttributes', 'breadcrumbs'));
112
    }
113
114
    /**
115
     * Handle an user update request for the application.
116
     *
117
     * @param \Illuminate\Http\Request $request
118
     * @param int $id The id of the user to update.
119
     *
120
     * @return \Illuminate\Http\RedirectResponse
121
     */
122
    public function update(Request $request, int $id): RedirectResponse
123
    {
124
        $user = User::findOrFail($id);
125
126
        UserValidator::update($request->all(), $user->id)->validate();
127
        UserRepository::update($request->all(), $user);
128
        AccountRepository::update($request->get('account'), $user->id);
129
130
        $user->roles()->sync($request->get('roles'));
131
132
        return redirect()
133
            ->back()
134
            ->with('success', 'This user has been updated successfully !');
135
    }
136
137
    /**
138
     * Handle the delete request for the user.
139
     *
140
     * @param \Illuminate\Http\Request $request
141
     * @param int $id The id of the user to delete.
142
     *
143
     * @return \Illuminate\Http\RedirectResponse
144
     */
145 View Code Duplication
    public function delete(Request $request, int $id): RedirectResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        $user = User::findOrFail($id);
148
149
        if (!Hash::check($request->input('password'), Auth::user()->password)) {
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
150
            return redirect()
151
                ->back()
152
                ->with('danger', 'Your Password does not match !');
153
        }
154
155
        if ($user->delete()) {
156
            return redirect()
157
                ->route('admin.user.user.index')
158
                ->with('success', 'This user has been deleted successfully !');
159
        }
160
161
        return redirect()
162
            ->route('admin.user.user.index')
163
            ->with('danger', 'An error occurred while deleting this user !');
164
    }
165
166
    /**
167
     * Delete the avatar for the specified user.
168
     *
169
     * @param int $id The id of the user.
170
     *
171
     * @return \Illuminate\Http\RedirectResponse
172
     */
173
    public function deleteAvatar(int $id): RedirectResponse
174
    {
175
        $user = User::findOrFail($id);
176
177
        $user->clearMediaCollection('avatar');
178
        $user->addMedia(resource_path('assets/images/avatar.png'))
179
            ->preservingOriginal()
180
            ->setName(substr(md5($user->username), 0, 10))
181
            ->setFileName(substr(md5($user->username), 0, 10) . '.png')
182
            ->toMediaCollection('avatar');
183
184
        return redirect()
185
            ->back()
186
            ->with('success', 'The avatar has been deleted successfully !');
187
    }
188
}
189