ldc-cocotte/app/Http/Controllers/Fo/Auth/ForgotPasswordController.php
Jérémy Munsch 74ab9b3041
refactor: reorganize imports across multiple controllers and jobs
This commit refactors the import statements in various controllers and jobs
to improve code organization and readability. Unused imports were removed,
and necessary imports were added to ensure proper functionality.
2026-11-10 18:15:32 +01:00

85 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Auth;
use App\Contracts\Executor;
use App\Cqs\Queries\Customers\GetCustomerByEmailQuery;
use App\Cqs\Queries\Customers\GetCustomerByEmailQueryInput;
use App\Exceptions\CustomerNotEmployedException;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\ShopperId;
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,
) {
$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
{
$this->validateEmail($request);
$shopperId = $this->executor->execute(
$query,
new GetCustomerByEmailQueryInput(new Email($request->string('email')->toString())),
);
if ($shopperId instanceof ShopperId) {
$shopper = $this->shopperService->getShopperFor($shopperId);
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');
}
}