custom/plugins/SwagMarkets/src/Subscriber/ProductDeleteSubscriber.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\Markets\Subscriber;
  3. use Exception;
  4. use Generator;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\Content\Product\ProductEvents;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. class ProductDeleteSubscriber implements EventSubscriberInterface 
  12. {
  13.     /** @var EntityRepositoryInterface */
  14.     private $productToDeleteRepository;
  15.     /**
  16.      * @param EntityRepositoryInterface $productToDeleteRepository
  17.      */
  18.     public function __construct(EntityRepositoryInterface $productToDeleteRepository)
  19.     {
  20.         $this->productToDeleteRepository $productToDeleteRepository;
  21.     }
  22.     /**
  23.      * Register events to listen to
  24.      *
  25.      * @return array
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  30.         return [
  31.             ProductEvents::PRODUCT_DELETED_EVENT => 'onProductsDeleted',
  32.         ];
  33.     }
  34.     /**
  35.      * @param EntityDeletedEvent $event
  36.      * @return void
  37.      */
  38.     public function onProductsDeleted(EntityDeletedEvent $event): void
  39.     {
  40.         try {
  41.             $eventWriteResult $event->getWriteResults();
  42.             if ($event->getEntityName() === 'product') {
  43.                 $this->storeProductToDelete($eventWriteResult);
  44.             }
  45.         } catch (Exception $e) {
  46.         }
  47.     }
  48.     /**
  49.      * @param array $eventWriteResult
  50.      * @return void
  51.      */
  52.     private function storeProductToDelete(array $eventWriteResult): void
  53.     {
  54.         /** @var EntityWriteResult $entityWriteResult */
  55.         foreach ($this->getEntityWriteResult($eventWriteResult) as $entityWriteResult) {
  56.             $data = [
  57.                 'productId' => $entityWriteResult->getPrimaryKey(),
  58.                 'isVariation' => $entityWriteResult->getExistence()->isChild() ? 0
  59.             ];
  60.             $this->productToDeleteRepository->upsert([$data], Context::createDefaultContext());
  61.         }
  62.     }
  63.     /**
  64.      * Returns a single EntityWriteResult with every loop to work with.
  65.      *
  66.      * @param array $eventWriteResult
  67.      * @return Generator
  68.      */
  69.     private function getEntityWriteResult(array $eventWriteResult): Generator
  70.     {
  71.         if (count($eventWriteResult) > 0) {
  72.             /** @var EntityWriteResult $entityWriteResult */
  73.             foreach ($eventWriteResult as $entityWriteResult) {
  74.                 if ($entityWriteResult->getEntityName() === 'product' && $entityWriteResult->getOperation() === 'delete') {
  75.                     yield $entityWriteResult;
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }