custom/plugins/SwagEnterpriseSearchPlatform/src/Product/ProductSuggestCriteriaSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\EnterpriseSearch\Product;
  3. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  4. use Shopware\Core\Content\Product\ProductDefinition;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Swag\EnterpriseSearch\Configuration\GatewayConfigurationEntity;
  10. use Swag\EnterpriseSearch\Configuration\GatewayNotConfiguredException;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductSuggestCriteriaSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var EntityRepositoryInterface
  16.      */
  17.     private $configRepository;
  18.     public function __construct(
  19.         EntityRepositoryInterface $configRepository
  20.     ) {
  21.         $this->configRepository $configRepository;
  22.     }
  23.     /**
  24.      * {@inheritdoc}
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             ProductSuggestCriteriaEvent::class => 'onProductSuggestCriteria',
  30.         ];
  31.     }
  32.     public function onProductSuggestCriteria(ProductSuggestCriteriaEvent $event): void
  33.     {
  34.         $criteria $event->getCriteria();
  35.         $salesChannelContext $event->getSalesChannelContext();
  36.         $machSuggestCount $this->fetchLimit($salesChannelContext);
  37.         $criteria->setLimit($machSuggestCount);
  38.     }
  39.     private function fetchLimit(SalesChannelContext $salesChannelContext): int
  40.     {
  41.         $criteria = new Criteria();
  42.         $criteria
  43.             ->setLimit(1)
  44.             ->addFilter(new EqualsFilter('salesChannelId'$salesChannelContext->getSalesChannel()->getId()))
  45.             ->addFilter(new EqualsFilter('entityName'ProductDefinition::ENTITY_NAME));
  46.         /** @var GatewayConfigurationEntity|null $configuration */
  47.         $configuration $this->configRepository->search($criteria$salesChannelContext->getContext())->first();
  48.         if ($configuration === null) {
  49.             throw new GatewayNotConfiguredException('Search not configured for ' ProductDefinition::ENTITY_NAME);
  50.         }
  51.         return $configuration->getMaxSuggestCount();
  52.     }
  53. }