ldc-cocotte/app/Http/Controllers/Fo/Auth/ResetPasswordController.php
Jérémy Munsch 66e406499d
refactor(auth): remove parent constructor calls in auth controllers
This change simplifies the constructors in the ForgotPasswordController, LoginController, RegisterController, and ResetPasswordController by removing unnecessary parent constructor calls.
2026-11-15 19:41:27 +01:00

95 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Auth;
use App\Exceptions\CustomerNotEmployedException;
use App\Models\Customer;
use Domain\Services\ShopperService;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Webmozart\Assert\Assert;
class ResetPasswordController extends AuthController
{
use ResetsPasswords;
public function __construct(
private readonly AppCustomerAuthService $authService,
private readonly ShopperService $shopperService,
) {
}
public function showResetForm(Request $request): View
{
$token = $request->route()?->parameter('token');
return view('fo.auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
protected function resetPassword(CanResetPassword $user, string $password): void
{
Assert::isInstanceOf($user, Customer::class);
$shopper = $this->shopperService->getShopperFor($user->getId());
if (! $shopper || ! $shopper->isActive()) {
throw new CustomerNotEmployedException();
}
$this->setUserPassword($user, $password);
$user->setRememberToken(Str::random(60));
$user->save();
event(new PasswordReset($user));
if ($this->authService->hasVerifiedEmail($user)) {
$this->guard()->login($user);
}
}
protected function setUserPassword(Customer $customer, string $password): void
{
$result = $this->authService->resetPasswordFor($customer, $password);
if ($result instanceof ValidationException) {
throw ValidationException::withMessages([
'error' => sprintf(
'%s : %s',
trans('validation.attributes.password'),
$result->getMessage()
),
]);
}
}
public function broker(): PasswordBroker
{
return Password::broker('customers');
}
protected function guard(): StatefulGuard
{
return Auth::guard('frontend');
}
protected function redirectTo(): string
{
return route('fo.pages.home');
}
}