Adds the ability for customers to update their passwords through a new action and input/output classes.
54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Crm;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Services\CrmService;
|
|
use Domain\Models\ValueObjects\CrmCustomer;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<\App\Actions\Crm\UpdateCustomerPasswordInput, \App\Actions\Crm\UpdateCustomerPasswordOutput>
|
|
*/
|
|
final class UpdateCustomerPassword implements Action
|
|
{
|
|
public function __construct(
|
|
private readonly AppCustomerAuthService $authService,
|
|
private readonly CrmService $crmService
|
|
) {
|
|
}
|
|
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): UpdateCustomerPasswordOutput
|
|
{
|
|
Assert::isInstanceOf($input, UpdateCustomerPasswordInput::class);
|
|
|
|
$hashcode = $this->authService->getCustomerAsMandatory()->hashcode;
|
|
|
|
$crmCustomer = $this->crmService->getCustomerFromHashCode(
|
|
$hashcode
|
|
);
|
|
|
|
if ($crmCustomer instanceof CrmCustomer === false) {
|
|
return new UpdateCustomerPasswordOutput(false);
|
|
}
|
|
|
|
try {
|
|
$result = $this->crmService->changePassword(
|
|
$crmCustomer,
|
|
$input->currentPassword,
|
|
$input->newPassword,
|
|
);
|
|
|
|
return new UpdateCustomerPasswordOutput($result);
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
|
|
return new UpdateCustomerPasswordOutput(false);
|
|
}
|
|
}
|
|
}
|