vendor/shopware/core/Checkout/Customer/Subscriber/CustomerBeforeDeleteSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerDefinition;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerDeletedEvent;
  5. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\BeforeDeleteEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\Util\Random;
  10. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
  11. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class CustomerBeforeDeleteSubscriber implements EventSubscriberInterface
  15. {
  16.     private EntityRepositoryInterface $customerRepository;
  17.     private SalesChannelContextServiceInterface $salesChannelContextService;
  18.     private EventDispatcherInterface $eventDispatcher;
  19.     public function __construct(
  20.         EntityRepositoryInterface $customerRepository,
  21.         SalesChannelContextServiceInterface $salesChannelContextService,
  22.         EventDispatcherInterface $eventDispatcher
  23.     ) {
  24.         $this->customerRepository $customerRepository;
  25.         $this->salesChannelContextService $salesChannelContextService;
  26.         $this->eventDispatcher $eventDispatcher;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             BeforeDeleteEvent::class => 'beforeDelete',
  32.         ];
  33.     }
  34.     public function beforeDelete(BeforeDeleteEvent $event): void
  35.     {
  36.         $context $event->getContext();
  37.         $ids $event->getIds(CustomerDefinition::ENTITY_NAME);
  38.         if (empty($ids)) {
  39.             return;
  40.         }
  41.         $source $context->getSource();
  42.         $salesChannelId null;
  43.         if ($source instanceof SalesChannelApiSource) {
  44.             $salesChannelId $source->getSalesChannelId();
  45.         }
  46.         $customers $this->customerRepository->search(new Criteria($ids), $context);
  47.         $event->addSuccess(function () use ($customers$context$salesChannelId): void {
  48.             foreach ($customers->getElements() as $customer) {
  49.                 $salesChannelContext $this->salesChannelContextService->get(
  50.                     new SalesChannelContextServiceParameters(
  51.                         $salesChannelId ?? $customer->getSalesChannelId(),
  52.                         Random::getAlphanumericString(32),
  53.                         $customer->getLanguageId(),
  54.                         null,
  55.                         null,
  56.                         $context,
  57.                     )
  58.                 );
  59.                 $this->eventDispatcher->dispatch(new CustomerDeletedEvent($salesChannelContext$customer));
  60.             }
  61.         });
  62.     }
  63. }