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

  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(
  26.         private readonly EntityRepository $customerRepository,
  27.         private readonly SalesChannelContextServiceInterface $salesChannelContextService,
  28.         private readonly EventDispatcherInterface $eventDispatcher
  29.     ) {
  30.     }
  31.     /**
  32.      * @return array<string, string>
  33.      */
  34.     public static function getSubscribedEvents(): array
  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.         /** @var CustomerCollection $customers */
  53.         $customers $this->customerRepository->search(new Criteria($ids), $context);
  54.         $event->addSuccess(function () use ($customers$context$salesChannelId): void {
  55.             foreach ($customers->getElements() as $customer) {
  56.                 $salesChannelContext $this->salesChannelContextService->get(
  57.                     new SalesChannelContextServiceParameters(
  58.                         $salesChannelId ?? $customer->getSalesChannelId(),
  59.                         Random::getAlphanumericString(32),
  60.                         $customer->getLanguageId(),
  61.                         null,
  62.                         null,
  63.                         $context,
  64.                     )
  65.                 );
  66.                 $this->eventDispatcher->dispatch(new CustomerDeletedEvent($salesChannelContext$customer));
  67.             }
  68.         });
  69.     }
  70. }