Adds functionality for resetting customer passwords, including input and output handling.
56 lines
1.6 KiB
PHP
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)
|
|
);
|
|
}
|
|
}
|