39 lines
1.0 KiB
PHP
39 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Exceptions\UserNotFoundException;
|
|
use App\Models\User;
|
|
use App\Services\OTPService;
|
|
use App\Services\TwilioService;
|
|
use Twilio\Exceptions\TwilioException;
|
|
|
|
final readonly class PasswordResetAction
|
|
{
|
|
public function __construct(
|
|
private SendPasswordResetMailAction $mailAction,
|
|
private OTPService $otpService,
|
|
private TwilioService $twilioService
|
|
) {}
|
|
|
|
/**
|
|
* @throws \Throwable
|
|
*/
|
|
public function execute(array $data): void
|
|
{
|
|
$user = User::where('email', $data['email'])->first();
|
|
throw_if(! $user, new UserNotFoundException('User not found'));
|
|
|
|
$otp = $this->otpService->generate($user);
|
|
$this->mailAction->execute($user->email, $otp);
|
|
|
|
if ($user?->type->phone !== null) {
|
|
try {
|
|
$this->twilioService->sendSms($user->type->phone, "Your OTP is $otp");
|
|
} catch (TwilioException $e) {
|
|
\Log::error('SMS send failed', [$e->getMessage()]);
|
|
}
|
|
}
|
|
}
|
|
}
|