<?php declare(strict_types=1);namespace SmartDato\DatoElectronicInvoicing\Event;use Shopware\Core\Checkout\Customer\CustomerEvents;use Shopware\Core\Framework\Event\DataMappingEvent;use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;use SmartDato\DatoElectronicInvoicing\Bootstrap\SchemaManager;use SmartDato\DatoElectronicInvoicing\Validation\Constraint\MainValidator;use Symfony\Component\EventDispatcher\EventSubscriberInterface;/** * Class DataMappingEventListener * * @package SmartDato\DatoElectronicInvoicing\Event */class DataMappingEventListener implements EventSubscriberInterface{ /** * @inheritDoc */ public static function getSubscribedEvents(): array { return [ CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => ['onMappingRegisterAddressBilling'], CustomerEvents::MAPPING_ADDRESS_CREATE => ['onMappingAddressCreate'] ]; } /** * Event is dispatched during user registration and after collecting the parameters of the billing address. * * @param DataMappingEvent $event */ public function onMappingRegisterAddressBilling(DataMappingEvent $event): void { $this->setCustomFieldsToAddressData($event); } /** * Event is dispatched when collecting the parameters of the address to create or update. * * @param DataMappingEvent $event */ public function onMappingAddressCreate(DataMappingEvent $event): void { $this->setCustomFieldsToAddressData($event); } /** * Set custom fields to address data to create or update address * todo - empty custom fields * @param DataMappingEvent $event */ private function setCustomFieldsToAddressData(DataMappingEvent $event): void { if (MainValidator::checkFiscalCodeDefinitionExistsInContext($event->getContext())) { $this->setFiscalCodeToAddressData($event); } if (MainValidator::checkInvoicingFieldsDefinitionExistsInContext($event->getContext())) { $this->setInvoicingFieldsToAddressData($event); } } /** * Set fiscal code custom fields to address data to create or update address * * @param DataMappingEvent $event */ private function setFiscalCodeToAddressData(DataMappingEvent $event): void { $output = $event->getOutput(); $output['customFields'][SchemaManager::DATO_CUSTOMER_ADDRESS_RECIPIENT_FISCAL] = $event->getInput()->get(MainValidator::DATO_FISCAL_CODE); $event->setOutput($output); } /** * Set invoicing fields custom fields to address data to create or update address * * @param DataMappingEvent $event */ private function setInvoicingFieldsToAddressData(DataMappingEvent $event): void { $electronicInvoicingFields = $event->getInput()->get(MainValidator::DATO_ELECTRONIC_INVOICING_FIELDS); if (!$electronicInvoicingFields instanceof RequestDataBag) { return; } $output = $event->getOutput(); $recipientCode = $electronicInvoicingFields->get(MainValidator::DATO_RECIPIENT_CODE); if ($recipientCode !== null && $recipientCode !== '') { $output['customFields'][SchemaManager::DATO_CUSTOMER_ADDRESS_RECIPIENT_CODE] = $recipientCode; } $recipientEmail = $electronicInvoicingFields->get(MainValidator::DATO_RECIPIENT_EMAIL); if ($recipientEmail !== null && $recipientEmail !== '') { $output['customFields'][SchemaManager::DATO_CUSTOMER_ADDRESS_RECIPIENT_EMAIL] = $recipientEmail; } $event->setOutput($output); }}