custom/plugins/SwagEnterpriseSearchPlatform/src/Configuration/GatewayConfigurationCreator.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\EnterpriseSearch\Configuration;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\Product\Aggregate\ProductManufacturer\ProductManufacturerDefinition;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\System\SalesChannel\SalesChannelEvents;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class GatewayConfigurationCreator implements EventSubscriberInterface
  12. {
  13.     private const MAX_SEARCH_COUNT 10;
  14.     private const MAX_SUGGEST_COUNT 10;
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     private $entityRepository;
  19.     public function __construct(
  20.         EntityRepositoryInterface $entityRepository
  21.     ) {
  22.         $this->entityRepository $entityRepository;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             SalesChannelEvents::SALES_CHANNEL_WRITTEN => 'onSalesChannelWritten',
  28.         ];
  29.     }
  30.     public function onSalesChannelWritten(EntityWrittenEvent $salesChannelWrittenEvent): void
  31.     {
  32.         $salesChannelWrittenResults $salesChannelWrittenEvent->getWriteResults();
  33.         $newGatewayConfigurations = [];
  34.         foreach ($salesChannelWrittenResults as $salesChannelWrittenResult) {
  35.             if (!$this->isInsertOperation($salesChannelWrittenResult)) {
  36.                 continue;
  37.             }
  38.             $newGatewayConfigurations array_merge($this->createGatewayConfigurationsForSalesChannel($salesChannelWrittenResult), $newGatewayConfigurations);
  39.         }
  40.         if (empty($newGatewayConfigurations)) {
  41.             return;
  42.         }
  43.         $this->entityRepository->create($newGatewayConfigurations$salesChannelWrittenEvent->getContext());
  44.     }
  45.     protected function getEntityNames(): array
  46.     {
  47.         return [
  48.             ProductDefinition::ENTITY_NAME,
  49.             ProductManufacturerDefinition::ENTITY_NAME,
  50.             CategoryDefinition::ENTITY_NAME,
  51.         ];
  52.     }
  53.     private function createGatewayConfigurationsForSalesChannel(EntityWriteResult $createdSalesChannel): array
  54.     {
  55.         $entities = [];
  56.         foreach ($this->getEntityNames() as $entityName) {
  57.             $configuration = new GatewayConfigurationEntity();
  58.             $configuration->setEntityName($entityName);
  59.             $configuration->setMaxSuggestCount(self::MAX_SUGGEST_COUNT);
  60.             if ($entityName !== ProductDefinition::ENTITY_NAME) {
  61.                 $configuration->setMaxSearchCount(self::MAX_SEARCH_COUNT);
  62.             }
  63.             /** @var string $salesChannelId */
  64.             $salesChannelId $createdSalesChannel->getPrimaryKey();
  65.             $configuration->setSalesChannelId($salesChannelId);
  66.             $entities[] = $configuration->getVars();
  67.         }
  68.         return $entities;
  69.     }
  70.     private function isInsertOperation(EntityWriteResult $entityWriteResult): bool
  71.     {
  72.         return $entityWriteResult->getOperation() === EntityWriteResult::OPERATION_INSERT;
  73.     }
  74. }