custom/static-plugins/AbasConnector/src/Subscribers/Customer/UpdateAddressSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. namespace Abas\AbasConnector\Subscribers\Customer;
  3. use Abas\AbasConnector\Service\Mapper\Customer\Exceptions\NoAbasCustomerException;
  4. use Abas\AbasConnector\Service\Sync\AbasDataComponentSync;
  5. use Abas\AbasConnector\Service\Utils\Config\PluginConfigReader;
  6. use Exception;
  7. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  8. use Shopware\Core\Checkout\Customer\CustomerEvents;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class UpdateAddressSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var bool
  19.      */
  20.     protected static $running false;
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     protected $entityRepository;
  25.     /**
  26.      * @var AbasDataComponentSync
  27.      */
  28.     protected $abasDataComponentSync;
  29.     /**
  30.      * @var PluginConfigReader
  31.      */
  32.     protected $pluginConfigReader;
  33.     /**
  34.      * @param EntityRepositoryInterface $entityRepository
  35.      * @param AbasDataComponentSync $abasDataComponentSync
  36.      * @param PluginConfigReader $pluginConfigReader
  37.      */
  38.     public function __construct(
  39.         EntityRepositoryInterface $entityRepository,
  40.         AbasDataComponentSync $abasDataComponentSync,
  41.         PluginConfigReader $pluginConfigReader
  42.     ) {
  43.         $this->entityRepository $entityRepository;
  44.         $this->abasDataComponentSync $abasDataComponentSync;
  45.         $this->pluginConfigReader $pluginConfigReader;
  46.     }
  47.     /**
  48.      * @inheritDoc
  49.      */
  50.     public static function getSubscribedEvents()
  51.     {
  52.         return [
  53.             CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'onCustomerAddressWrittenEvent'
  54.         ];
  55.     }
  56.     /**
  57.      * @param EntityWrittenEvent $event
  58.      *
  59.      * @return void
  60.      * @throws Exception
  61.      */
  62.     public function onCustomerAddressWrittenEvent(EntityWrittenEvent $event)
  63.     {
  64.         if (self::$running || !$this->pluginConfigReader->getSyncShippingAddressesAsCustomerContacts()) {
  65.             return;
  66.         }
  67.         self::$running true;
  68.         $context $event->getContext();
  69.         $ids $event->getIds();
  70.         try {
  71.             foreach ($ids as $id) {
  72.                 $this->syncCustomerAddress($id$context);
  73.             }
  74.         } finally {
  75.             self::$running false;
  76.         }
  77.     }
  78.     /**
  79.      * @param string $id
  80.      * @param Context $context
  81.      *
  82.      * @return void
  83.      */
  84.     protected function syncCustomerAddress(string $idContext $context)
  85.     {
  86.         $customerAddressEntity $this->getAddressCustomerAddressEntity($id$context);
  87.         try {
  88.             $this->abasDataComponentSync->syncEntityToAbas($customerAddressEntity);
  89.         } catch (NoAbasCustomerException $exception) {
  90.         }
  91.     }
  92.     /**
  93.      * @param string $id
  94.      * @param Context $context
  95.      *
  96.      * @return CustomerAddressEntity|null
  97.      * @throws InconsistentCriteriaIdsException
  98.      */
  99.     protected function getAddressCustomerAddressEntity(string $idContext $context): ?CustomerAddressEntity
  100.     {
  101.         $criteria = new Criteria([$id]);
  102.         $criteria->addAssociations(
  103.             [
  104.                 'customer',
  105.                 'country',
  106.                 'salutation'
  107.             ]
  108.         );
  109.         $criteria->setLimit(1);
  110.         return $this->entityRepository->search($criteria$context)->first();
  111.     }
  112. }