custom/plugins/rapi1Connector/src/Subscribers/UpdatedCustomerAddressEntitySubscriber.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Rapidmail\Shopware\Subscribers;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. class UpdatedCustomerAddressEntitySubscriber extends UpdatedCustomerRelatedEntitySubscriber
  8. {
  9.     public const SUBSCRIBED_TO_ENTITY_NAME 'customer_address';
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         return [
  13.             CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'onWrittenAddress',
  14.         ];
  15.     }
  16.     public function onWrittenAddress(EntityWrittenEvent $event): void
  17.     {
  18.         $context $event->getContext();
  19.         $writeResult $event->getWriteResults()[0] ?? null;
  20.         if (!($writeResult instanceof EntityWriteResult)) {
  21.             return;
  22.         }
  23.         $payload $writeResult->getPayload();
  24.         if ($this->isAllowed($event$payload)) {
  25.             $this->updateCustomer(
  26.                 $this->getCustomerId($payload),
  27.                 $context
  28.             );
  29.         }
  30.     }
  31.     protected function isAllowed(EntityWrittenEvent $event, ?array $data): bool
  32.     {
  33.         return (
  34.             $event->getEntityName() === self::SUBSCRIBED_TO_ENTITY_NAME &&
  35.             is_array($data) &&
  36.             !empty($this->getCustomerId($data))
  37.         );
  38.     }
  39.     private function getCustomerId(array $data): ?string
  40.     {
  41.         return (!empty($data['customerId'])) ? $data['customerId'] : null;
  42.     }
  43. }