custom/plugins/AcrisSeparateStreetCS/src/Storefront/Subscriber/AddressSubscriber.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\SeparateStreet\Storefront\Subscriber;
  3. use Acris\SeparateStreet\Components\Service\SeparateStreetService;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  5. use Shopware\Core\Framework\Event\DataMappingEvent;
  6. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  7. use Shopware\Core\Framework\Validation\DataValidator;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Validator\Constraints\NotBlank;
  11. use Symfony\Component\Validator\Constraints\Type;
  12. class AddressSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var DataValidator
  16.      */
  17.     private $dataValidator;
  18.     /**
  19.      * @var SystemConfigService
  20.      */
  21.     private $systemConfigService;
  22.     /**
  23.      * @var SeparateStreetService
  24.      */
  25.     private $separateStreetService;
  26.     public function __construct(
  27.         DataValidator $dataValidator,
  28.         SystemConfigService $systemConfigService,
  29.         SeparateStreetService $separateStreetService
  30.     ) {
  31.         $this->dataValidator $dataValidator;
  32.         $this->systemConfigService $systemConfigService;
  33.         $this->separateStreetService $separateStreetService;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onDataMappingEvent',
  39.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onDataMappingEvent',
  40.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onDataMappingEvent'
  41.         ];
  42.     }
  43.     /**
  44.      * @param DataMappingEvent $event
  45.      */
  46.     public function onDataMappingEvent(DataMappingEvent $event): void
  47.     {
  48.         $this->setAddressOutput($event);
  49.     }
  50.     /**
  51.      * @param DataMappingEvent $event
  52.      */
  53.     private function setAddressOutput(DataMappingEvent $event): void
  54.     {
  55.         $output $event->getOutput();
  56.         $houseNumber $event->getInput()->get('houseNumber');
  57.         $salesChannelId $event->getContext()->hasExtension('AcrisSeparateStreetSalesChannel') ? $event->getContext()->getExtension('AcrisSeparateStreetSalesChannel')->get('id') : null;
  58.         if ($this->separateStreetService->skipValidation($event->getInput()->all()) !== true && !empty($salesChannelId) && $this->systemConfigService->get('AcrisSeparateStreetCS.config.activateHouseNumberFieldValidation'$salesChannelId)) {
  59.             $definition = new DataValidationDefinition();
  60.             $definition->add('houseNumber', new NotBlank(), new Type('string'));
  61.             $this->dataValidator->validate($event->getInput()->all(), $definition);
  62.         }
  63.         if (!$houseNumber) return;
  64.         $output['street'] = trim($output['street'])." ".trim($houseNumber);
  65.         if (!array_key_exists('customFields'$output)) {
  66.             $output['customFields'] = [
  67.                 'acris_separate_street_address_street' => trim($event->getInput()->get('street')),
  68.                 'acris_separate_street_address_house_number' => trim($event->getInput()->get('houseNumber'))
  69.             ];
  70.         } else {
  71.             $output['customFields']['acris_separate_street_address_street'] = trim($event->getInput()->get('street'));
  72.             $output['customFields']['acris_separate_street_address_house_number'] = trim($event->getInput()->get('houseNumber'));
  73.         }
  74.         $event->setOutput($output);
  75.     }
  76. }