ldc-cocotte/app/Cqs/Actions/Auth/ResetCustomerPassword.php

57 lines
1.6 KiB
PHP
Raw Permalink Normal View History

<?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)
);
}
}