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

UpdateUserProfile::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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