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

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