custom/plugins/SwagB2bPlatform/components/Address/BridgePlatform/AddressSynchronizationSubscriber.php line 73

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.     /**
  18.      * @var CustomerAddressDataService
  19.      */
  20.     private $customerAddressDataService;
  21.     /**
  22.      * @var AuthenticationService
  23.      */
  24.     private $authenticationService;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $customerRepository;
  29.     /**
  30.      * @var DebtorRepositoryInterface
  31.      */
  32.     private $debtorRepository;
  33.     public function __construct(
  34.         CustomerAddressDataService $customerAddressDataService,
  35.         AuthenticationService $authenticationService,
  36.         EntityRepositoryInterface $customerRepository,
  37.         DebtorRepositoryInterface $debtorRepository
  38.     ) {
  39.         $this->customerAddressDataService $customerAddressDataService;
  40.         $this->authenticationService $authenticationService;
  41.         $this->customerRepository $customerRepository;
  42.         $this->debtorRepository $debtorRepository;
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'syncAddressesByAddressWritten',
  48.             CustomerLoginEvent::class => ['syncAddressesByCustomerLogin'self::SUBSCRIBER_PRIORITY],
  49.         ];
  50.     }
  51.     public function syncAddressesByCustomerLogin(CustomerLoginEvent $event): void
  52.     {
  53.         if (!$this->authenticationService->isB2b()) {
  54.             return;
  55.         }
  56.         $customer $event->getCustomer();
  57.         $this->customerAddressDataService
  58.             ->synchronizeCustomerAddresses($customer);
  59.     }
  60.     public function syncAddressesByAddressWritten(EntityWrittenEvent $event): void
  61.     {
  62.         if ($event->getEntityName() !== CustomerAddressDefinition::ENTITY_NAME) {
  63.             return;
  64.         }
  65.         $writtenResults $event->getPayloads();
  66.         $customerIds = [];
  67.         foreach ($writtenResults as $address) {
  68.             $customerId $address['customerId'];
  69.             try {
  70.                 $this->debtorRepository->fetchOneById(IdValue::create($customerId));
  71.             } catch (NotFoundException $exception) {
  72.                 continue;
  73.             }
  74.             $customerIds[] = $customerId;
  75.         }
  76.         if (!$customerIds) {
  77.             return;
  78.         }
  79.         $result $this->customerRepository
  80.             ->search(new Criteria($customerIds), $event->getContext());
  81.         foreach ($result->getEntities() as $entity) {
  82.             $this->customerAddressDataService
  83.                 ->synchronizeCustomerAddresses($entity);
  84.         }
  85.     }
  86. }