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

  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(private readonly ElasticsearchHelper $elasticsearchHelper, private readonly ProductDefinition $productDefinition, private readonly Client $client, private readonly MessageBusInterface $bus)
  20.     {
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             'sales_channel_language.written' => 'onSalesChannelWritten',
  26.         ];
  27.     }
  28.     public function onSalesChannelWritten(EntityWrittenEvent $event): void
  29.     {
  30.         if (!$this->elasticsearchHelper->allowIndexing()) {
  31.             return;
  32.         }
  33.         foreach ($event->getWriteResults() as $writeResult) {
  34.             if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
  35.                 continue;
  36.             }
  37.             $languageId $writeResult->getProperty('languageId');
  38.             $esIndex $this->elasticsearchHelper->getIndexName($this->productDefinition$languageId);
  39.             // index exists, don't need to do anything
  40.             if ($this->client->indices()->exists(['index' => $esIndex])) {
  41.                 continue;
  42.             }
  43.             $this->bus->dispatch(new ElasticsearchLanguageIndexIteratorMessage($languageId));
  44.         }
  45.     }
  46. }