Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize flushes in Catalog Promotions processing #13133

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Bundle\CoreBundle\Listener;

use Doctrine\ORM\EntityManagerInterface;
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface;
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
Expand All @@ -27,14 +28,18 @@ final class CatalogPromotionUpdateListener

private RepositoryInterface $catalogPromotionRepository;

private EntityManagerInterface $entityManager;

public function __construct(
CatalogPromotionClearerInterface $catalogPromotionClearer,
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
RepositoryInterface $catalogPromotionRepository
RepositoryInterface $catalogPromotionRepository,
EntityManagerInterface $entityManager
) {
$this->catalogPromotionClearer = $catalogPromotionClearer;
$this->catalogPromotionProcessor = $catalogPromotionProcessor;
$this->catalogPromotionRepository = $catalogPromotionRepository;
$this->entityManager = $entityManager;
}

public function __invoke(CatalogPromotionUpdated $event): void
Expand All @@ -50,5 +55,7 @@ public function __invoke(CatalogPromotionUpdated $event): void
foreach ($this->catalogPromotionRepository->findAll() as $catalogPromotion) {
$this->catalogPromotionProcessor->process($catalogPromotion);
}

$this->entityManager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ final class CatalogPromotionClearer implements CatalogPromotionClearerInterface
{
private ChannelPricingRepositoryInterface $channelPricingRepository;

private EntityManagerInterface $entityManager;

public function __construct(
ChannelPricingRepositoryInterface $channelPricingRepository,
EntityManagerInterface $entityManager
) {
public function __construct(ChannelPricingRepositoryInterface $channelPricingRepository)
{
$this->channelPricingRepository = $channelPricingRepository;
$this->entityManager = $entityManager;
}

public function clear(): void
Expand All @@ -41,7 +36,5 @@ public function clear(): void
$channelPricing->setPrice($channelPricing->getOriginalPrice());
$channelPricing->clearAppliedPromotions();
}

$this->entityManager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ final class CatalogPromotionProcessor implements CatalogPromotionProcessorInterf

private CatalogPromotionApplicatorInterface $catalogPromotionApplicator;

private EntityManagerInterface $entityManager;

public function __construct(
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
CatalogPromotionApplicatorInterface $catalogPromotionApplicator,
EntityManagerInterface $entityManager
CatalogPromotionApplicatorInterface $catalogPromotionApplicator
) {
$this->catalogPromotionVariantsProvider = $catalogPromotionVariantsProvider;
$this->catalogPromotionApplicator = $catalogPromotionApplicator;
$this->entityManager = $entityManager;
}

public function process(CatalogPromotionInterface $catalogPromotion): void
Expand All @@ -48,7 +44,5 @@ public function process(CatalogPromotionInterface $catalogPromotion): void
foreach ($variants as $variant) {
$this->catalogPromotionApplicator->applyCatalogPromotion($variant, $catalogPromotion);
}

$this->entityManager->flush();
}
}
2 changes: 0 additions & 2 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,13 @@
>
<argument type="service" id="Sylius\Component\Core\Provider\CatalogPromotionVariantsProviderInterface" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface" />
<argument type="service" id="sylius.manager.product" />
</service>

<service
id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface"
class="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearer"
>
<argument type="service" id="sylius.repository.channel_pricing" />
<argument type="service" id="sylius.manager.channel_pricing" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface" />
<argument type="service" id="Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface" />
<argument type="service" id="sylius.repository.catalog_promotion" />
<argument type="service" id="doctrine.orm.entity_manager" />
<tag name="messenger.message_handler" bus="sylius.event_bus" />
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace spec\Sylius\Bundle\CoreBundle\Listener;

use Doctrine\ORM\EntityManagerInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionClearerInterface;
Expand All @@ -26,14 +27,21 @@ final class CatalogPromotionUpdateListenerSpec extends ObjectBehavior
function let(
CatalogPromotionClearerInterface $catalogPromotionClearer,
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
RepositoryInterface $catalogPromotionRepository
RepositoryInterface $catalogPromotionRepository,
EntityManagerInterface $entityManager
): void {
$this->beConstructedWith($catalogPromotionClearer, $catalogPromotionProcessor, $catalogPromotionRepository);
$this->beConstructedWith(
$catalogPromotionClearer,
$catalogPromotionProcessor,
$catalogPromotionRepository,
$entityManager
);
}

