custom/plugins/LeopardSearchExtension/src/Subscriber/AbstractSubscriber.php line 52

Open in your IDE?
  1. <?php /** @noinspection PhpRedundantVariableDocTypeInspection */
  2. /**
  3.  * LeopardSearchExtension
  4.  * Copyright (c) Die Leoparden GmbH
  5.  */
  6. namespace LeopardSearchExtension\Subscriber;
  7. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  8. use Shopware\Core\Content\Product\ProductEntity;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. /**
  19.  * Class AbstractSubscriber
  20.  */
  21. abstract class AbstractSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var SystemConfigService
  25.      */
  26.     private $systemConfigService;
  27.     /**
  28.      * @var EntityRepositoryInterface
  29.      */
  30.     private $productRepository;
  31.     /**
  32.      * AbstractSubscriber constructor.
  33.      *
  34.      * @param SystemConfigService $systemConfigService
  35.      * @param EntityRepositoryInterface $productRepository
  36.      */
  37.     public function __construct(SystemConfigService $systemConfigServiceEntityRepositoryInterface $productRepository)
  38.     {
  39.         $this->systemConfigService $systemConfigService;
  40.         $this->productRepository $productRepository;
  41.     }
  42.     /**
  43.      * @param ProductListingCriteriaEvent $event
  44.      */
  45.     public function onSearchCriteria(ProductListingCriteriaEvent $event): void
  46.     {
  47.         /** @var array $config */
  48.         $config $this->getConfig($event->getSalesChannelContext());
  49.         /** @var string[] $searchTerm */
  50.         $filterTargets $config['filterTargets'];
  51.         if (empty($config['activated']) || empty($filterTargets)) {
  52.             return;
  53.         }
  54.         /** @var Request $request */
  55.         $request $event->getRequest();
  56.         /** @var string $searchTerm */
  57.         $searchTerm trim($request->get('search'));
  58.         /** @var ProductEntity|null $productEntity */
  59.         $productEntity $this->loadProductEntity($filterTargets$searchTerm$event->getContext());
  60.         $request->query->set('search'$productEntity $productEntity->getProductNumber() : $searchTerm);
  61.         if (!$productEntity instanceof ProductEntity) {
  62.             return;
  63.         }
  64.         $event->getCriteria()->addFilter(
  65.             new EqualsFilter(
  66.                 'productNumber',
  67.                 $productEntity->getProductNumber()
  68.             )
  69.         );
  70.     }
  71.     /**
  72.      * @return SystemConfigService
  73.      */
  74.     protected function getSystemConfigService(): SystemConfigService
  75.     {
  76.         return $this->systemConfigService;
  77.     }
  78.     /**
  79.      * @param SalesChannelContext $salesChannelContext
  80.      *
  81.      * @return array
  82.      */
  83.     protected function getConfig(SalesChannelContext $salesChannelContext): array
  84.     {
  85.         return $this->getSystemConfigService()->get('LeopardSearchExtension.config'$salesChannelContext->getSalesChannel()->getId());
  86.     }
  87.     /**
  88.      * @return EntityRepositoryInterface
  89.      */
  90.     protected function getProductRepository(): EntityRepositoryInterface
  91.     {
  92.         return $this->productRepository;
  93.     }
  94.     /**
  95.      * @param array $filterFields
  96.      * @param string $filterValue
  97.      * @param Context $context
  98.      * @return ProductEntity|null
  99.      */
  100.     protected function loadProductEntity(array $filterFieldsstring $filterValueContext $context): ?ProductEntity
  101.     {
  102.         $entitySearchResult $this->getProductRepository()->search(
  103.             (new Criteria())->addFilter(
  104.                 array_reduce($filterFields, function (MultiFilter $multiFilterstring $field) use ($filterValue) {
  105.                     return $multiFilter->addQuery(new EqualsFilter($field$filterValue));
  106.                 }, new MultiFilter(MultiFilter::CONNECTION_OR))),
  107.             $context
  108.         );
  109.         if ($entitySearchResult->count() === 1) {
  110.             return $entitySearchResult->first();
  111.         }
  112.         return null;
  113.     }
  114. }