Passed
Branch main (b90ec4)
by Thierry
20:23 queued 14:04
created

UpdateUserProfileInformation::update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 2
nop 2
dl 0
loc 22
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace App\Actions\Fortify;
4
5
use Illuminate\Contracts\Auth\MustVerifyEmail;
6
use Illuminate\Support\Facades\Validator;
7
use Illuminate\Validation\Rule;
8
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
9
use Siak\Tontine\Model\User;
10
11
class UpdateUserProfileInformation implements UpdatesUserProfileInformation
12
{
13
    /**
14
     * Validate and update the given user's profile information.
15
     *
16
     * @param  array<string, string>  $input
17
     */
18
    public function update(User $user, array $input): void
19
    {
20
        Validator::make($input, [
21
            'name' => ['required', 'string', 'max:255'],
22
23
            'email' => [
24
                'required',
25
                'string',
26
                'email',
27
                'max:255',
28
                Rule::unique('users')->ignore($user->id),
29
            ],
30
        ])->validateWithBag('updateProfileInformation');
31
32
        if ($input['email'] !== $user->email &&
33
            $user instanceof MustVerifyEmail) {
34
            $this->updateVerifiedUser($user, $input);
35
        } else {
36
            $user->forceFill([
37
                'name' => $input['name'],
38
                'email' => $input['email'],
39
            ])->save();
40
        }
41
    }
42
43
    /**
44
     * Update the given verified user's profile information.
45
     *
46
     * @param  array<string, string>  $input
47
     */
48
    protected function updateVerifiedUser(User $user, array $input): void
49
    {
50
        $user->forceFill([
51
            'name' => $input['name'],
52
            'email' => $input['email'],
53
            'email_verified_at' => null,
54
        ])->save();
55
56
        $user->sendEmailVerificationNotification();
57
    }
58
}
59