custom/plugins/DatoElectronicInvoicingSw6/src/Event/DataMappingEventListener.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SmartDato\DatoElectronicInvoicing\Event;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Shopware\Core\Framework\Event\DataMappingEvent;
  5. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  6. use SmartDato\DatoElectronicInvoicing\Bootstrap\SchemaManager;
  7. use SmartDato\DatoElectronicInvoicing\Validation\Constraint\MainValidator;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10.  * Class DataMappingEventListener
  11.  *
  12.  * @package SmartDato\DatoElectronicInvoicing\Event
  13.  */
  14. class DataMappingEventListener implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @inheritDoc
  18.      */
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => ['onMappingRegisterAddressBilling'],
  23.             CustomerEvents::MAPPING_ADDRESS_CREATE => ['onMappingAddressCreate']
  24.         ];
  25.     }
  26.     /**
  27.      * Event is dispatched during user registration and after collecting the parameters of the billing address.
  28.      *
  29.      * @param DataMappingEvent $event
  30.      */
  31.     public function onMappingRegisterAddressBilling(DataMappingEvent $event): void
  32.     {
  33.         $this->setCustomFieldsToAddressData($event);
  34.     }
  35.     /**
  36.      * Event is dispatched when collecting the parameters of the address to create or update.
  37.      *
  38.      * @param DataMappingEvent $event
  39.      */
  40.     public function onMappingAddressCreate(DataMappingEvent $event): void
  41.     {
  42.         $this->setCustomFieldsToAddressData($event);
  43.     }
  44.     /**
  45.      * Set custom fields to address data to create or update address
  46.      * todo - empty custom fields
  47.      * @param DataMappingEvent $event
  48.      */
  49.     private function setCustomFieldsToAddressData(DataMappingEvent $event): void
  50.     {
  51.         if (MainValidator::checkFiscalCodeDefinitionExistsInContext($event->getContext())) {
  52.             $this->setFiscalCodeToAddressData($event);
  53.         }
  54.         if (MainValidator::checkInvoicingFieldsDefinitionExistsInContext($event->getContext())) {
  55.             $this->setInvoicingFieldsToAddressData($event);
  56.         }
  57.     }
  58.     /**
  59.      * Set fiscal code custom fields to address data to create or update address
  60.      *
  61.      * @param DataMappingEvent $event
  62.      */
  63.     private function setFiscalCodeToAddressData(DataMappingEvent $event): void
  64.     {
  65.         $output $event->getOutput();
  66.         $output['customFields'][SchemaManager::DATO_CUSTOMER_ADDRESS_RECIPIENT_FISCAL] = $event->getInput()->get(MainValidator::DATO_FISCAL_CODE);
  67.         $event->setOutput($output);
  68.     }
  69.     /**
  70.      * Set invoicing fields custom fields to address data to create or update address
  71.      *
  72.      * @param DataMappingEvent $event
  73.      */
  74.     private function setInvoicingFieldsToAddressData(DataMappingEvent $event): void
  75.     {
  76.         $electronicInvoicingFields $event->getInput()->get(MainValidator::DATO_ELECTRONIC_INVOICING_FIELDS);
  77.         if (!$electronicInvoicingFields instanceof RequestDataBag) {
  78.             return;
  79.         }
  80.         $output $event->getOutput();
  81.         $recipientCode $electronicInvoicingFields->get(MainValidator::DATO_RECIPIENT_CODE);
  82.         if ($recipientCode !== null && $recipientCode !== '') {
  83.             $output['customFields'][SchemaManager::DATO_CUSTOMER_ADDRESS_RECIPIENT_CODE] = $recipientCode;
  84.         }
  85.         $recipientEmail $electronicInvoicingFields->get(MainValidator::DATO_RECIPIENT_EMAIL);
  86.         if ($recipientEmail !== null && $recipientEmail !== '') {
  87.             $output['customFields'][SchemaManager::DATO_CUSTOMER_ADDRESS_RECIPIENT_EMAIL] = $recipientEmail;
  88.         }
  89.         $event->setOutput($output);
  90.     }
  91. }