Skip to content

Commit

Permalink
added dependency on doctrine orm batcher bundle instead of the library
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jul 11, 2019
1 parent d7e2c6f commit 2057e92
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": "^7.1",
"setono/doctrine-orm-batcher": "^0.4.1",
"setono/doctrine-orm-batcher-bundle": "^0.2.0",
"sylius/sylius": "^1.0"
},
"require-dev": {
Expand Down
48 changes: 20 additions & 28 deletions spec/Provider/ProductUrlProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace spec\SitemapPlugin\Provider;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\QueryBuilder;
use PhpSpec\ObjectBehavior;
use Setono\DoctrineORMBatcher\Batch\CollectionBatchInterface;
use Setono\DoctrineORMBatcher\Batcher\Collection\CollectionBatcherInterface;
use Setono\DoctrineORMBatcher\Factory\BatcherFactoryInterface;
use SitemapPlugin\Factory\AlternativeUrlFactoryInterface;
use SitemapPlugin\Factory\UrlFactoryInterface;
use SitemapPlugin\Generator\ProductImagesToSitemapImagesCollectionGeneratorInterface;
Expand Down Expand Up @@ -36,9 +37,10 @@ function let(
AlternativeUrlFactoryInterface $alternativeUrlFactory,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator,
BatcherFactoryInterface $batcherFactory
): void {
$this->beConstructedWith($repository, $router, $urlFactory, $alternativeUrlFactory, $localeContext, $channelContext, $productToImageSitemapArrayGenerator);
$this->beConstructedWith($repository, $router, $urlFactory, $alternativeUrlFactory, $localeContext, $channelContext, $productToImageSitemapArrayGenerator, $batcherFactory);
}

function it_is_initializable(): void
Expand All @@ -59,18 +61,18 @@ function it_generates_urls_for_the_unique_channel_locale(
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
LocaleInterface $locale,
Collection $products,
\Iterator $iterator,
ProductInterface $product,
ProductImageInterface $productImage,
ProductTranslation $productEnUSTranslation,
ProductTranslation $productNlNLTranslation,
UrlInterface $url,
AlternativeUrlInterface $alternativeUrl,
QueryBuilder $queryBuilder,
AbstractQuery $query,
ChannelInterface $channel,
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator,
BatcherFactoryInterface $batcherFactory,
CollectionBatcherInterface $batcher,
CollectionBatchInterface $collectionBatch
): void {
$now = new \DateTime();

Expand All @@ -90,15 +92,10 @@ function it_generates_urls_for_the_unique_channel_locale(
$queryBuilder->andWhere('o.enabled = :enabled')->willReturn($queryBuilder);
$queryBuilder->setParameter('channel', $channel)->willReturn($queryBuilder);
$queryBuilder->setParameter('enabled', true)->willReturn($queryBuilder);
$queryBuilder->getQuery()->willReturn($query);
$query->getResult()->willReturn($products);

$products->getIterator()->willReturn($iterator);
$iterator->valid()->willReturn(true, false);
$iterator->next()->shouldBeCalled();
$iterator->rewind()->shouldBeCalled();

$iterator->current()->willReturn($product);
$batcherFactory->createObjectCollectionBatcher($queryBuilder)->willReturn($batcher);
$batcher->getBatches()->willReturn([$collectionBatch]);
$collectionBatch->getCollection()->willReturn([$product]);

$productImage->getPath()->willReturn(null);

Expand Down Expand Up @@ -151,18 +148,18 @@ function it_generates_urls_for_all_channel_locales(
ChannelContextInterface $channelContext,
LocaleInterface $enUSLocale,
LocaleInterface $nlNLLocale,
Collection $products,
\Iterator $iterator,
ProductInterface $product,
ProductImageInterface $productImage,
ProductTranslation $productEnUSTranslation,
ProductTranslation $productNlNLTranslation,
UrlInterface $url,
AlternativeUrlInterface $alternativeUrl,
QueryBuilder $queryBuilder,
AbstractQuery $query,
ChannelInterface $channel,
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator,
BatcherFactoryInterface $batcherFactory,
CollectionBatcherInterface $batcher,
CollectionBatchInterface $collectionBatch
): void {
$now = new \DateTime();

Expand All @@ -184,15 +181,10 @@ function it_generates_urls_for_all_channel_locales(
$queryBuilder->andWhere('o.enabled = :enabled')->willReturn($queryBuilder);
$queryBuilder->setParameter('channel', $channel)->willReturn($queryBuilder);
$queryBuilder->setParameter('enabled', true)->willReturn($queryBuilder);
$queryBuilder->getQuery()->willReturn($query);
$query->getResult()->willReturn($products);

$products->getIterator()->willReturn($iterator);
$iterator->valid()->willReturn(true, false);
$iterator->next()->shouldBeCalled();
$iterator->rewind()->shouldBeCalled();

$iterator->current()->willReturn($product);
$batcherFactory->createObjectCollectionBatcher($queryBuilder)->willReturn($batcher);
$batcher->getBatches()->willReturn([$collectionBatch]);
$collectionBatch->getCollection()->willReturn([$product]);

$productImage->getPath()->willReturn(null);

Expand Down
21 changes: 11 additions & 10 deletions src/Provider/ProductUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace SitemapPlugin\Provider;

use Doctrine\Common\Collections\Collection;
use Safe\Exceptions\StringsException;
use Setono\DoctrineORMBatcher\Batcher\Collection\ObjectCollectionBatcher;
use Setono\DoctrineORMBatcher\Batch\CollectionBatchInterface;
use Setono\DoctrineORMBatcher\Factory\BatcherFactoryInterface;
use SitemapPlugin\Factory\AlternativeUrlFactoryInterface;
use SitemapPlugin\Factory\UrlFactoryInterface;
use SitemapPlugin\Generator\ProductImagesToSitemapImagesCollectionGeneratorInterface;
Expand Down Expand Up @@ -52,14 +52,18 @@ final class ProductUrlProvider implements UrlProviderInterface
/** @var ProductImagesToSitemapImagesCollectionGeneratorInterface */
private $productToImageSitemapArrayGenerator;

/** @var BatcherFactoryInterface */
private $batcherFactory;

public function __construct(
ProductRepositoryInterface $productRepository,
RouterInterface $router,
UrlFactoryInterface $urlFactory,
AlternativeUrlFactoryInterface $urlAlternativeFactory,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator,
BatcherFactoryInterface $batcherFactory
) {
$this->productRepository = $productRepository;
$this->router = $router;
Expand All @@ -68,16 +72,14 @@ public function __construct(
$this->localeContext = $localeContext;
$this->channelContext = $channelContext;
$this->productToImageSitemapArrayGenerator = $productToImageSitemapArrayGenerator;
$this->batcherFactory = $batcherFactory;
}

public function getName(): string
{
return 'products';
}

/**
* @throws StringsException
*/
public function generate(): iterable
{
foreach ($this->getProducts() as $product) {
Expand All @@ -103,9 +105,7 @@ private function localeInLocaleCodes(TranslationInterface $translation): bool
}

/**
* @throws StringsException
*
* @return array|Collection|ProductInterface[]
* @return iterable<ProductInterface>
*/
private function getProducts(): iterable
{
Expand All @@ -118,8 +118,9 @@ private function getProducts(): iterable
->setParameter('enabled', true)
;

$batcher = new ObjectCollectionBatcher($qb);
$batcher = $this->batcherFactory->createObjectCollectionBatcher($qb);

/** @var CollectionBatchInterface $batch */
foreach ($batcher->getBatches() as $batch) {
yield from $batch->getCollection();
}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/providers/products.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<argument type="service" id="sylius.context.locale" />
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="sylius.sitemap.generator.product_images_to_sitemap_images_collection" />
<argument type="service" id="setono_doctrine_orm_batcher.factory.batcher" />
<tag name="sylius.sitemap_provider" />
</service>
</services>
Expand Down
1 change: 1 addition & 0 deletions tests/Application/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Setono\DoctrineORMBatcherBundle\SetonoDoctrineORMBatcherBundle::class => ['all' => true],
];

0 comments on commit 2057e92

Please sign in to comment.