UpdateUserProfileInformation::update()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 2
dl 0
loc 20
ccs 0
cts 14
cp 0
crap 20
rs 9.7666
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
10
class UpdateUserProfileInformation implements UpdatesUserProfileInformation
11
{
12
    /**
13
     * Validate and update the given user's profile information.
14
     *
15
     * @param  mixed  $user
16
     * @param  array  $input
17
     * @return void
18
     */
19
    public function update($user, array $input)
20
    {
21
        Validator::make($input, [
22
            'name' => ['required', 'string', 'max:255'],
23
            'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
24
            'photo' => ['nullable', 'image', 'max:1024'],
25
        ])->validateWithBag('updateProfileInformation');
26
27
        if (isset($input['photo'])) {
28
            $user->updateProfilePhoto($input['photo']);
29
        }
30
31
        if ($input['email'] !== $user->email &&
32
            $user instanceof MustVerifyEmail) {
33
            $this->updateVerifiedUser($user, $input);
34
        } else {
35
            $user->forceFill([
36
                'name' => $input['name'],
37
                'email' => $input['email'],
38
            ])->save();
39
        }
40
    }
41
42
    /**
43
     * Update the given verified user's profile information.
44
     *
45
     * @param  mixed  $user
46
     * @param  array  $input
47
     * @return void
48
     */
49
    protected function updateVerifiedUser($user, array $input)
50
    {
51
        $user->forceFill([
52
            'name' => $input['name'],
53
            'email' => $input['email'],
54
            'email_verified_at' => null,
55
        ])->save();
56
57
        $user->sendEmailVerificationNotification();
58
    }
59
}
60