vendor/store.shopware.com/swagenterprisesearchplatform/src/Product/ProductSearchCriteriaSubscriber.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\ProductSearchCriteriaEvent;
  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 ProductSearchCriteriaSubscriber 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.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteria',
  30.         ];
  31.     }
  32.     public function onProductSearchCriteria(ProductSearchCriteriaEvent $event): void
  33.     {
  34.         $criteria $event->getCriteria();
  35.         $salesChannelContext $event->getSalesChannelContext();
  36.         $maxSearchCount $this->fetchLimit($salesChannelContext);
  37.         if (!$maxSearchCount) {
  38.             return;
  39.         }
  40.         $criteria->setLimit($maxSearchCount);
  41.     }
  42.     /**
  43.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  44.      */
  45.     private function fetchLimit(SalesChannelContext $salesChannelContext): ?int
  46.     {
  47.         $criteria = new Criteria();
  48.         $criteria
  49.             ->setLimit(1)
  50.             ->addFilter(new EqualsFilter('salesChannelId'$salesChannelContext->getSalesChannel()->getId()))
  51.             ->addFilter(new EqualsFilter('entityName'ProductDefinition::ENTITY_NAME));
  52.         /** @var GatewayConfigurationEntity|null $configuration */
  53.         $configuration $this->configRepository->search($criteria$salesChannelContext->getContext())->first();
  54.         if ($configuration === null) {
  55.             throw new GatewayNotConfiguredException('Search not configured for ' ProductDefinition::ENTITY_NAME);
  56.         }
  57.         return $configuration->getMaxSearchCount();
  58.     }
  59. }