custom/plugins/SwpaSort/src/Subscriber/ProductUpdater.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swpa\SwpaSort\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  5. use Shopware\Elasticsearch\Framework\Indexing\ElasticsearchIndexer;
  6. use Swpa\SwpaSort\Service\CatalogRepositoryInterface;
  7. use Swpa\SwpaSort\Service\Indexing\SwpaSortingMessage;
  8. use Swpa\SwpaSort\Service\SwpaSortingContextSource;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Messenger\MessageBusInterface;
  11. class ProductUpdater implements EventSubscriberInterface
  12. {
  13.     private ElasticsearchIndexer $indexer;
  14.     private EntityDefinition $definition;
  15.     private MessageBusInterface $messageBus;
  16.     private CatalogRepositoryInterface $catalogRepository;
  17.     private bool $elasticsearchEnabled;
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(
  22.         ElasticsearchIndexer       $indexer,
  23.         EntityDefinition           $definition,
  24.         MessageBusInterface        $messageBus,
  25.         CatalogRepositoryInterface $catalogRepository,
  26.         bool $elasticSearchEnabled
  27.     )
  28.     {
  29.         $this->indexer $indexer;
  30.         $this->definition $definition;
  31.         $this->messageBus $messageBus;
  32.         $this->catalogRepository $catalogRepository;
  33.         $this->elasticsearchEnabled $elasticSearchEnabled;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             ProductIndexerEvent::class => ['update', -1],
  39.         ];
  40.     }
  41.     public function update(ProductIndexerEvent $event): void
  42.     {
  43.         if ($this->elasticsearchEnabled && $event->getContext()->getSource() instanceof SwpaSortingContextSource) {
  44.             $productIds $event->getIds();
  45.             $productIds array_unique($productIds);
  46.             $message = new SwpaSortingMessage($productIdsnull);
  47.             $message->setIndexer('swpa.sort.product.indexer');
  48.             $this->messageBus->dispatch($message);
  49.             if ($children $this->catalogRepository->fetchChildrenByProductIds($productIds)) {
  50.                 $this->indexer->updateIds($this->definition$children);
  51.             }
  52.         }
  53.     }
  54. }