-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from delyriand/feature/product-datasource
Refactoring of the RepositoryDatasource class and add a datasource for the products
- Loading branch information
Showing
4 changed files
with
80 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters