Completed
Push — develop ( e16864...bade13 )
by Abdelrahman
09:13
created

UsersController::process()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 40
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 18
nc 32
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Controllers\Backend;
6
7
use Carbon\Carbon;
8
use Illuminate\Http\Request;
9
use Cortex\Fort\Models\Role;
10
use Cortex\Fort\Models\User;
11
use Cortex\Fort\Models\Ability;
12
use Cortex\Fort\DataTables\Backend\UsersDataTable;
13
use Cortex\Foundation\Http\Controllers\AuthorizedController;
14
15
class UsersController extends AuthorizedController
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected $resource = 'users';
21
22
    /**
23
     * Display a listing of the resource.
24
     *
25
     * @return \Illuminate\Http\Response
26
     */
27
    public function index()
28
    {
29
        return app(UsersDataTable::class)->render('cortex/foundation::backend.pages.datatable', ['resource' => 'cortex/fort::common.users']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 141 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...
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 \Cortex\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 \Cortex\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 \Cortex\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 = countries();
84
        $roleList = Role::all()->pluck('name', 'id')->toArray();
85
        $languages = collect(languages())->pluck('name', 'iso_639_1');
86
        $abilityList = Ability::all()->groupBy('resource')->map(function ($item) {
87
            return $item->pluck('name', 'id');
88
        })->toArray();
89
90
        return view('cortex/fort::backend.forms.user', 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...
91
    }
92
93
    /**
94
     * Process the form for store/update of the given resource.
95
     *
96
     * @param \Illuminate\Http\Request $request
97
     * @param \Cortex\Fort\Models\User $user
98
     *
99
     * @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...
100
     */
101
    protected function process(Request $request, User $user)
102
    {
103
        // Prepare required input fields
104
        $input = $request->all();
105
        $input['email_verified'] = $request->get('email_verified', false);
106
        $input['phone_verified'] = $request->get('phone_verified', false);
107
108
        // Remove empty password fields
109
        if (! $input['password']) {
110
            unset($input['password']);
111
        }
112
113
        // Update email verification date
114
        if ($user->email_verified && ! $user->email_verified_at) {
115
            $input['email_verified_at'] = Carbon::now();
116
        }
117
118
        // Update phone verification date
119
        if ($user->phone_verified && ! $user->phone_verified_at) {
120
            $input['phone_verified_at'] = Carbon::now();
121
        }
122
123
        // Save user
124
        $user->fill($input)->save();
125
126
        // Sync abilities
127
        if ($request->user($this->getGuard())->can('grant-abilities')) {
128
            $user->abilities()->sync((array) array_pull($input, 'abilityList'));
129
        }
130
131
        // Sync roles
132
        if ($request->user($this->getGuard())->can('assign-roles')) {
133
            $user->roles()->sync((array) array_pull($input, 'roleList'));
134
        }
135
136
        return intend([
137
            'url' => route('backend.users.index'),
138
            'with' => ['success' => trans('cortex/fort::messages.user.saved', ['userId' => $user->id])],
139
        ]);
140
    }
141
}
142