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

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.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(
  23.         EntityRepositoryInterface $customerRepository,
  24.         SalesChannelContextServiceInterface $salesChannelContextService,
  25.         EventDispatcherInterface $eventDispatcher
  26.     ) {
  27.         $this->customerRepository $customerRepository;
  28.         $this->salesChannelContextService $salesChannelContextService;
  29.         $this->eventDispatcher $eventDispatcher;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             BeforeDeleteEvent::class => 'beforeDelete',
  35.         ];
  36.     }
  37.     public function beforeDelete(BeforeDeleteEvent $event): void
  38.     {
  39.         $context $event->getContext();
  40.         $ids $event->getIds(CustomerDefinition::ENTITY_NAME);
  41.         if (empty($ids)) {
  42.             return;
  43.         }
  44.         $source $context->getSource();
  45.         $salesChannelId null;
  46.         if ($source instanceof SalesChannelApiSource) {
  47.             $salesChannelId $source->getSalesChannelId();
  48.         }
  49.         $customers $this->customerRepository->search(new Criteria($ids), $context);
  50.         $event->addSuccess(function () use ($customers$context$salesChannelId): void {
  51.             foreach ($customers->getElements() as $customer) {
  52.                 $salesChannelContext $this->salesChannelContextService->get(
  53.                     new SalesChannelContextServiceParameters(
  54.                         $salesChannelId ?? $customer->getSalesChannelId(),
  55.                         Random::getAlphanumericString(32),
  56.                         $customer->getLanguageId(),
  57.                         null,
  58.                         null,
  59.                         $context,
  60.                     )
  61.                 );
  62.                 $this->eventDispatcher->dispatch(new CustomerDeletedEvent($salesChannelContext$customer));
  63.             }
  64.         });
  65.     }
  66. }