<?phpnamespace Abas\AbasConnector\Subscribers\Customer;use Abas\AbasConnector\Service\Mapper\Customer\Exceptions\NoAbasCustomerException;use Abas\AbasConnector\Service\Sync\AbasDataComponentSync;use Abas\AbasConnector\Service\Utils\Config\PluginConfigReader;use Exception;use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;use Shopware\Core\Checkout\Customer\CustomerEvents;use Shopware\Core\Framework\Context;use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class UpdateAddressSubscriber implements EventSubscriberInterface{ /** * @var bool */ protected static $running = false; /** * @var EntityRepositoryInterface */ protected $entityRepository; /** * @var AbasDataComponentSync */ protected $abasDataComponentSync; /** * @var PluginConfigReader */ protected $pluginConfigReader; /** * @param EntityRepositoryInterface $entityRepository * @param AbasDataComponentSync $abasDataComponentSync * @param PluginConfigReader $pluginConfigReader */ public function __construct( EntityRepositoryInterface $entityRepository, AbasDataComponentSync $abasDataComponentSync, PluginConfigReader $pluginConfigReader ) { $this->entityRepository = $entityRepository; $this->abasDataComponentSync = $abasDataComponentSync; $this->pluginConfigReader = $pluginConfigReader; } /** * @inheritDoc */ public static function getSubscribedEvents() { return [ CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'onCustomerAddressWrittenEvent' ]; } /** * @param EntityWrittenEvent $event * * @return void * @throws Exception */ public function onCustomerAddressWrittenEvent(EntityWrittenEvent $event) { if (self::$running || !$this->pluginConfigReader->getSyncShippingAddressesAsCustomerContacts()) { return; } self::$running = true; $context = $event->getContext(); $ids = $event->getIds(); try { foreach ($ids as $id) { $this->syncCustomerAddress($id, $context); } } finally { self::$running = false; } } /** * @param string $id * @param Context $context * * @return void */ protected function syncCustomerAddress(string $id, Context $context) { $customerAddressEntity = $this->getAddressCustomerAddressEntity($id, $context); try { $this->abasDataComponentSync->syncEntityToAbas($customerAddressEntity); } catch (NoAbasCustomerException $exception) { } } /** * @param string $id * @param Context $context * * @return CustomerAddressEntity|null * @throws InconsistentCriteriaIdsException */ protected function getAddressCustomerAddressEntity(string $id, Context $context): ?CustomerAddressEntity { $criteria = new Criteria([$id]); $criteria->addAssociations( [ 'customer', 'country', 'salutation' ] ); $criteria->setLimit(1); return $this->entityRepository->search($criteria, $context)->first(); }}