ldc-cocotte/app/Http/Controllers/Fo/Auth/ForgotPasswordController.php

86 lines
2.6 KiB
PHP
Raw Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Auth;
use App\Contracts\Executor;
2026-06-14 21:32:07 +02:00
use App\Exceptions\CustomerNotEmployedException;
use App\Cqs\Queries\Customers\GetCustomerByEmailQuery;
use App\Cqs\Queries\Customers\GetCustomerByEmailQueryInput;
2026-06-14 21:32:07 +02:00
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\ShopperId;
2026-06-14 21:32:07 +02:00
use Domain\Services\ShopperService;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
class ForgotPasswordController extends AuthController
{
use SendsPasswordResetEmails {
sendResetLinkEmail as traitSendResetLinkEmail;
}
public function __construct(
private readonly Executor $executor,
private readonly ShopperService $shopperService,
2026-06-14 21:32:07 +02:00
) {
$this->middleware('guest:frontend');
parent::__construct();
}
public function showLinkRequestForm(): View
{
return view('fo.auth.passwords.email');
}
public function sendResetLinkEmail(Request $request, GetCustomerByEmailQuery $query): JsonResponse | RedirectResponse
2026-06-14 21:32:07 +02:00
{
$this->validateEmail($request);
$shopperId = $this->executor->execute(
$query,
new GetCustomerByEmailQueryInput(new Email($request->string('email')->toString())),
);
2026-06-14 21:32:07 +02:00
if ($shopperId instanceof ShopperId) {
$shopper = $this->shopperService->getShopperFor($shopperId);
2026-06-14 21:32:07 +02:00
if (! $shopper || ! $shopper->isActive()) {
throw new CustomerNotEmployedException();
}
}
return $this->traitSendResetLinkEmail($request);
}
protected function sendResetLinkResponse(Request $request, string $response): JsonResponse | RedirectResponse
{
return $this->sendResponse($request->wantsJson());
}
protected function sendResetLinkFailedResponse(Request $request, string $response): JsonResponse | RedirectResponse
{
return $this->sendResponse($request->wantsJson());
}
protected function sendResponse(bool $wantsJson): JsonResponse | RedirectResponse
{
$message = trans(
'Please check your email, if we know you you should receive a reset link'
);
return $wantsJson
? new JsonResponse(['message' => $message], 202)
: back()->with('status', $message);
}
public function broker(): PasswordBroker
{
return Password::broker('customers');
}
}