vendor/store.shopware.com/swagb2bplatform/components/Address/BridgePlatform/AddressSynchronizationSubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Address\BridgePlatform;
  3. use Shopware\B2B\Common\IdValue;
  4. use Shopware\B2B\Common\Repository\NotFoundException;
  5. use Shopware\B2B\Debtor\Framework\DebtorRepositoryInterface;
  6. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  7. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressDefinition;
  8. use Shopware\Core\Checkout\Customer\CustomerEvents;
  9. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class AddressSynchronizationSubscriber implements EventSubscriberInterface
  15. {
  16.     private const SUBSCRIBER_PRIORITY = -10;
  17.     private CustomerAddressDataService $customerAddressDataService;
  18.     private AuthenticationService $authenticationService;
  19.     private EntityRepositoryInterface $customerRepository;
  20.     private DebtorRepositoryInterface $debtorRepository;
  21.     public function __construct(
  22.         CustomerAddressDataService $customerAddressDataService,
  23.         AuthenticationService $authenticationService,
  24.         EntityRepositoryInterface $customerRepository,
  25.         DebtorRepositoryInterface $debtorRepository
  26.     ) {
  27.         $this->customerAddressDataService $customerAddressDataService;
  28.         $this->authenticationService $authenticationService;
  29.         $this->customerRepository $customerRepository;
  30.         $this->debtorRepository $debtorRepository;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'syncAddressesByAddressWritten',
  36.             CustomerLoginEvent::class => ['syncAddressesByCustomerLogin'self::SUBSCRIBER_PRIORITY],
  37.         ];
  38.     }
  39.     public function syncAddressesByCustomerLogin(CustomerLoginEvent $event): void
  40.     {
  41.         if (!$this->authenticationService->isB2b()) {
  42.             return;
  43.         }
  44.         $customer $event->getCustomer();
  45.         $this->customerAddressDataService
  46.             ->synchronizeCustomerAddresses($customer);
  47.     }
  48.     public function syncAddressesByAddressWritten(EntityWrittenEvent $event): void
  49.     {
  50.         if ($event->getEntityName() !== CustomerAddressDefinition::ENTITY_NAME) {
  51.             return;
  52.         }
  53.         $writtenResults $event->getPayloads();
  54.         $customerIds = [];
  55.         foreach ($writtenResults as $address) {
  56.             $customerId $address['customerId'];
  57.             try {
  58.                 $this->debtorRepository->fetchOneById(IdValue::create($customerId));
  59.             } catch (NotFoundException $exception) {
  60.                 continue;
  61.             }
  62.             $customerIds[] = $customerId;
  63.         }
  64.         if (!$customerIds) {
  65.             return;
  66.         }
  67.         $result $this->customerRepository
  68.             ->search(new Criteria($customerIds), $event->getContext());
  69.         foreach ($result->getEntities() as $entity) {
  70.             $this->customerAddressDataService
  71.                 ->synchronizeCustomerAddresses($entity);
  72.         }
  73.     }
  74. }