ldc-boutique-cnc-sable/app/Actions/Crm/UpdateCustomer.php
Jérémy Munsch b8a0f16efa
fix(Crm): update customer input handling in UpdateCustomer action
Refactor input validation and data preparation for customer updates.
2026-10-27 18:57:46 +01:00

62 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions\Crm;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Services\CrmService;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<\App\Actions\Crm\UpdateCustomerInput, \App\Actions\Crm\UpdateCustomerOutput>
*/
final class UpdateCustomer implements Action
{
public function __construct(
private readonly AppCustomerAuthService $authService,
private readonly CrmService $crmService
) {
}
#[\Override]
public function __invoke(ActionInput $input): UpdateCustomerOutput
{
Assert::isInstanceOf($input, UpdateCustomerInput::class);
$customer = $this->authService->getCustomerAsMandatory();
try {
/** @var array{firstname:string,lastname:string,birth_date:string,phone?:string,address?:string,postcode:string,city:string,opt_in?:bool} $data */
$data = [
'firstname' => $input->firstname,
'lastname' => $input->lastname,
'birth_date' => $input->birthDate,
'postcode' => $input->postcode,
'city' => $input->city,
'opt_in' => $input->optIn,
];
if ($input->phone !== null) {
$data['phone'] = $input->phone;
}
if ($input->address !== null) {
$data['address'] = $input->address;
}
$result = $this->crmService->updateCustomerInfos(
$customer,
$data
);
return new UpdateCustomerOutput($result);
} catch (\Throwable $e) {
report($e);
return new UpdateCustomerOutput(false);
}
}
}