custom/static-plugins/NdSRS/src/Subscriber/CustomerSubscriber.php line 64

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Srs\Subscriber;
  3. use Nd\Srs\Service\CustomerCommunicator;
  4. use Shopware\Core\Content\Seo\SeoUrlUpdater;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CustomerSubscriber implements EventSubscriberInterface{
  13.     protected EntityRepositoryInterface $customerRepository;
  14.     protected CustomerCommunicator $customerCommunicator;
  15.     public function __construct(EntityRepositoryInterface $customerRepositoryCustomerCommunicator $customerCommunicator){
  16.         $this->customerRepository $customerRepository;
  17.         $this->customerCommunicator $customerCommunicator;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             'customer.written' => 'onCustomerEntityWritten',
  23.             'customer_address.written' => 'onCustomerAddressEntityWritten'
  24.         ];
  25.     }
  26.     public function onCustomerAddressEntityWritten(EntityWrittenEvent $event): void
  27.     {
  28.         $payload $event->getPayloads()[0];
  29.         if(count($event->getWriteResults()) == 0){
  30.             return;
  31.         }
  32.         if(!array_key_exists("updatedAt"$payload) || !array_key_exists("customerId"$payload) || !array_key_exists("street"$payload)){
  33.             return;
  34.         }
  35.         $customerId $payload["customerId"];
  36.         $customer $this->retrieveCustomer($customerId$event->getContext());
  37.         if(!$customer){
  38.             return;
  39.         }
  40.         if($event->getWriteResults()[0]->getOperation() == "update"){
  41.             if($customer->getDefaultBillingAddressId() == $payload["id"]){
  42.                 if($payload['firstName'] != $customer->getFirstName() || $payload['lastName'] != $customer->getLastName()){
  43.                     $data = ['id' => $customer->getId(), "firstName" => $payload['firstName'], "lastName" => $payload['lastName']];
  44.                     $this->customerRepository->upsert([$data], $event->getContext());
  45.                     return; // This one also triggers the complete flow
  46.                 }
  47.                 $this->customerCommunicator->updateCurrentSrsCustomer($customer$event->getContext());
  48.             }
  49.         }
  50.     }
  51.     public function onCustomerEntityWritten(EntityWrittenEvent $event): void
  52.     {
  53.         $payload $event->getPayloads()[0];
  54.         if(count($event->getWriteResults()) == 0){
  55.             return;
  56.         }
  57.         $customerId $event->getWriteResults()[0]->getPrimaryKey();
  58.         $customer $this->retrieveCustomer($customerId$event->getContext());
  59.         if(!$customer){
  60.             return;
  61.         }
  62.         if($event->getWriteResults()[0]->getOperation() == "insert"){
  63.             $this->customerCommunicator->createOrUpdateSrsCustomer($customer$event->getContext());
  64.         }elseif($event->getWriteResults()[0]->getOperation() == "update"){
  65.             if(in_array("lastLogin"array_keys($payload))){
  66.                 $this->customerCommunicator->checkSrsCustomerForChanges($customer$event->getContext());
  67.             }elseif(in_array("updatedAt"array_keys($payload))){
  68.                 if(array_key_exists("firstName"$payload) && array_key_exists("lastName"$payload)){
  69.                     if($payload['firstName'] != $customer->getDefaultBillingAddress()->getFirstName() || $payload['lastName'] != $customer->getDefaultBillingAddress()->getLastName()){
  70.                         $data = ['id' => $customer->getId(), "defaultBillingAddress" => ["id" => $customer->getDefaultBillingAddress()->getId(), "firstName" => $payload['firstName'], "lastName" => $payload['lastName']]];
  71.                         $this->customerRepository->upsert([$data], $event->getContext());
  72.                         return; // This one also triggers the complete flow
  73.                     }
  74.                     $this->customerCommunicator->updateCurrentSrsCustomer($customer$event->getContext());
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     private function retrieveCustomer($customerIdContext $context){
  80.         try{
  81.             $criteria = new Criteria([$customerId]);
  82.             $criteria->addAssociation("defaultBillingAddress.country");
  83.             $criteria->addAssociation("defaultShippingAddress.country");
  84.             $criteria->addAssociation("salutation");
  85.             $criteria->addAssociation("country");
  86.             $customer $this->customerRepository->search($criteria$context)->first();
  87.         } catch (InvalidUuidException $e) {
  88.             return false;
  89.         }
  90.         if(!$customer){
  91.             return false;
  92.         }
  93.         if($customer->getGuest()){
  94.             return false;
  95.         }
  96.         return $customer;
  97.     }
  98. }