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

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\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. *
  15. * When an language is created, we need to trigger an indexing for that
  16. */
  17. #[Package('core')]
  18. class LanguageSubscriber implements EventSubscriberInterface
  19. {
  20. private ElasticsearchHelper $elasticsearchHelper;
  21. private ProductDefinition $productDefinition;
  22. private Client $client;
  23. private MessageBusInterface $bus;
  24. public function __construct(ElasticsearchHelper $elasticsearchHelper, ProductDefinition $productDefinition, Client $client, MessageBusInterface $bus)
  25. {
  26. $this->elasticsearchHelper = $elasticsearchHelper;
  27. $this->productDefinition = $productDefinition;
  28. $this->client = $client;
  29. $this->bus = $bus;
  30. }
  31. public static function getSubscribedEvents(): array
  32. {
  33. return [
  34. 'sales_channel_language.written' => 'onSalesChannelWritten',
  35. ];
  36. }
  37. public function onSalesChannelWritten(EntityWrittenEvent $event): void
  38. {
  39. if (!$this->elasticsearchHelper->allowIndexing()) {
  40. return;
  41. }
  42. foreach ($event->getWriteResults() as $writeResult) {
  43. if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
  44. continue;
  45. }
  46. $languageId = $writeResult->getProperty('languageId');
  47. $esIndex = $this->elasticsearchHelper->getIndexName($this->productDefinition, $languageId);
  48. // index exists, don't need to do anything
  49. if ($this->client->indices()->exists(['index' => $esIndex])) {
  50. continue;
  51. }
  52. $this->bus->dispatch(new ElasticsearchLanguageIndexIteratorMessage($languageId));
  53. }
  54. }
  55. }