Skip to content

Commit

Permalink
Merge pull request #244 from Setono/move-flush-outside-applicator
Browse files Browse the repository at this point in the history
Move the flush of order outside the applicator
  • Loading branch information
loevgaard authored Feb 27, 2023
2 parents 0d934ad + 258b1ad commit 1f575d4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
11 changes: 1 addition & 10 deletions src/Applicator/GiftCardApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Setono\SyliusGiftCardPlugin\Applicator;

use Doctrine\Persistence\ObjectManager;
use RuntimeException;
use Setono\SyliusGiftCardPlugin\Exception\ChannelMismatchException;
use Setono\SyliusGiftCardPlugin\Exception\GiftCardNotFoundException;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -65,8 +60,6 @@ public function apply(OrderInterface $order, $giftCard): void
$order->addGiftCard($giftCard);

$this->orderProcessor->process($order);

$this->orderManager->flush();
}

/**
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/Controller/Action/AddGiftCardToOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +22,8 @@

final class AddGiftCardToOrderAction
{
use ORMManagerTrait;

private FormFactoryInterface $formFactory;

private CartContextInterface $cartContext;
Expand All @@ -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
Expand All @@ -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');
Expand Down
10 changes: 9 additions & 1 deletion src/Controller/Action/RemoveGiftCardFromOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +19,8 @@

final class RemoveGiftCardFromOrderAction
{
use ORMManagerTrait;

private CartContextInterface $cartContext;

private GiftCardApplicatorInterface $giftCardApplicator;
Expand All @@ -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
Expand All @@ -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');
Expand Down
8 changes: 2 additions & 6 deletions src/Resources/config/services/applicator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="setono_sylius_gift_card.applicator.gift_card"
class="Setono\SyliusGiftCardPlugin\Applicator\GiftCardApplicator">
<argument type="service" id="setono_sylius_gift_card.repository.gift_card" />
<argument type="service" id="sylius.order_processing.order_processor.composite" />
<argument type="service" id="sylius.manager.order" />
<argument type="service" id="setono_sylius_gift_card.repository.gift_card"/>
<argument type="service" id="sylius.order_processing.order_processor.composite"/>
</service>

</services>

</container>
2 changes: 2 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
<argument type="service" id="setono_sylius_gift_card.applicator.gift_card"/>
<argument type="service" id="setono_sylius_gift_card.resolver.redirect_url"/>
<argument type="service" id="twig"/>
<argument type="service" id="doctrine"/>
</service>

<service id="setono_sylius_gift_card.controller.action.remove_gift_card_from_order"
class="Setono\SyliusGiftCardPlugin\Controller\Action\RemoveGiftCardFromOrderAction">
<argument type="service" id="sylius.context.cart"/>
<argument type="service" id="setono_sylius_gift_card.applicator.gift_card"/>
<argument type="service" id="setono_sylius_gift_card.resolver.redirect_url"/>
<argument type="service" id="doctrine"/>
</service>

<service id="setono_sylius_gift_card.controller.action.gift_card_balance"
Expand Down

0 comments on commit 1f575d4

Please sign in to comment.