Passed
Push — feature/post-covid-application... ( 981d58...8c4c7e )
by Yonathan
04:17
created

SettingsController::updateContactPreferences()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
rs 9.9332
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\User;
7
use App\Services\Validation\Rules\PasswordCorrectRule;
8
use App\Services\Validation\Rules\PasswordFormatRule;
9
use Facades\App\Services\WhichPortal;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Hash;
12
use Illuminate\Support\Facades\Lang;
13
use Illuminate\Validation\Rule;
14
15
class SettingsController extends Controller
16
{
17
18
    /**
19
     * Show the form for editing the logged-in User's settings
20
     *
21
     * @param  \Illuminate\Http\Request $request Incoming request.
22
     * @return \Illuminate\Http\Response
23
     */
24
    public function editAuthenticated(Request $request)
25
    {
26
        return $this->edit($request, $request->user());
27
    }
28
29
30
    /**
31
     * Display the specified resource.
32
     *
33
     * @param  \Illuminate\Http\Request $request Incoming request.
34
     * @param  \App\Models\User    $user Incoming User.
35
     * @return \Illuminate\Http\Response
36
     */
37
    public function edit(Request $request, User $user)
38
    {
39
        $this->authorize('view', $user);
40
        $this->authorize('update', $user);
41
42
        $data = [
43
            // Localized strings.
44
            'settings' => Lang::get('common/settings'),
45
            // Applicant data.
46
            'user' => $user,
47
            // Update routes.
48
            'submit_personal' => route(WhichPortal::prefixRoute('settings.personal.update'), $user),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

48
            'submit_personal' => /** @scrutinizer ignore-call */ route(WhichPortal::prefixRoute('settings.personal.update'), $user),
Loading history...
49
            'submit_password' => route(WhichPortal::prefixRoute('settings.password.update'), $user),
50
            'submit_government' => route(WhichPortal::prefixRoute('settings.government.update'), $user),
51
            'submit_contact_preferences' => route(WhichPortal::prefixRoute('settings.contact_preferences.update'), $user),
52
            'activate_two_factor' => route(WhichPortal::prefixRoute('two_factor.activate')),
53
            'deactivate_two_factor' => route(WhichPortal::prefixRoute('two_factor.deactivate')),
54
            'forget_remembered_devices' => route(WhichPortal::prefixRoute('two_factor.forget')),
55
            'generate_recovery_codes' => route(WhichPortal::prefixRoute('recovery_codes.show'))
56
        ];
57
58
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

58
        return /** @scrutinizer ignore-call */ view(
Loading history...
59
            'common/settings',
60
            $data
61
        );
62
    }
63
64
    /**
65
     * Update personal information
66
     *
67
     * @param  \Illuminate\Http\Request $request   Incoming request.
68
     * @param  \App\Models\User    $user Incoming User.
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function updatePersonal(Request $request, User $user)
72
    {
73
        $validData = $request->validate([
74
            'first_name' => 'required|string|max:191',
75
            'last_name' => 'required|string|max:191',
76
            'email' => [
77
                'required',
78
                'email:rfc',
79
                'max:191',
80
                // Email may match existing email for this user, must be unique if changed.
81
                Rule::unique('users', 'email')->ignore($user->id)
82
            ]
83
        ]);
84
85
        if ($validData) {
86
            $user->update([
87
                'first_name' => $validData['first_name'],
88
                'last_name' => $validData['last_name'],
89
                'email' => $validData['email'],
90
            ]);
91
        }
92
93
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_personal'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

93
        return /** @scrutinizer ignore-call */ redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_personal'));
Loading history...
94
    }
95
96
    /**
97
     * Update password.
98
     *
99
     * @param  \Illuminate\Http\Request $request   Incoming request.
100
     * @param  \App\Models\User    $user Incoming User.
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function updatePassword(Request $request, User $user)
104
    {
105
        $validData = $request->validate([
106
            'current_password' => ['required', new PasswordCorrectRule],
107
            'new_password' => ['required', new PasswordFormatRule],
108
            'new_confirm_password' => ['required', 'same:new_password']
109
        ]);
110
111
        if ($validData) {
112
            $user->update(['password' => Hash::make($validData['new_password'])]);
113
        }
114
115
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_password'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

115
        return /** @scrutinizer ignore-call */ redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_password'));
Loading history...
116
    }
117
118
    /**
119
     * Update government information.
120
     *
121
     * @param  \Illuminate\Http\Request $request   Incoming request.
122
     * @param  \App\Models\User    $user Incoming User.
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function updateGovernment(Request $request, User $user)
126
    {
127
        $validData = $request->validate([
128
            'gov_email' => 'nullable|required_unless:department,0|email:rfc|max:191',
129
            Rule::unique('users', 'gov_email')->ignore($user->id)
130
        ]);
131
132
        if ($validData) {
133
            $user->update(['gov_email' => $validData['gov_email']]);
134
        }
135
136
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_government'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

136
        return /** @scrutinizer ignore-call */ redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_government'));
Loading history...
137
    }
138
139
    /**
140
     * Update contact information.
141
     *
142
     * @param \Illuminate\Http\Request $request Incoming request.
143
     * @param \App\Models\User $user Incoming User.
144
     * @return \Illuminate\Http\Response
145
     */
146
    public function updateContactPreferences(Request $request, User $user)
147
    {
148
        $validData = $request->validate([
149
            'contact_language' => [
150
                'required',
151
                'string',
152
                Rule::in(['en', 'fr']),
153
            ],
154
            'job_alerts' => 'boolean|in:0,1',
155
        ]);
156
157
        if ($validData) {
158
            $user->update(['contact_language' => $validData['contact_language']]);
159
            $user->update(['job_alerts' => $validData['job_alerts']]);
160
        }
161
162
        return redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_contact_preferences'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

162
        return /** @scrutinizer ignore-call */ redirect()->route(WhichPortal::prefixRoute('settings.edit'))->withSuccess(Lang::get('success.update_contact_preferences'));
Loading history...
163
    }
164
}
165