custom/static-plugins/BaumarktTheme/src/Subscriber/CustomerSubscriber.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BaumarktTheme\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. /**
  11.  * @property RequestStack $requestStack
  12.  */
  13. class CustomerSubscriber implements EventSubscriberInterface
  14. {
  15.     private EntityRepositoryInterface $ianeoCustomerAttributesEntity;
  16.     /**
  17.      * @throws \Exception
  18.      */
  19.     public function __construct(
  20.         RequestStack              $requestStack,
  21.         EntityRepositoryInterface $ianeoCustomerAttributesRepository
  22.     )
  23.     {
  24.         $this->requestStack $requestStack;
  25.         $this->ianeoCustomerAttributesEntity $ianeoCustomerAttributesRepository;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             'customer.written' => 'onCustomerWritten',
  31.             'customer_address.written' => 'onCustomerAddressWritten'
  32.         ];
  33.     }
  34.     public function onCustomerWritten(EntityWrittenEvent $event)
  35.     {
  36.         $customerId $event->getIds()[0];
  37.         // Return when event not triggered in frontend context with httpRequest
  38.         if(empty($this->requestStack->getCurrentRequest())) {
  39.             return;
  40.         }
  41.         $personalPrimaryPhone $this->requestStack->getCurrentRequest()->get('personalPrimaryPhone');
  42.         if (!$personalPrimaryPhone) {
  43.             return;
  44.         }
  45.         $this->upsertCustomerAttributes($customerId$personalPrimaryPhone$event->getContext());
  46.     }
  47.     public function onCustomerAddressWritten(EntityWrittenEvent $event) : void
  48.     {
  49.         $customer $event->getPayloads();
  50.         if (empty($customer) || empty($customer[0]['customerId'])) {
  51.             return;
  52.         }
  53.         $customerId $customer[0]['customerId'];
  54.         // Return when event not triggered in frontend context with httpRequest
  55.         if(
  56.             empty($this->requestStack->getCurrentRequest())
  57.             || empty($this->requestStack->getCurrentRequest()->request->get('address'))
  58.             || empty($this->requestStack->getCurrentRequest()->request->get('address')['personalPrimaryPhone'])
  59.         ) {
  60.             return;
  61.         }
  62.         if($this->requestStack->getCurrentRequest()->request->get('address') !== null) {
  63.             $personalPrimaryPhone $this->requestStack->getCurrentRequest()->request->get('address')['personalPrimaryPhone'];
  64.         } else {
  65.             $personalPrimaryPhone $this->requestStack->getCurrentRequest()->request->get('personalPrimaryPhone');
  66.         }
  67.         if (!$personalPrimaryPhone) {
  68.             return;
  69.         }
  70.         $this->upsertCustomerAttributes($customerId$personalPrimaryPhone$event->getContext());
  71.     }
  72.     private function upsertCustomerAttributes($customerId$personalPrimaryPhone$context)
  73.     {
  74.         $criteria = new Criteria();
  75.         $criteria->addFilter(new EqualsFilter('customerId'$customerId));
  76.         $ianeoCustomerAttributesEntity $this->ianeoCustomerAttributesEntity->search(
  77.             $criteria,
  78.             $context
  79.         )->first();
  80.         $attributesId Uuid::randomHex();
  81.         if ($ianeoCustomerAttributesEntity) {
  82.             $attributesId $ianeoCustomerAttributesEntity->getId();
  83.         }
  84.         $this->ianeoCustomerAttributesEntity->upsert([
  85.             [
  86.                 'id' => $attributesId,
  87.                 'customerId' => $customerId,
  88.                 'primaryPhoneNumber' => $personalPrimaryPhone
  89.             ]
  90.         ], $context);
  91.     }
  92. }