vendor/shopware/elasticsearch/Product/LanguageSubscriber.php line 37

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Elasticsearch\Product;
  3. use OpenSearch\Client;
  4. use Shopware\Core\Content\Product\ProductDefinition;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Elasticsearch\Framework\ElasticsearchHelper;
  9. use Shopware\Elasticsearch\Framework\Indexing\ElasticsearchLanguageIndexIteratorMessage;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Messenger\MessageBusInterface;
  12. /**
  13.  * @internal
  14.  * When an language is created, we need to trigger an indexing for that
  15.  */
  16. #[Package('core')]
  17. class LanguageSubscriber implements EventSubscriberInterface
  18. {
  19.     public function __construct(
  20.         private readonly ElasticsearchHelper $elasticsearchHelper,
  21.         private readonly ProductDefinition $productDefinition,
  22.         private readonly Client $client,
  23.         private readonly MessageBusInterface $bus
  24.     ) {
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             'sales_channel_language.written' => 'onSalesChannelWritten',
  30.         ];
  31.     }
  32.     public function onSalesChannelWritten(EntityWrittenEvent $event): void
  33.     {
  34.         if (!$this->elasticsearchHelper->allowIndexing()) {
  35.             return;
  36.         }
  37.         foreach ($event->getWriteResults() as $writeResult) {
  38.             if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
  39.                 continue;
  40.             }
  41.             $languageId $writeResult->getProperty('languageId');
  42.             $esIndex $this->elasticsearchHelper->getIndexName($this->productDefinition$languageId);
  43.             // index exists, don't need to do anything
  44.             if ($this->client->indices()->exists(['index' => $esIndex])) {
  45.                 continue;
  46.             }
  47.             $this->bus->dispatch(new ElasticsearchLanguageIndexIteratorMessage($languageId));
  48.         }
  49.     }
  50. }