<?php declare(strict_types=1);
namespace RhiemExtendedRegistration\Subscriber\Frontend;
use RhiemExtendedRegistration\Components\Services\RegistrationGroupService;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Customer implements EventSubscriberInterface
{
/**
* @var RegistrationGroupService
*/
private RegistrationGroupService $registrationGroupService;
/**
* Customer constructor.
*
* @param RegistrationGroupService $registrationGroupService
*/
public function __construct(RegistrationGroupService $registrationGroupService)
{
$this->registrationGroupService = $registrationGroupService;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'mapCustomerCustomFields'
];
}
/**
* @param DataMappingEvent $event
*/
public function mapCustomerCustomFields(DataMappingEvent $event): void
{
$data = $event->getInput();
$customer = $event->getOutput();
if($data instanceof DataBag && $data->has('Rhiem_Additional_Registration_Fields_Personal')){
$customer = $this->setCustomerCustomFields($data, $customer);
}
$event->setOutput($customer);
}
/**
* @param DataBag $data
* @param array|null $customer
*
* @return array
*/
private function setCustomerCustomFields(DataBag $data, ?array $customer): array
{
return $this->registrationGroupService->setCustomerAttributes(
$customer,
$data->get('Rhiem_Additional_Registration_Fields_Personal')
);
}
}