Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| UpdateUserProfileAction | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
| execute | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Organizations\Application\Actions; |
| 6 | |
| 7 | use App\Models\User; |
| 8 | use Illuminate\Support\Facades\Hash; |
| 9 | use Illuminate\Validation\ValidationException; |
| 10 | |
| 11 | final class UpdateUserProfileAction |
| 12 | { |
| 13 | /** |
| 14 | * @param array{ |
| 15 | * name: string, |
| 16 | * email: string, |
| 17 | * phone?: string|null, |
| 18 | * creci?: string|null, |
| 19 | * password?: string|null, |
| 20 | * current_password?: string|null, |
| 21 | * } $data |
| 22 | */ |
| 23 | public function execute(User $user, array $data): User |
| 24 | { |
| 25 | if (! empty($data['password'])) { |
| 26 | if (empty($data['current_password']) || ! Hash::check((string) $data['current_password'], (string) $user->password)) { |
| 27 | throw ValidationException::withMessages([ |
| 28 | 'current_password' => ['Senha atual incorreta.'], |
| 29 | ]); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | $emailTaken = User::query() |
| 34 | ->where('email', $data['email']) |
| 35 | ->whereKeyNot($user->id) |
| 36 | ->exists(); |
| 37 | |
| 38 | if ($emailTaken) { |
| 39 | throw ValidationException::withMessages([ |
| 40 | 'email' => ['Este e-mail já está em uso.'], |
| 41 | ]); |
| 42 | } |
| 43 | |
| 44 | $payload = [ |
| 45 | 'name' => trim($data['name']), |
| 46 | 'email' => strtolower(trim($data['email'])), |
| 47 | 'phone' => $data['phone'] ?? null, |
| 48 | 'creci' => $data['creci'] ?? null, |
| 49 | ]; |
| 50 | |
| 51 | if (! empty($data['password'])) { |
| 52 | $payload['password'] = $data['password']; |
| 53 | } |
| 54 | |
| 55 | $user->forceFill($payload)->save(); |
| 56 | |
| 57 | return $user->fresh() ?? $user; |
| 58 | } |
| 59 | } |