src/Core/Checkout/Customer/Subscriber/CustomerBeforeDeleteSubscriber.php line 42

  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\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\BeforeDeleteEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Shopware\Core\Framework\Util\Random;
  12. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
  13. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. /**
  17.  * @internal
  18.  */
  19. #[Package('customer-order')]
  20. class CustomerBeforeDeleteSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @internal
  24.      */
  25.     public function __construct(private readonly EntityRepository $customerRepository, private readonly SalesChannelContextServiceInterface $salesChannelContextService, private readonly EventDispatcherInterface $eventDispatcher)
  26.     {
  27.     }
  28.     /**
  29.      * @return array<string, string>
  30.      */
  31.     public static function getSubscribedEvents(): array
  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.         /** @var CustomerCollection $customers */
  50.         $customers $this->customerRepository->search(new Criteria($ids), $context);
  51.         $event->addSuccess(function () use ($customers$context$salesChannelId): void {
  52.             foreach ($customers->getElements() as $customer) {
  53.                 $salesChannelContext $this->salesChannelContextService->get(
  54.                     new SalesChannelContextServiceParameters(
  55.                         $salesChannelId ?? $customer->getSalesChannelId(),
  56.                         Random::getAlphanumericString(32),
  57.                         $customer->getLanguageId(),
  58.                         null,
  59.                         null,
  60.                         $context,
  61.                     )
  62.                 );
  63.                 $this->eventDispatcher->dispatch(new CustomerDeletedEvent($salesChannelContext$customer));
  64.             }
  65.         });
  66.     }
  67. }