Skip to content

Commit

Permalink
feat: add a product data source to index only enabled products that h…
Browse files Browse the repository at this point in the history
…ave a channel
  • Loading branch information
delyriand committed Jun 30, 2023
1 parent 35df45e commit 972499d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
57 changes: 57 additions & 0 deletions src/Model/Datasource/ProductDatasource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* 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);

Check failure on line 33 in src/Model/Datasource/ProductDatasource.php

View workflow job for this annotation

GitHub Actions / php (8.0)

Parameter #1 $className of method Doctrine\ORM\EntityManagerInterface::getRepository() expects class-string<object>, string given.

Check failure on line 33 in src/Model/Datasource/ProductDatasource.php

View workflow job for this annotation

GitHub Actions / php (8.0)

Unable to resolve the template type T in call to method Doctrine\ORM\EntityManagerInterface::getRepository()

Check failure on line 33 in src/Model/Datasource/ProductDatasource.php

View workflow job for this annotation

GitHub Actions / php (8.1)

Parameter #1 $className of method Doctrine\ORM\EntityManagerInterface::getRepository() expects class-string<object>, string given.

Check failure on line 33 in src/Model/Datasource/ProductDatasource.php

View workflow job for this annotation

GitHub Actions / php (8.1)

Unable to resolve the template type T in call to method Doctrine\ORM\EntityManagerInterface::getRepository()
/** @var ProductRepositoryInterface $repository */
Assert::isInstanceOf($repository, ProductRepositoryInterface::class);

$queryBuilder = $repository->createQueryBuilder('o')

Check failure on line 37 in src/Model/Datasource/ProductDatasource.php

View workflow job for this annotation

GitHub Actions / php (8.0)

Call to an undefined method Sylius\Component\Core\Repository\ProductRepositoryInterface::createQueryBuilder().

Check failure on line 37 in src/Model/Datasource/ProductDatasource.php

View workflow job for this annotation

GitHub Actions / php (8.1)

Call to an undefined method Sylius\Component\Core\Repository\ProductRepositoryInterface::createQueryBuilder().
->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;
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/monsieurbiz_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
Expand Down

0 comments on commit 972499d

Please sign in to comment.