function it_processes_catalog_promotion_that_has_just_been_updated(
CatalogPromotionClearerInterface $catalogPromotionClearer,
CatalogPromotionProcessorInterface $catalogPromotionProcessor,
EntityManagerInterface $entityManager,
RepositoryInterface $catalogPromotionRepository,
CatalogPromotionInterface $firstCatalogPromotion,
CatalogPromotionInterface $secondCatalogPromotion
Expand All @@ -47,6 +55,8 @@ function it_processes_catalog_promotion_that_has_just_been_updated(
$catalogPromotionProcessor->process($firstCatalogPromotion)->shouldBeCalled();
$catalogPromotionProcessor->process($secondCatalogPromotion)->shouldBeCalled();

$entityManager->flush()->shouldBeCalled();

$this->__invoke(new CatalogPromotionUpdated('WINTER_MUGS_SALE'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
final class CatalogPromotionClearerSpec extends ObjectBehavior
{
function let(
ChannelPricingRepositoryInterface $channelPricingRepository,
EntityManagerInterface $entityManager
ChannelPricingRepositoryInterface $channelPricingRepository
): void {
$this->beConstructedWith($channelPricingRepository, $entityManager);
$this->beConstructedWith($channelPricingRepository);
}

function it_implements_catalog_promotion_clearer_interface(): void
Expand All @@ -35,7 +34,6 @@ function it_implements_catalog_promotion_clearer_interface(): void

function it_clears_channel_pricings_with_catalog_promotions_applied(
ChannelPricingRepositoryInterface $channelPricingRepository,
EntityManagerInterface $entityManager,
ChannelPricingInterface $firstChannelPricing,
ChannelPricingInterface $secondChannelPricing
): void {
Expand All @@ -53,8 +51,6 @@ function it_clears_channel_pricings_with_catalog_promotions_applied(
$secondChannelPricing->getOriginalPrice()->shouldNotBeCalled();
$secondChannelPricing->clearAppliedPromotions()->shouldNotBeCalled();

$entityManager->flush()->shouldBeCalled();

$this->clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\ORM\EntityManagerInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\Applicator\CatalogPromotionApplicatorInterface;
use Sylius\Bundle\CoreBundle\Processor\CatalogPromotionProcessorInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
Expand All @@ -25,13 +26,11 @@ final class CatalogPromotionProcessorSpec extends ObjectBehavior
{
function let(
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
CatalogPromotionApplicatorInterface $productCatalogPromotionApplicator,
EntityManagerInterface $entityManager
CatalogPromotionApplicatorInterface $productCatalogPromotionApplicator
): void {
$this->beConstructedWith(
$catalogPromotionVariantsProvider,
$productCatalogPromotionApplicator,
$entityManager
$productCatalogPromotionApplicator
);
}

Expand All @@ -43,7 +42,6 @@ function it_implements_catalog_promotion_processor_interface(): void
function it_applies_catalog_promotion_on_eligible_variants(
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
CatalogPromotionApplicatorInterface $productCatalogPromotionApplicator,
EntityManagerInterface $entityManager,
CatalogPromotionInterface $catalogPromotion,
ProductVariantInterface $firstVariant,
ProductVariantInterface $secondVariant
Expand All @@ -56,19 +54,17 @@ function it_applies_catalog_promotion_on_eligible_variants(
$productCatalogPromotionApplicator->applyCatalogPromotion($firstVariant, $catalogPromotion)->shouldBeCalled();
$productCatalogPromotionApplicator->applyCatalogPromotion($secondVariant, $catalogPromotion)->shouldBeCalled();

$entityManager->flush()->shouldBeCalled();

$this->process($catalogPromotion);
}

function it_does_nothing_if_there_are_no_eligible_variants(
CatalogPromotionVariantsProviderInterface $catalogPromotionVariantsProvider,
EntityManagerInterface $entityManager,
CatalogPromotionApplicatorInterface $productCatalogPromotionApplicator,
CatalogPromotionInterface $catalogPromotion
): void {
$catalogPromotionVariantsProvider->provideEligibleVariants($catalogPromotion)->willReturn([]);

$entityManager->flush()->shouldNotBeCalled();
$productCatalogPromotionApplicator->applyCatalogPromotion(Argument::any())->shouldNotBeCalled();

$this->process($catalogPromotion);
}
Expand Down