Completed
Push — manage-users ( 2c714e...6ca97d )
by Fèvre
02:42
created

UserController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 13
nc 2
nop 2
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\User;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\View\View;
7
use Ultraware\Roles\Models\Role;
8
use Xetaravel\Http\Controllers\Admin\Controller;
9
use Xetaravel\Models\Repositories\UserRepository;
10
use Xetaravel\Models\Repositories\AccountRepository;
11
use Xetaravel\Models\User;
12
use Xetaravel\Models\Validators\UserValidator;
13
14
class UserController extends Controller
15
{
16
    /**
17
     * Show the search page.
18
     *
19
     * @return \Illuminate\View\View
20
     */
21
    public function index(): View
22
    {
23
        $latestUsers = User::with(['roles'])
24
            ->limit(5)
25
            ->latest()
26
            ->get();
27
28
        $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...
29
30
        return view('Admin::User.user.index', compact('latestUsers', 'breadcrumbs'));
31
    }
32
    /**
33
     * Search users related to the type.
34
     *
35
     * @param \Illuminate\Http\Request $request
36
     *
37
     * @return \Illuminate\View\View
38
     */
39
    public function search(Request $request): View
40
    {
41
        $query = User::with(['roles'])->select();
42
        $search = str_replace('%', '\\%', trim($request->input('search')));
43
        $type = trim($request->input('type'));
44
45
        switch ($type) {
46
            case 'username':
47
                $query->where('username', 'like', '%' . $search . '%');
48
                break;
49
50
            case 'email':
51
                $query->where('email', 'like', '%' . $search . '%');
52
                break;
53
54
            case 'register_ip':
55
                $query->where('register_ip', 'like', '%' . $search . '%');
56
                break;
57
58
            case 'last_login_ip':
59
                $query->where('last_login_ip', 'like', '%' . $search . '%');
60
                break;
61
62
            default:
63
                $query->where('username', 'like', '%' . $search . '%');
64
                $type = 'username';
65
                break;
66
        }
67
        $users = $query
68
            ->paginate(10)
69
            ->appends($request->except('page'));
70
71
        $breadcrumbs = $this->breadcrumbs
72
            ->addCrumb('Manage Users', route('admin.user.user.index'))
73
            ->addCrumb('Search an user', route('admin.user.user.search'));
74
75
        return view('Admin::User.user.search', compact('users', 'breadcrumbs', 'type', 'search'));
76
    }
77
78
    /**
79
     * Show the update form.
80
     *
81
     * @param \Illuminate\Http\Request $request
82
     * @param string $slug The slug of the user.
83
     * @param int $id The id of the user.
84
     *
85
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
0 ignored issues
show
Documentation introduced by
Should the return type not be RedirectResponse|View|\I...\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...
86
     */
87
    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...
88
    {
89
        $user = User::find($id);
90
91
        if (is_null($user)) {
92
            return redirect()
93
                ->route('admin.user.user.index')
94
                ->with('danger', 'This user doesn\'t exist or has been deleted !');
95
        }
96
97
        $roles = Role::pluck('name', 'id');
98
        $attributes = Role::pluck('id')->toArray();
99
100
        $optionsAttributes = [];
101
        foreach ($attributes as $attribute) {
102
            $optionsAttributes[$attribute] = [
103
                'style' => Role::where('id', $attribute)->select('css')->first()->css
104
            ];
105
        }
106
107
        $breadcrumbs = $this->breadcrumbs
108
            ->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0')
109
            ->addCrumb('Manage Users', route('admin.user.user.index'))
110
            ->addCrumb(
111
                'Edit ' . e($user->username),
112
                route('admin.user.user.update', $user->slug, $user->id)
113
            );
114
115
        return view('Admin::User.user.update', compact('user', 'roles', 'optionsAttributes', 'breadcrumbs'));
116
    }
117
118
    /**
119
     * Handle an user update request for the application.
120
     *
121
     * @param \Illuminate\Http\Request $request
122
     * @param int $id The id of the user to update.
123
     *
124
     * @return \Illuminate\Http\RedirectResponse
125
     */
126
    public function update(Request $request, int $id): RedirectResponse
127
    {
128
        $user = User::find($id);
129
130
        if (is_null($user)) {
131
            return redirect()
132
                ->back()
133
                ->with('danger', 'This user doesn\'t exist or has been deleted !');
134
        }
135
136
        UserValidator::update($request->all(), $user->id)->validate();
137
138
        UserRepository::update($request->all(), $user);
139
        AccountRepository::update($request->get('account'), $user->id);
140
141
        $user->roles()->sync($request->get('roles'));
142
143
        return redirect()
144
            ->back()
145
            ->with('success', 'This user has been updated successfully !');
146
    }
147
}
148