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

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