custom/static-plugins/SamsonCustomer/src/Subscriber/AddressSubscriber.php line 68

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2021
  11.  *
  12.  ***/
  13. use Samson\CustomFieldSet\Constants\CustomerAddressCustomFieldConstants;
  14. use Samson\Service\AddressServiceInterface;
  15. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  16. use Shopware\Core\Checkout\Customer\CustomerEvents;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  18. use Shopware\Core\Framework\Event\DataMappingEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class AddressSubscriber implements EventSubscriberInterface
  21. {
  22.     private AddressServiceInterface $customerAddressService;
  23.     public function __construct(AddressServiceInterface $customerAddressService)
  24.     {
  25.         $this->customerAddressService $customerAddressService;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'setAddressOutput',
  31.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'setAddressOutput',
  32.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'setAddressOutput',
  33.             CustomerEvents::CUSTOMER_ADDRESS_LOADED_EVENT => 'onCustomAddressLoaded'
  34.         ];
  35.     }
  36.     public function onCustomAddressLoaded(EntityLoadedEvent $event): void
  37.     {
  38.         /** @var CustomerAddressEntity $address */
  39.         foreach ($event->getEntities() as $address) {
  40.             $customFields $address->getCustomFields();
  41.             if (!isset($customFields[CustomerAddressCustomFieldConstants::CUSTOM_FIELD_HOUSE_NUMBER])) {
  42.                 $street $address->getStreet();
  43.                 $houseNumberAndStreet $this->customerAddressService->separateHouseNumberFromStreet($street);
  44.                 if (!array_key_exists('houseNumber'$houseNumberAndStreet)) {
  45.                     return;
  46.                 }
  47.                 $street $houseNumberAndStreet['street'];
  48.                 $houseNumber $houseNumberAndStreet['houseNumber'];
  49.                 $customFields = [
  50.                     CustomerAddressCustomFieldConstants::CUSTOM_FIELD_STREET => $street,
  51.                     CustomerAddressCustomFieldConstants::CUSTOM_FIELD_HOUSE_NUMBER => $houseNumber
  52.                 ];
  53.                 $address->setCustomFields($customFields);
  54.             }
  55.         }
  56.     }
  57.     public function setAddressOutput(DataMappingEvent $event): void
  58.     {
  59.         $output $event->getOutput();
  60.         $input $event->getInput();
  61.         $houseNumber trim($input->get('houseNumber'''));
  62.         $street trim($input->get('street'''));
  63.         $houseNumberAndStreet $this->customerAddressService->separateHouseNumberFromStreet($street);
  64.         if (!array_key_exists('houseNumber'$houseNumberAndStreet) && empty($houseNumber)) {
  65.             return;
  66.         }
  67.         if (array_key_exists('houseNumber'$houseNumberAndStreet)) {
  68.             $houseNumber $houseNumber === $houseNumberAndStreet['houseNumber']
  69.                 ? $houseNumber
  70.                 $houseNumberAndStreet['houseNumber'];
  71.         }
  72.         $street $houseNumberAndStreet['street'];
  73.         $output['street'] = $street " " $houseNumber;
  74.         $output['customFields'] = [
  75.             CustomerAddressCustomFieldConstants::CUSTOM_FIELD_STREET => $street,
  76.             CustomerAddressCustomFieldConstants::CUSTOM_FIELD_HOUSE_NUMBER => $houseNumber
  77.         ];
  78.         $event->setOutput($output);
  79.     }
  80. }