From 972499dc9cfaec88c45af824693b569fcce03db1 Mon Sep 17 00:00:00 2001 From: Maxime Leclercq Date: Fri, 21 Oct 2022 22:43:29 +0200 Subject: [PATCH] feat: add a product data source to index only enabled products that have a channel --- src/Model/Datasource/ProductDatasource.php | 57 ++++++++++++++++++++ src/Resources/config/monsieurbiz_search.yaml | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/Model/Datasource/ProductDatasource.php diff --git a/src/Model/Datasource/ProductDatasource.php b/src/Model/Datasource/ProductDatasource.php new file mode 100644 index 00000000..491fbdf3 --- /dev/null +++ b/src/Model/Datasource/ProductDatasource.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusSearchPlugin\Model\Datasource; + +use Doctrine\ORM\EntityManagerInterface; +use Pagerfanta\Doctrine\ORM\QueryAdapter; +use Pagerfanta\Pagerfanta; +use Sylius\Component\Core\Repository\ProductRepositoryInterface; +use Webmozart\Assert\Assert; + +class ProductDatasource implements DatasourceInterface +{ + private EntityManagerInterface $entityManager; + + public function __construct(EntityManagerInterface $entityManager) + { + $this->entityManager = $entityManager; + } + + public function getItems(string $sourceClass): iterable + { + $repository = $this->entityManager->getRepository($sourceClass); + /** @var ProductRepositoryInterface $repository */ + Assert::isInstanceOf($repository, ProductRepositoryInterface::class); + + $queryBuilder = $repository->createQueryBuilder('o') + ->andWhere('o.channels IS NOT EMPTY') + ->andWhere('o.enabled = :enabled') + ->setParameter('enabled', true) + ; + + $paginator = new Pagerfanta(new QueryAdapter($queryBuilder, false, false)); + $paginator->setMaxPerPage(self::DEFAULT_MAX_PER_PAGE); + $page = 1; + do { + $paginator->setCurrentPage($page); + + foreach ($paginator->getIterator() as $item) { + yield $item; + } + $page = $paginator->hasNextPage() ? $paginator->getNextPage() : 1; + } while ($paginator->hasNextPage()); + + return null; + } +} diff --git a/src/Resources/config/monsieurbiz_search.yaml b/src/Resources/config/monsieurbiz_search.yaml index aa5fd911..9598168e 100644 --- a/src/Resources/config/monsieurbiz_search.yaml +++ b/src/Resources/config/monsieurbiz_search.yaml @@ -14,7 +14,7 @@ monsieurbiz_sylius_search: item: '@MonsieurBizSyliusSearchPlugin/Search/product/_box.html.twig' instant: '@MonsieurBizSyliusSearchPlugin/Instant/Product/_box.html.twig' #mapping_provider: '...' # by default monsieurbiz.search.mapper_provider - #dataprovider: '...' # by default MonsieurBiz\SyliusSearchPlugin\Model\Datasource\RepositoryDatasource + datasource: 'MonsieurBiz\SyliusSearchPlugin\Model\Datasource\ProductDatasource' # by default MonsieurBiz\SyliusSearchPlugin\Model\Datasource\RepositoryDatasource automapper_classes: sources: product: '%sylius.model.product.class%'