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

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\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. * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  18. */
  19. #[Package('customer-order')]
  20. class CustomerBeforeDeleteSubscriber implements EventSubscriberInterface
  21. {
  22. private EntityRepositoryInterface $customerRepository;
  23. private SalesChannelContextServiceInterface $salesChannelContextService;
  24. private EventDispatcherInterface $eventDispatcher;
  25. /**
  26. * @internal
  27. */
  28. public function __construct(
  29. EntityRepositoryInterface $customerRepository,
  30. SalesChannelContextServiceInterface $salesChannelContextService,
  31. EventDispatcherInterface $eventDispatcher
  32. ) {
  33. $this->customerRepository = $customerRepository;
  34. $this->salesChannelContextService = $salesChannelContextService;
  35. $this->eventDispatcher = $eventDispatcher;
  36. }
  37. /**
  38. * @return array<string, string>
  39. */
  40. public static function getSubscribedEvents()
  41. {
  42. return [
  43. BeforeDeleteEvent::class => 'beforeDelete',
  44. ];
  45. }
  46. public function beforeDelete(BeforeDeleteEvent $event): void
  47. {
  48. $context = $event->getContext();
  49. $ids = $event->getIds(CustomerDefinition::ENTITY_NAME);
  50. if (empty($ids)) {
  51. return;
  52. }
  53. $source = $context->getSource();
  54. $salesChannelId = null;
  55. if ($source instanceof SalesChannelApiSource) {
  56. $salesChannelId = $source->getSalesChannelId();
  57. }
  58. /** @var CustomerCollection $customers */
  59. $customers = $this->customerRepository->search(new Criteria($ids), $context);
  60. $event->addSuccess(function () use ($customers, $context, $salesChannelId): void {
  61. foreach ($customers->getElements() as $customer) {
  62. $salesChannelContext = $this->salesChannelContextService->get(
  63. new SalesChannelContextServiceParameters(
  64. $salesChannelId ?? $customer->getSalesChannelId(),
  65. Random::getAlphanumericString(32),
  66. $customer->getLanguageId(),
  67. null,
  68. null,
  69. $context,
  70. )
  71. );
  72. $this->eventDispatcher->dispatch(new CustomerDeletedEvent($salesChannelContext, $customer));
  73. }
  74. });
  75. }
  76. }