custom/plugins/NetiNextAdminTools/src/Subscriber/CustomerSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextAdminTools\Subscriber;
  4. use NetInventors\NetiNextAdminTools\Core\FlowTrigger\NetiNextAdminToolsCustomerActivatedEvent;
  5. use NetInventors\NetiNextAdminTools\Core\FlowTrigger\NetiNextAdminToolsCustomerDeactivatedEvent;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Customer\CustomerEvents;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  14. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class CustomerSubscriber implements EventSubscriberInterface
  18. {
  19.     private EventDispatcherInterface           $eventDispatcher;
  20.     private EntityRepositoryInterface          $customerRepository;
  21.     private AbstractSalesChannelContextFactory $salesChannelContextFactory;
  22.     public function __construct(
  23.         EventDispatcherInterface           $eventDispatcher,
  24.         EntityRepositoryInterface          $customerRepository,
  25.         AbstractSalesChannelContextFactory $salesChannelContextFactory
  26.     ) {
  27.         $this->eventDispatcher            $eventDispatcher;
  28.         $this->customerRepository         $customerRepository;
  29.         $this->salesChannelContextFactory $salesChannelContextFactory;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  35.         ];
  36.     }
  37.     public function onCustomerWritten(EntityWrittenEvent $event): void
  38.     {
  39.         $payloads    $event->getPayloads();
  40.         $context     $event->getContext();
  41.         $customerIds = [];
  42.         /** @var array<string, Context> $languageContexts */
  43.         $languageContexts                            = [];
  44.         $languageContexts[$context->getLanguageId()] = $context;
  45.         /** @var array<string, mixed> $payload */
  46.         foreach ($payloads as $payload) {
  47.             if (!isset($payload['active'], $payload['id'])) {
  48.                 continue;
  49.             }
  50.             $customerIds[] = (string)$payload['id'];
  51.         }
  52.         if ([] === $customerIds) {
  53.             return;
  54.         }
  55.         $customers $this->getCustomersById($customerIds$context);
  56.         if ([] === $customers) {
  57.             return;
  58.         }
  59.         /** @var CustomerEntity $customer */
  60.         foreach ($customers as $customer) {
  61.             if (!$customer instanceof CustomerEntity) {
  62.                 continue;
  63.             }
  64.             $customersLanguageId $customer->getLanguageId();
  65.             if (!isset($languageContexts[$customersLanguageId])) {
  66.                 $salesChannelContext $this->salesChannelContextFactory->create(
  67.                     '',
  68.                     $customer->getSalesChannelId(),
  69.                     [ SalesChannelContextService::LANGUAGE_ID => $customersLanguageId ]
  70.                 );
  71.                 $languageContexts[$customersLanguageId] = $salesChannelContext->getContext();
  72.             }
  73.             //if it is true, then the customer was fetched with the wrong language id
  74.             if ($context->getLanguageId() !== $customersLanguageId) {
  75.                 $criteria = new Criteria([ $customer->getId() ]);
  76.                 $criteria->addAssociation('salutation');
  77.                 //loading customer again for the correct salutation
  78.                 /** @var CustomerEntity $customer */
  79.                 $customer =
  80.                     $this->customerRepository->search($criteria$languageContexts[$customersLanguageId])->first();
  81.             }
  82.             if ($customer->getActive()) {
  83.                 $this->eventDispatcher->dispatch(
  84.                     new NetiNextAdminToolsCustomerActivatedEvent(
  85.                         $languageContexts[$customersLanguageId], $customer$customer->getSalesChannelId()
  86.                     )
  87.                 );
  88.             } else {
  89.                 $this->eventDispatcher->dispatch(
  90.                     new NetiNextAdminToolsCustomerDeactivatedEvent(
  91.                         $languageContexts[$customersLanguageId],
  92.                         $customer,
  93.                         $customer->getSalesChannelId()
  94.                     )
  95.                 );
  96.             }
  97.         }
  98.     }
  99.     private function getCustomersById(array $customerIdsContext $context): array
  100.     {
  101.         $criteria = new Criteria();
  102.         $criteria->addFilter(new EqualsAnyFilter('id'$customerIds));
  103.         $criteria->addAssociation('salutation');
  104.         return $this->customerRepository->search($criteria$context)->getElements();
  105.     }
  106. }