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

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