Skip to content

Commit

Permalink
Merge pull request #144 from delyriand/feature/product-datasource
Browse files Browse the repository at this point in the history
Refactoring of the RepositoryDatasource class and add a datasource for the products
  • Loading branch information
maximehuran authored Jun 30, 2023
2 parents 56d4cee + 972499d commit b43674b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/Model/Datasource/DatasourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@

interface DatasourceInterface
{
public const DEFAULT_MAX_PER_PAGE = 100;

public function getItems(string $sourceClass): iterable;
}
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 <[email protected]>
*
* 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;
}
}
31 changes: 20 additions & 11 deletions src/Model/Datasource/RepositoryDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Sylius\Component\Resource\Repository\RepositoryInterface;

Expand All @@ -31,19 +32,27 @@ public function getItems(string $sourceClass): iterable
{
/** @phpstan-ignore-next-line */
$repository = $this->entityManager->getRepository($sourceClass);
$paginator = $this->getPaginator($repository);

$page = 1;
$paginator->setMaxPerPage(self::DEFAULT_MAX_PER_PAGE);
do {
$paginator->setCurrentPage($page);
foreach ($paginator as $item) {
yield $item;
}
$page = $paginator->hasNextPage() ? $paginator->getNextPage() : 1;
} while ($paginator->hasNextPage());

return null;
}

private function getPaginator(EntityRepository $repository): Pagerfanta
{
if ($repository instanceof RepositoryInterface && ($paginator = $repository->createPaginator()) instanceof Pagerfanta) {
$page = 1;
do {
$paginator->setCurrentPage($page);
foreach ($paginator as $item) {
yield $item;
}
$page = $paginator->hasNextPage() ? $paginator->getNextPage() : 1;
} while ($paginator->hasNextPage());

return null;
return $paginator;
}

return $repository instanceof EntityRepository ? $repository->createQueryBuilder('o')->getQuery()->toIterable() : null;
return new Pagerfanta(new QueryAdapter($repository->createQueryBuilder('o'), false, false));
}
}
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 b43674b

Please sign in to comment.