ldc-cocotte/app/Cqs/Actions/Auth/ResetCustomerPassword.php
Jérémy Munsch 47b0c16857
feat(auth): implement ResetCustomerPassword action and related classes
Adds functionality for resetting customer passwords, including input and output handling.
2026-11-15 19:57:38 +01:00

56 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Cqs\Actions\Auth;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use Domain\Services\ShopperService;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<
* \App\Cqs\Actions\Auth\ResetCustomerPasswordInput,
* \App\Cqs\Actions\Auth\ResetCustomerPasswordOutput
* >
*/
final class ResetCustomerPassword implements Action
{
public function __construct(
private readonly ShopperService $shopperService,
private readonly AppCustomerAuthService $authService,
) {
}
/**
* @param \App\Cqs\Actions\Auth\ResetCustomerPasswordInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): ResetCustomerPasswordOutput
{
Assert::isInstanceOf($input, ResetCustomerPasswordInput::class);
$shopper = $this->shopperService->getShopperFor($input->customer->getId());
if (! $shopper || ! $shopper->isActive()) {
return ResetCustomerPasswordOutput::notEmployed();
}
$result = $this->authService->resetPasswordFor($input->customer, $input->password);
if ($result instanceof ValidationException) {
return ResetCustomerPasswordOutput::invalidPassword($result->getMessage());
}
$input->customer->setRememberToken(Str::random(60));
$input->customer->saveOrFail();
return ResetCustomerPasswordOutput::success(
$this->authService->hasVerifiedEmail($input->customer)
);
}
}