dealhub/app/Actions/PasswordResetAction.php
kusowl 0b631f6049 feature(password reset): send otp via SMS
- use twilio to send otp via test sms
2026-01-29 18:50:53 +05:30

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()]);
}
}
}
}