custom/plugins/rapi1Connector/src/Subscribers/DeletedEntitySubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Rapidmail\Shopware\Subscribers;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Content\Newsletter\NewsletterEvents;
  7. use Shopware\Core\Content\Product\ProductEvents;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class DeletedEntitySubscriber implements EventSubscriberInterface
  13. {
  14.     private $entityRepository;
  15.     private $logger;
  16.     public function __construct(
  17.         EntityRepositoryInterface $entityRepository,
  18.         LoggerInterface $logger
  19.     ) {
  20.         $this->entityRepository $entityRepository;
  21.         $this->logger $logger;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             CustomerEvents::CUSTOMER_DELETED_EVENT => 'onDeleteCustomer',
  27.             ProductEvents::PRODUCT_DELETED_EVENT => 'onDeleteProduct',
  28.             NewsletterEvents::NEWSLETTER_RECIPIENT_DELETED_EVENT => 'onDeleteNewsletterRecipient',
  29.         ];
  30.     }
  31.     public function onDeleteCustomer(EntityDeletedEvent $event): void
  32.     {
  33.         $this->createDeletedEntity('customer'$event->getIds(), $event->getContext());
  34.     }
  35.     public function onDeleteProduct(EntityDeletedEvent $event): void
  36.     {
  37.         $this->createDeletedEntity('product'$event->getIds(), $event->getContext());
  38.     }
  39.     public function onDeleteNewsletterRecipient(EntityDeletedEvent $event): void
  40.     {
  41.         $this->createDeletedEntity('newsletter_recipient'$event->getIds(), $event->getContext());
  42.     }
  43.     /**
  44.      * @param string[] $ids
  45.      */
  46.     private function createDeletedEntity(string $type, array $idsContext $context): void
  47.     {
  48.         $this->logger->info("Track deleted $type"compact('ids'));
  49.         $this->entityRepository->create(
  50.             array_map(
  51.                 function (string $id) use ($type) {
  52.                     return [
  53.                         'entityId' => (string)$id,
  54.                         'type' => $type,
  55.                     ];
  56.                 },
  57.                 $ids
  58.             ),
  59.             $context
  60.         );
  61.     }
  62. }