From 258b1ad1bbed351ca241cab8718220d3a9690363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Mon, 27 Feb 2023 08:33:38 +0100 Subject: [PATCH] Move the flush of order outside the applicator because flushes are handled by Doctrine middleware for Symfony messages --- src/Applicator/GiftCardApplicator.php | 11 +---------- src/Controller/Action/AddGiftCardToOrderAction.php | 10 +++++++++- .../Action/RemoveGiftCardFromOrderAction.php | 10 +++++++++- src/Resources/config/services/applicator.xml | 8 ++------ src/Resources/config/services/controller.xml | 2 ++ 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Applicator/GiftCardApplicator.php b/src/Applicator/GiftCardApplicator.php index be63efd9..644a32b2 100644 --- a/src/Applicator/GiftCardApplicator.php +++ b/src/Applicator/GiftCardApplicator.php @@ -4,7 +4,6 @@ namespace Setono\SyliusGiftCardPlugin\Applicator; -use Doctrine\Persistence\ObjectManager; use RuntimeException; use Setono\SyliusGiftCardPlugin\Exception\ChannelMismatchException; use Setono\SyliusGiftCardPlugin\Exception\GiftCardNotFoundException; @@ -19,16 +18,12 @@ final class GiftCardApplicator implements GiftCardApplicatorInterface private OrderProcessorInterface $orderProcessor; - private ObjectManager $orderManager; - public function __construct( GiftCardRepositoryInterface $giftCardRepository, - OrderProcessorInterface $orderProcessor, - ObjectManager $orderManager + OrderProcessorInterface $orderProcessor ) { $this->giftCardRepository = $giftCardRepository; $this->orderProcessor = $orderProcessor; - $this->orderManager = $orderManager; } /** @@ -65,8 +60,6 @@ public function apply(OrderInterface $order, $giftCard): void $order->addGiftCard($giftCard); $this->orderProcessor->process($order); - - $this->orderManager->flush(); } /** @@ -81,8 +74,6 @@ public function remove(OrderInterface $order, $giftCard): void $order->removeGiftCard($giftCard); $this->orderProcessor->process($order); - - $this->orderManager->flush(); } private function getGiftCard(string $giftCardCode): GiftCardInterface diff --git a/src/Controller/Action/AddGiftCardToOrderAction.php b/src/Controller/Action/AddGiftCardToOrderAction.php index aafb8149..65aab0a1 100644 --- a/src/Controller/Action/AddGiftCardToOrderAction.php +++ b/src/Controller/Action/AddGiftCardToOrderAction.php @@ -4,6 +4,8 @@ namespace Setono\SyliusGiftCardPlugin\Controller\Action; +use Doctrine\Persistence\ManagerRegistry; +use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait; use Setono\SyliusGiftCardPlugin\Applicator\GiftCardApplicatorInterface; use Setono\SyliusGiftCardPlugin\Form\Type\AddGiftCardToOrderType; use Setono\SyliusGiftCardPlugin\Model\OrderInterface; @@ -20,6 +22,8 @@ final class AddGiftCardToOrderAction { + use ORMManagerTrait; + private FormFactoryInterface $formFactory; private CartContextInterface $cartContext; @@ -35,13 +39,15 @@ public function __construct( CartContextInterface $cartContext, GiftCardApplicatorInterface $giftCardApplicator, RedirectUrlResolverInterface $redirectRouteResolver, - Environment $twig + Environment $twig, + ManagerRegistry $managerRegistry ) { $this->formFactory = $formFactory; $this->cartContext = $cartContext; $this->giftCardApplicator = $giftCardApplicator; $this->redirectRouteResolver = $redirectRouteResolver; $this->twig = $twig; + $this->managerRegistry = $managerRegistry; } public function __invoke(Request $request): Response @@ -62,6 +68,8 @@ public function __invoke(Request $request): Response Assert::notNull($giftCard); $this->giftCardApplicator->apply($order, $giftCard); + $this->getManager($order)->flush(); + $session = $request->getSession(); if ($session instanceof Session) { $session->getFlashBag()->add('success', 'setono_sylius_gift_card.gift_card_added'); diff --git a/src/Controller/Action/RemoveGiftCardFromOrderAction.php b/src/Controller/Action/RemoveGiftCardFromOrderAction.php index d6104d40..a5130a1c 100644 --- a/src/Controller/Action/RemoveGiftCardFromOrderAction.php +++ b/src/Controller/Action/RemoveGiftCardFromOrderAction.php @@ -4,6 +4,8 @@ namespace Setono\SyliusGiftCardPlugin\Controller\Action; +use Doctrine\Persistence\ManagerRegistry; +use Setono\DoctrineObjectManagerTrait\ORM\ORMManagerTrait; use Setono\SyliusGiftCardPlugin\Applicator\GiftCardApplicatorInterface; use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface; use Setono\SyliusGiftCardPlugin\Model\OrderInterface; @@ -17,6 +19,8 @@ final class RemoveGiftCardFromOrderAction { + use ORMManagerTrait; + private CartContextInterface $cartContext; private GiftCardApplicatorInterface $giftCardApplicator; @@ -26,11 +30,13 @@ final class RemoveGiftCardFromOrderAction public function __construct( CartContextInterface $cartContext, GiftCardApplicatorInterface $giftCardApplicator, - RedirectUrlResolverInterface $redirectRouteResolver + RedirectUrlResolverInterface $redirectRouteResolver, + ManagerRegistry $managerRegistry ) { $this->cartContext = $cartContext; $this->giftCardApplicator = $giftCardApplicator; $this->redirectRouteResolver = $redirectRouteResolver; + $this->managerRegistry = $managerRegistry; } public function __invoke(Request $request): Response @@ -44,6 +50,8 @@ public function __invoke(Request $request): Response $this->giftCardApplicator->remove($order, $giftCard); + $this->getManager($order)->flush(); + $session = $request->getSession(); if ($session instanceof Session) { $session->getFlashBag()->add('success', 'setono_sylius_gift_card.gift_card_removed'); diff --git a/src/Resources/config/services/applicator.xml b/src/Resources/config/services/applicator.xml index 36ad81a4..56f70eb2 100644 --- a/src/Resources/config/services/applicator.xml +++ b/src/Resources/config/services/applicator.xml @@ -3,15 +3,11 @@ - - - - + + - - diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml index 9e80fe1f..b7db4695 100644 --- a/src/Resources/config/services/controller.xml +++ b/src/Resources/config/services/controller.xml @@ -14,6 +14,7 @@ + +