Completed
Push — develop ( 3c0bec...e16864 )
by Abdelrahman
01:47
created

UsersController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Controllers\Backend;
6
7
use Illuminate\Http\Request;
8
use Rinvex\Fort\Models\Role;
9
use Rinvex\Fort\Models\User;
10
use Rinvex\Fort\Models\Ability;
11
use Cortex\Foundation\Http\Controllers\AuthorizedController;
12
13
class UsersController extends AuthorizedController
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $resource = 'users';
19
20
    /**
21
     * Display a listing of the resource.
22
     *
23
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\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...
24
     */
25
    public function index()
26
    {
27
        $users = User::paginate(config('rinvex.fort.backend.items_per_page'));
28
29
        return view('cortex/fort::backend.users.index', compact('users'));
30
    }
31
32
    /**
33
     * Store a newly created resource in storage.
34
     *
35
     * @param \Illuminate\Http\Request $request
36
     *
37
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
38
     */
39
    public function store(Request $request)
40
    {
41
        return $this->process($request, new User());
42
    }
43
44
    /**
45
     * Update the given resource in storage.
46
     *
47
     * @param \Illuminate\Http\Request $request
48
     * @param \Rinvex\Fort\Models\User $user
49
     *
50
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
51
     */
52
    public function update(Request $request, User $user)
53
    {
54
        return $this->process($request, $user);
55
    }
56
57
    /**
58
     * Delete the given resource from storage.
59
     *
60
     * @param \Rinvex\Fort\Models\User $user
61
     *
62
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
63
     */
64
    public function delete(User $user)
65
    {
66
        $user->delete();
67
68
        return intend([
69
            'url' => route('backend.users.index'),
70
            'with' => ['warning' => trans('cortex/fort::messages.user.deleted', ['userId' => $user->id])],
71
        ]);
72
    }
73
74
    /**
75
     * Show the form for create/update of the given resource.
76
     *
77
     * @param \Rinvex\Fort\Models\User $user
78
     *
79
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\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...
80
     */
81
    public function form(User $user)
82
    {
83
        $countries = array_map(function ($country) {
84
            return $country['name'];
85
        }, countries());
86
87
        $languages = array_map(function ($language) {
88
            return $language['name'];
89
        }, languages());
90
91
        $abilityList = Ability::all()->groupBy('resource')->map(function ($item) {
92
            return $item->pluck('name', 'id');
93
        })->toArray();
94
95
        $roleList = Role::all()->pluck('name', 'id')->toArray();
96
97
        return view('cortex/fort::backend.users.form', compact('user', 'abilityList', 'roleList', 'countries', 'languages'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
98
    }
99
100
    /**
101
     * Process the form for store/update of the given resource.
102
     *
103
     * @param \Illuminate\Http\Request $request
104
     * @param \Rinvex\Fort\Models\User $user
105
     *
106
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
107
     */
108
    protected function process(Request $request, User $user)
109
    {
110
        // Prepare required input fields
111
        $input = $request->all();
112
        $input['email_verified'] = $request->get('email_verified', false);
113
        $input['phone_verified'] = $request->get('phone_verified', false);
114
115
        // Remove empty password fields
116
        if (! $input['password']) {
117
            unset($input['password']);
118
        }
119
120
        // Save user
121
        $user->fill($input)->save();
122
123
        // Sync abilities
124
        if ($request->user($this->getGuard())->can('grant-abilities')) {
125
            $user->abilities()->sync((array) array_pull($input, 'abilityList'));
126
        }
127
128
        // Sync roles
129
        if ($request->user($this->getGuard())->can('assign-roles')) {
130
            $user->roles()->sync((array) array_pull($input, 'roleList'));
131
        }
132
133
        return intend([
134
            'url' => route('backend.users.index'),
135
            'with' => ['success' => trans('cortex/fort::messages.user.saved', ['userId' => $user->id])],
136
        ]);
137
    }
138
}
139