|
| 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 | + 'first_name' => ['required', 'string', 'max:255'], |
| 23 | + 'last_name' => ['required', 'string', 'max:255'], |
| 24 | + 'mobile' => ['string', 'digits:10', Rule::unique('users')->ignore($user->id)], |
| 25 | + 'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->ignore($user->id),], |
| 26 | + ])->validateWithBag('updateProfileInformation'); |
| 27 | + |
| 28 | + if ($input['email'] !== $user->email && |
| 29 | + $user instanceof MustVerifyEmail) { |
| 30 | + $this->updateVerifiedUser($user, $input); |
| 31 | + } else { |
| 32 | + $user->forceFill([ |
| 33 | + 'first_name' => $input['first_name'], |
| 34 | + 'last_name' => $input['last_name'], |
| 35 | + 'email' => $input['email'], |
| 36 | + 'mobile' => $input['mobile'], |
| 37 | + ])->save(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Update the given verified user's profile information. |
| 43 | + * |
| 44 | + * @param mixed $user |
| 45 | + * @param array $input |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + protected function updateVerifiedUser($user, array $input) |
| 49 | + { |
| 50 | + $user->forceFill([ |
| 51 | + 'first_name' => $input['first_name'], |
| 52 | + 'last_name' => $input['last_name'], |
| 53 | + 'email' => $input['email'], |
| 54 | + 'mobile' => $input['mobile'], |
| 55 | + 'email_verified_at' => null, |
| 56 | + ])->save(); |
| 57 | + |
| 58 | + $user->sendEmailVerificationNotification(); |
| 59 | + } |
| 60 | +} |
0 commit comments