custom/plugins/FourtwosixSeparateStreetAndNumber/src/Storefront/Subscriber/AddressSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixSeparateStreetAndNumber\Storefront\Subscriber;
  3. use FourtwosixSeparateStreetAndNumber\Components\Constants\Constants;
  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.     public function __construct(
  17.         private readonly DataValidator $dataValidator
  18.         private readonly SystemConfigService $systemConfigService
  19.         private readonly RequestStack $requestStack)
  20.     {
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => Constants::DATA_MAPPING_EVENT,
  26.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => Constants::DATA_MAPPING_EVENT,
  27.             CustomerEvents::MAPPING_ADDRESS_CREATE => Constants::DATA_MAPPING_EVENT
  28.         ];
  29.     }
  30.     public function onDataMappingEvent(DataMappingEvent $event): void
  31.     {
  32.         $this->setAddressOutput($event);
  33.     }
  34.     private function setAddressOutput(DataMappingEvent $event): void
  35.     {
  36.         $output $event->getOutput();
  37.         $houseNumber $event->getInput()->get(Constants::ADDRESS_HOUSE_NUMBER_KEY);
  38.         $salesChannelId $event->getContext()->hasExtension(Constants::SALES_CHANNEL_ID) ? $event->getContext()->getExtension(Constants::SALES_CHANNEL_ID)->get(Constants::ID_KEY) : null;
  39.         $active $this->systemConfigService->get(Constants::PLUGIN_CONFIG.Constants::PLUGIN_ACTIVATION$salesChannelId);
  40.         if ($active) {
  41.             if (empty($salesChannelId) && !empty($this->requestStack) && !empty($request $this->requestStack->getCurrentRequest())) {
  42.                 $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  43.             }
  44.             if (!empty($salesChannelId) && $this->systemConfigService->get(Constants::PLUGIN_CONFIG.Constants::PLUGIN_CONFIG_ACTIVATION$salesChannelId)) {
  45.                 $definition = new DataValidationDefinition();
  46.                 $definition->add(Constants::ADDRESS_HOUSE_NUMBER_KEY, new NotBlank(), new Type(Constants::STRING_TYPE));
  47.                 $this->dataValidator->validate($event->getInput()->all(), $definition);
  48.             }
  49.             if (!$houseNumber) return;
  50.             $output[Constants::ADDRESS_STREET_KEY] = trim($output[Constants::ADDRESS_STREET_KEY]).Constants::EMPTY_SPACE.trim($houseNumber);
  51.             if (!array_key_exists(Constants::ADDRESS_CUSTOM_FIELD_KEY$output)) {
  52.                 $output[Constants::ADDRESS_CUSTOM_FIELD_KEY] = [
  53.                     Constants::ADDRESS_CUSTOM_FIELD_STREET_KEY => trim($event->getInput()->get(Constants::ADDRESS_STREET_KEY)),
  54.                     Constants::ADDRESS_CUSTOM_FIELD_HOUSE_NUMBER_KEY => trim($event->getInput()->get(Constants::ADDRESS_HOUSE_NUMBER_KEY))
  55.                 ];
  56.             } else {
  57.                 $output[Constants::ADDRESS_CUSTOM_FIELD_KEY][Constants::ADDRESS_CUSTOM_FIELD_STREET_KEY] = trim($event->getInput()->get(Constants::ADDRESS_STREET_KEY));
  58.                 $output[Constants::ADDRESS_CUSTOM_FIELD_KEY][Constants::ADDRESS_CUSTOM_FIELD_HOUSE_NUMBER_KEY] = trim($event->getInput()->get(Constants::ADDRESS_HOUSE_NUMBER_KEY));
  59.             }
  60.             $event->setOutput($output);
  61.         }
  62.     }
  63. }