src/Core/Subscriber/CustomerAddressExtensionSubscriber.php line 126

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bodymed\Webshop\Core\Subscriber;
  4. use Bodymed\Interop\Enum\Noms\Sequence\SequenceType;
  5. use Bodymed\Interop\Struct\Noms\Customer\CustomerAddress;
  6. use Bodymed\Interop\Util\Address\StreetHouseNumberTool;
  7. use Bodymed\Webshop\Constants;
  8. use Bodymed\Webshop\Core\Checkout\Customer\NomsCustomerAddressExtension\NomsCustomerAddressExtensionEntity;
  9. use Bodymed\Webshop\Core\Checkout\Customer\NomsSequence\NomsSequenceService;
  10. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  11. use Shopware\Core\Checkout\Customer\CustomerEvents;
  12. use Shopware\Core\Framework\Context;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use Shopware\Core\Framework\Event\DataMappingEvent;
  19. use Shopware\Core\Framework\Uuid\Uuid;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class CustomerAddressExtensionSubscriber implements EventSubscriberInterface
  22. {
  23.     public function __construct(
  24.         private EntityRepository $nomsCustomerAddressExtensionRepository,
  25.         private EntityRepository $customerAddressRepository,
  26.         private NomsSequenceService $nomsSequenceService
  27.     ) {
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             //CustomerRegisterEvent::class => 'onCreateCustomer',
  33.             #AddressController::class => 'onSaveAddress',
  34.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onMappingAddress',
  35.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onMappingAddress',
  36.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onMappingAddress',
  37.             CustomerEvents::CUSTOMER_ADDRESS_LOADED_EVENT => 'onCustomerAddressLoaded',
  38.             'customer_address.written' => 'onCustomerAddressWritten',
  39.         ];
  40.     }
  41.     public function onCustomerAddressWritten(EntityWrittenEvent $event)
  42.     {
  43.         $context $event->getContext();
  44.         $customerAddressIds $event->getIds();
  45.         $criteria = (new Criteria($customerAddressIds));
  46.         $customerAddresses $this->customerAddressRepository->search($criteria$context);
  47.         $upsertData = [];
  48.         /* @var $customerAddress CustomerAddressEntity */
  49.         foreach ($customerAddresses as  $customerAddress) {
  50.             $customerAddressExtension $customerAddress->getExtension(Constants::ADDRESS_EXTENSION_NOMS_EXTENSION_KEY);
  51.             $customerId $customerAddress->getCustomerId();
  52.             $customerAddressId $customerAddress->getId();
  53.             if (!$customerAddressExtension instanceof NomsCustomerAddressExtensionEntity) {
  54.                 $customerAddressExtensionId Uuid::randomHex();
  55.                 $nomsStreetAddress StreetHouseNumberTool::buildNomsHouseNumberFromStreetAndNumber($customerAddress->getStreet());
  56.                 $plainStreet $nomsStreetAddress->street;
  57.                 $houseNumber $nomsStreetAddress->houseNumber;
  58.                 $nomsPhoneId null;
  59.                 if (empty($plainStreet)) {
  60.                     continue;
  61.                 }
  62.                 if ($this->isAcceptablePhoneNumber($customerAddress->getPhoneNumber())) {
  63.                     $nomsPhoneId $this->generateNomsPhoneId($context);
  64.                 }
  65.                 $nomsAddressId $this->generateNomsAddressId($context);
  66.                 $nomsContactId $this->generateNomsContactId($context);
  67.             } else {
  68.                 $customerAddressExtensionId $customerAddressExtension->getId();
  69.                 $plainStreet $customerAddressExtension->getStreet();
  70.                 $houseNumber $customerAddressExtension->getStreetnumber();
  71.                 $nomsPhoneId $customerAddressExtension->getNomsPhoneId();
  72.                 $nomsAddressId $customerAddressExtension->getNomsAddressId();
  73.                 $nomsContactId $customerAddressExtension->getNomsContactId();
  74.                 $mergedStreetAndNumber StreetHouseNumberTool::mergeStreetAndNumber($plainStreet$houseNumber);
  75.                 /* Wenn die Straße geändert wurde und die Änderungen nicht mehr zum gesplitteten Wert passen, müssen diese auch geändert werden */
  76.                 if ($mergedStreetAndNumber !== $customerAddress->getStreet()) {
  77.                     $nomsStreetAddress StreetHouseNumberTool::buildNomsHouseNumberFromStreetAndNumber($customerAddress->getStreet());
  78.                     $plainStreet $nomsStreetAddress->street;
  79.                     $houseNumber $nomsStreetAddress->houseNumber;
  80.                 }
  81.                 if (null === $nomsPhoneId) {
  82.                     $nomsPhoneId $this->generateNomsPhoneId($context);
  83.                 }
  84.                 if (null === $nomsAddressId) {
  85.                     $nomsAddressId $this->generateNomsAddressId($context);
  86.                 }
  87.                 if (null === $nomsContactId) {
  88.                     $nomsContactId $this->generateNomsContactId($context);
  89.                 }
  90.             }
  91.             $upsertData[] = [
  92.                 'id' => $customerAddressExtensionId,
  93.                 'customerId' => $customerId,
  94.                 'customerAddressId' => $customerAddressId,
  95.                 'street' => $plainStreet,
  96.                 'streetnumber' => empty($houseNumber) ? ' ' $houseNumber,
  97.                 'nomsAddressId' => $nomsAddressId,
  98.                 'nomsContactId' => $nomsContactId,
  99.                 'nomsPhoneId' => $nomsPhoneId,
  100.             ];
  101.         }
  102.         $this->nomsCustomerAddressExtensionRepository->upsert($upsertData$context);
  103.     }
  104.     public function onCustomerAddressLoaded(EntityLoadedEvent $event): void
  105.     {
  106.         $context $event->getContext();
  107.         $addresses $event->getEntities();
  108.         /* @var $address CustomerAddressEntity */
  109.         foreach ($addresses as $address) {
  110.             $id $address->getId();
  111.             /* @todo Wird nicht mehr benötigt */
  112.             /*$criteria = (new Criteria())->addFilter(
  113.                 new EqualsFilter('customerAddressId', $id),
  114.             );
  115.             $result = $this->nomsCustomerAddressExtensionRepository->search($criteria, $context);
  116.             $nomsCustomerAddressExtension = $result->first();
  117.             if ($nomsCustomerAddressExtension instanceof NomsCustomerAddressExtensionEntity) {
  118.                 $address->addExtension(
  119.                     Constants::ADDRESS_EXTENSION_NOMS_EXTENSION_KEY,
  120.                     $nomsCustomerAddressExtension
  121.                 );
  122.             }*/
  123.             /*
  124.             if($address->getCustomFields()){
  125.                 if(isset($address->getCustomFields()['nomsStreet']) and $address->getExtensions()['nomsCustomerAddress'] == null){
  126.                     $this->nomsCustomerAddressRepository->create([
  127.                         [
  128.                             'customerAddressId' => $address->getId(),
  129.                             'customerId' => $address->getCustomerId(),
  130.                             'street' => $address->getCustomFields()['nomsStreet'],
  131.                             'streetnumber' => $address->getCustomFields()['nomsStreetnumber']
  132.                         ]
  133.                     ], $context);
  134.                     $criteria = (new Criteria())->addFilter(
  135.                         new EqualsFilter('customerAddressId', $id),
  136.                     );
  137.                     $result = $this->nomsCustomerAddressRepository->search($criteria, $context);
  138.                     $nomsCustomerAddress = $result->first();
  139.                     $address->addExtension(
  140.                         'nomsCustomerAddress',
  141.                         $nomsCustomerAddress
  142.                     );
  143.                 }
  144.             }*/
  145.         }
  146.     }
  147.     /*public function onCreateCustomer(CustomerRegisterEvent $event): void
  148.     {
  149.             $context = $event->getSalesChannelContext()->getContext();
  150.             $street = $event->getCustomer()->getDefaultBillingAddress()->getCustomFields()['nomsStreet'];
  151.             $streetnumber = $event->getCustomer()->getDefaultBillingAddress()->getCustomFields()['nomsStreetnumber'];
  152.             $customerId = $event->getCustomer()->getId();
  153.             $customerBillingAddressId = $event->getCustomer()->getDefaultBillingAddress()->getId();
  154.             $customerShippingAddressId = $event->getCustomer()->getDefaultShippingAddress()->getId();
  155.             $this->nomsCustomerAddressRepository->create([
  156.                 [
  157.                     'customerAddressId' => $customerBillingAddressId,
  158.                     'customerId' => $customerId,
  159.                     'street' => $street,
  160.                     'streetnumber' => $streetnumber
  161.                 ]
  162.             ], $context);
  163.             if ($customerBillingAddressId != $customerShippingAddressId) {
  164.                 $street = $event->getCustomer()->getDefaultShippingAddress()->getCustomFields()['nomsStreet'];
  165.                 $streetnumber = $event->getCustomer()->getDefaultShippingAddress()->getCustomFields()['nomsStreetnumber'];
  166.                 $this->nomsCustomerAddressRepository->create([
  167.                     [
  168.                         'customerAddressId' => $customerShippingAddressId,
  169.                         'customerId' => $customerId,
  170.                         'street' => $street,
  171.                         'streetnumber' => $streetnumber
  172.                     ]
  173.                 ], $context);
  174.             }
  175.     }*/
  176.     public function onMappingAddress(DataMappingEvent $event): void
  177.     {
  178.         $data $event->getInput();
  179.         $address $event->getOutput();
  180.         $context $event->getContext();
  181.         $plainStreet $address['street'];
  182.         $houseNumber $data->get('streetnumber');
  183.         if (null === $houseNumber) {
  184.             $streetNumberArray StreetHouseNumberTool::splitStreetToStreetAndNumber($plainStreet);
  185.             $plainStreet $streetNumberArray['street'];
  186.             $houseNumber $streetNumberArray['houseNumber'];
  187.         }
  188.         //Set Street and Streetnumber in shopware orginal field
  189.         $address['customFields']['nomsStreet'] = $plainStreet;
  190.         $address['customFields']['nomsStreetnumber'] = $houseNumber;
  191.         $address['street'] = StreetHouseNumberTool::mergeStreetAndNumber($plainStreet$houseNumber);
  192.         $shouldCreatePhoneId false;
  193.         /* PhoneNumberId muss nur erzeugt werden, wenn eine Telefonnummer angegbeen wurde */
  194.         if (\strlen((string) $address['phoneNumber']) > 1) {
  195.             $shouldCreatePhoneId true;
  196.         }
  197.         $addressId $data->get('id');
  198.         if (null !== $addressId) {
  199.             $criteria = (new Criteria())->addFilter(
  200.                 new EqualsFilter('customerAddressId'$addressId),
  201.             );
  202.             $nomsCustomerAddressExtension $this->nomsCustomerAddressExtensionRepository->search(
  203.                 $criteria,
  204.                 $context
  205.             )->first();
  206.             if ($nomsCustomerAddressExtension instanceof NomsCustomerAddressExtensionEntity) {
  207.                 $nomsAddressId $nomsCustomerAddressExtension->getNomsAddressId();
  208.                 $nomsPhoneId $nomsCustomerAddressExtension->getNomsPhoneId();
  209.                 $nomsContactId $nomsCustomerAddressExtension->getNomsContactId();
  210.                 if (null === $nomsAddressId) {
  211.                     $nomsAddressId $this->generateNomsAddressId($context);
  212.                 }
  213.                 if (null === $nomsContactId) {
  214.                     $nomsContactId $this->generateNomsContactId($context);
  215.                 }
  216.                 if (null === $nomsPhoneId && true === $shouldCreatePhoneId) {
  217.                     $nomsPhoneId $this->generateNomsPhoneId($context);
  218.                 }
  219.                 $this->nomsCustomerAddressExtensionRepository->update([
  220.                     [
  221.                         'id' => $nomsCustomerAddressExtension->getId(),
  222.                         'street' => $address['customFields']['nomsStreet'],
  223.                         'streetnumber' => $address['customFields']['nomsStreetnumber'],
  224.                         'nomsAddressId' => $nomsAddressId,
  225.                         'nomsContactId' => $nomsContactId,
  226.                         'nomsPhoneId' => $nomsPhoneId,
  227.                     ],
  228.                 ], $context);
  229.             } else {
  230.                 $nomsAddressId $this->generateNomsAddressId($context);
  231.                 $nomsContactId $this->generateNomsContactId($context);
  232.                 $nomsPhoneId null;
  233.                 if ($shouldCreatePhoneId) {
  234.                     $nomsPhoneId $this->generateNomsPhoneId($context);
  235.                 }
  236.                 $customerId $data->get('customerId');
  237.                 if (null !== $customerId) {
  238.                     $this->nomsCustomerAddressExtensionRepository->create([
  239.                         [
  240.                             'customerAddressId' => $addressId,
  241.                             'customerId' => $data->get('customerId'),
  242.                             'street' => $address['customFields']['nomsStreet'],
  243.                             'streetnumber' => $address['customFields']['nomsStreetnumber'],
  244.                             'nomsAddressId' => $nomsAddressId,
  245.                             'nomsContactId' => $nomsContactId,
  246.                             'nomsPhoneId' => $nomsPhoneId,
  247.                         ],
  248.                     ], $context);
  249.                 }
  250.             }
  251.         }
  252.         $event->setOutput($address);
  253.     }
  254.     private function generateNomsPhoneId(Context $context): ?int
  255.     {
  256.         return $this->nomsSequenceService->pop(SequenceType::PHONE_ID$context);
  257.     }
  258.     private function generateNomsAddressId(Context $context): ?int
  259.     {
  260.         return $this->nomsSequenceService->pop(SequenceType::ADDRESS_ID$context);
  261.     }
  262.     public function generateNomsContactId(Context $context): ?int
  263.     {
  264.         return $this->nomsSequenceService->pop(SequenceType::CONTACT_ID$context);
  265.     }
  266.     private function isAcceptablePhoneNumber(?string $phoneNumber): bool
  267.     {
  268.         return \mb_strlen((string) $phoneNumber) > 3;
  269.     }
  270. }