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

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\PlatformRequest;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Validator\Constraints\NotBlank;
  13. use Symfony\Component\Validator\Constraints\Type;
  14. class AddressSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var DataValidator
  18.      */
  19.     private $dataValidator;
  20.     /**
  21.      * @var SystemConfigService
  22.      */
  23.     private $systemConfigService;
  24.     /**
  25.      * @var SeparateStreetService
  26.      */
  27.     private $separateStreetService;
  28.     /**
  29.      * @var RequestStack
  30.      */
  31.     private $requestStack;
  32.     public function __construct(
  33.         DataValidator $dataValidator,
  34.         SystemConfigService $systemConfigService,
  35.         SeparateStreetService $separateStreetService,
  36.         RequestStack $requestStack
  37.     ) {
  38.         $this->dataValidator $dataValidator;
  39.         $this->systemConfigService $systemConfigService;
  40.         $this->separateStreetService $separateStreetService;
  41.         $this->requestStack $requestStack;
  42.     }
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onDataMappingEvent',
  47.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onDataMappingEvent',
  48.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onDataMappingEvent'
  49.         ];
  50.     }
  51.     /**
  52.      * @param DataMappingEvent $event
  53.      */
  54.     public function onDataMappingEvent(DataMappingEvent $event): void
  55.     {
  56.         $this->setAddressOutput($event);
  57.     }
  58.     /**
  59.      * @param DataMappingEvent $event
  60.      */
  61.     private function setAddressOutput(DataMappingEvent $event): void
  62.     {
  63.         $output $event->getOutput();
  64.         $houseNumber $event->getInput()->get('houseNumber');
  65.         $salesChannelId $event->getContext()->hasExtension('AcrisSeparateStreetSalesChannel') ? $event->getContext()->getExtension('AcrisSeparateStreetSalesChannel')->get('id') : null;
  66.         if (empty($salesChannelId) && !empty($this->requestStack) && !empty($request $this->requestStack->getCurrentRequest())) {
  67.             $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  68.         }
  69.         if ($this->separateStreetService->skipValidation($event->getInput()->all()) !== true && !empty($salesChannelId) && $this->systemConfigService->get('AcrisSeparateStreetCS.config.activateHouseNumberFieldValidation'$salesChannelId)) {
  70.             $definition = new DataValidationDefinition();
  71.             $definition->add('houseNumber', new NotBlank(), new Type('string'));
  72.             $this->dataValidator->validate($event->getInput()->all(), $definition);
  73.         }
  74.         if (!$houseNumber) return;
  75.         $output['street'] = trim($output['street'])." ".trim($houseNumber);
  76.         if (!array_key_exists('customFields'$output)) {
  77.             $output['customFields'] = [
  78.                 'acris_separate_street_address_street' => trim($event->getInput()->get('street')),
  79.                 'acris_separate_street_address_house_number' => trim($event->getInput()->get('houseNumber'))
  80.             ];
  81.         } else {
  82.             $output['customFields']['acris_separate_street_address_street'] = trim($event->getInput()->get('street'));
  83.             $output['customFields']['acris_separate_street_address_house_number'] = trim($event->getInput()->get('houseNumber'));
  84.         }
  85.         $event->setOutput($output);
  86.     }
  87. }