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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the same input for Coupon code and GiftCard #202

Draft
wants to merge 1 commit into
base: 0.12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false,
"ergebnis/composer-normalize": true,
"symfony/thanks": true
"symfony/thanks": true,
"composer/package-versions-deprecated": true
},
"sort-packages": true
},
Expand Down
17 changes: 4 additions & 13 deletions src/Applicator/GiftCardApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,12 @@ public function __construct(
}

/**
* @param string|GiftCardInterface|mixed $giftCard
* @throws GiftCardNotFoundException if gift card is not found
* @throws ChannelMismatchException if the orders channel does not match the gift cards channel
*/
public function apply(OrderInterface $order, $giftCard): void
public function apply(OrderInterface $order, string $giftCardOrPromotionCode): void
{
if (is_string($giftCard)) {
$giftCard = $this->getGiftCard($giftCard);
}

if (!$giftCard instanceof GiftCardInterface) {
throw new GiftCardNotFoundException($giftCard);
}
$giftCard = $this->getGiftCard($giftCardOrPromotionCode);

$orderChannel = $order->getChannel();
if (null === $orderChannel) {
Expand All @@ -59,10 +54,6 @@ public function apply(OrderInterface $order, $giftCard): void
}

$order->addGiftCard($giftCard);

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

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

/**
Expand Down
14 changes: 1 addition & 13 deletions src/Applicator/GiftCardApplicatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@

namespace Setono\SyliusGiftCardPlugin\Applicator;

use Setono\SyliusGiftCardPlugin\Exception\ChannelMismatchException;
use Setono\SyliusGiftCardPlugin\Exception\GiftCardNotFoundException;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Model\OrderInterface;

interface GiftCardApplicatorInterface
interface GiftCardApplicatorInterface extends GiftCardOrPromotionApplicatorInterface
{
/**
* Applies $giftCard to $order
*
* @param string|GiftCardInterface $giftCard
*
* @throws GiftCardNotFoundException if gift card is not found
* @throws ChannelMismatchException if the orders channel does not match the gift cards channel
*/
public function apply(OrderInterface $order, $giftCard): void;

/**
* @param string|GiftCardInterface|mixed $giftCard
*/
Expand Down
49 changes: 49 additions & 0 deletions src/Applicator/GiftCardOrPromotionApplicator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Applicator;

use Doctrine\Persistence\ObjectManager;
use Laminas\Stdlib\PriorityQueue;
use Setono\SyliusGiftCardPlugin\Exception\ExceptionInterface;
use Setono\SyliusGiftCardPlugin\Model\OrderInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;

final class GiftCardOrPromotionApplicator implements GiftCardOrPromotionApplicatorInterface
{
private PriorityQueue $applicators;

private OrderProcessorInterface $orderProcessor;

private ObjectManager $orderManager;

public function __construct(OrderProcessorInterface $orderProcessor, ObjectManager $orderManager)
{
$this->applicators = new PriorityQueue();

$this->orderProcessor = $orderProcessor;
$this->orderManager = $orderManager;
}

public function addApplicator(GiftCardOrPromotionApplicatorInterface $applicator, int $priority = 0): void
{
$this->applicators->insert($applicator, $priority);
}

public function apply(OrderInterface $order, string $giftCardOrPromotionCode): void
{
foreach ($this->applicators->toArray() as $applicator) {
try {
$applicator->apply($order, $giftCardOrPromotionCode);

break;
} catch (ExceptionInterface $e) {
}
}

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

$this->orderManager->flush();
}
}
12 changes: 12 additions & 0 deletions src/Applicator/GiftCardOrPromotionApplicatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Applicator;

use Setono\SyliusGiftCardPlugin\Model\OrderInterface;

interface GiftCardOrPromotionApplicatorInterface
{
public function apply(OrderInterface $order, string $giftCardOrPromotionCode): void;
}
29 changes: 29 additions & 0 deletions src/Applicator/PromotionApplicator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Applicator;

use Setono\SyliusGiftCardPlugin\Exception\PromotionCouponNotFoundException;
use Setono\SyliusGiftCardPlugin\Model\OrderInterface;
use Sylius\Component\Promotion\Repository\PromotionCouponRepositoryInterface;

final class PromotionApplicator implements GiftCardOrPromotionApplicatorInterface
{
private PromotionCouponRepositoryInterface $promotionCouponRepository;

public function __construct(PromotionCouponRepositoryInterface $promotionCouponRepository)
{
$this->promotionCouponRepository = $promotionCouponRepository;
}

public function apply(OrderInterface $order, string $giftCardOrPromotionCode): void
{
$promotionCoupon = $this->promotionCouponRepository->findOneBy(['code' => $giftCardOrPromotionCode]);
if (null === $promotionCoupon) {
throw new PromotionCouponNotFoundException($giftCardOrPromotionCode);
}

$order->setPromotionCoupon($promotionCoupon);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Setono\SyliusGiftCardPlugin\Controller\Action;

use Setono\SyliusGiftCardPlugin\Applicator\GiftCardApplicatorInterface;
use Setono\SyliusGiftCardPlugin\Form\Type\AddGiftCardToOrderType;
use Setono\SyliusGiftCardPlugin\Applicator\GiftCardOrPromotionApplicatorInterface;
use Setono\SyliusGiftCardPlugin\Form\Type\AddGiftCardOrPromotionToOrderType;
use Setono\SyliusGiftCardPlugin\Model\OrderInterface;
use Setono\SyliusGiftCardPlugin\Resolver\RedirectUrlResolverInterface;
use Sylius\Component\Order\Context\CartContextInterface;
Expand All @@ -16,17 +16,16 @@
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;
use Webmozart\Assert\Assert;

final class AddGiftCardToOrderAction
final class AddGiftCardOrPromotionToOrderAction
{
private FormFactoryInterface $formFactory;

private CartContextInterface $cartContext;

private FlashBagInterface $flashBag;

private GiftCardApplicatorInterface $giftCardApplicator;
private GiftCardOrPromotionApplicatorInterface $giftCardOrPromotionApplicator;

private RedirectUrlResolverInterface $redirectRouteResolver;

Expand All @@ -36,14 +35,14 @@ public function __construct(
FormFactoryInterface $formFactory,
CartContextInterface $cartContext,
FlashBagInterface $flashBag,
GiftCardApplicatorInterface $giftCardApplicator,
GiftCardOrPromotionApplicatorInterface $giftCardOrPromotionApplicator,
RedirectUrlResolverInterface $redirectRouteResolver,
Environment $twig
) {
$this->formFactory = $formFactory;
$this->cartContext = $cartContext;
$this->flashBag = $flashBag;
$this->giftCardApplicator = $giftCardApplicator;
$this->giftCardOrPromotionApplicator = $giftCardOrPromotionApplicator;
$this->redirectRouteResolver = $redirectRouteResolver;
$this->twig = $twig;
}
Expand All @@ -57,14 +56,16 @@ public function __invoke(Request $request): Response
throw new NotFoundHttpException();
}

$addGiftCardToOrderCommand = new AddGiftCardToOrderCommand();
$form = $this->formFactory->create(AddGiftCardToOrderType::class, $addGiftCardToOrderCommand);
$addGiftCardOrPromotionToOrderCommand = new AddGiftCardOrPromotionToOrderCommand();
$form = $this->formFactory->create(
AddGiftCardOrPromotionToOrderType::class,
$addGiftCardOrPromotionToOrderCommand
);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$giftCard = $addGiftCardToOrderCommand->getGiftCard();
Assert::notNull($giftCard);
$this->giftCardApplicator->apply($order, $giftCard);
$giftCardOrPromotionCode = $addGiftCardOrPromotionToOrderCommand->getCode();
$this->giftCardOrPromotionApplicator->apply($order, $giftCardOrPromotionCode);

$this->flashBag->add('success', 'setono_sylius_gift_card.gift_card_added');

Expand Down
20 changes: 20 additions & 0 deletions src/Controller/Action/AddGiftCardOrPromotionToOrderCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Controller\Action;

final class AddGiftCardOrPromotionToOrderCommand
{
private ?string $code = null;

public function getCode(): ?string
{
return $this->code;
}

public function setCode(?string $code): void
{
$this->code = $code;
}
}
22 changes: 0 additions & 22 deletions src/Controller/Action/AddGiftCardToOrderCommand.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\DependencyInjection\Compiler;

use Setono\SyliusGiftCardPlugin\Applicator\GiftCardOrPromotionApplicatorInterface;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\PrioritizedCompositeServicePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterGiftCardOrPromotionApplicatorsPass extends PrioritizedCompositeServicePass
{
public const APPLICATOR_SERVICE_TAG = 'setono_sylius_gift_card.gift_card_or_promotion_applicator';

public function __construct()
{
parent::__construct(
'setono_sylius_gift_card.applicator.gift_card_or_promotion',
'setono_sylius_gift_card.applicator.gift_card_or_promotion.composite',
self::APPLICATOR_SERVICE_TAG,
'addApplicator'
);
}

public function process(ContainerBuilder $container): void
{
parent::process($container);

$container->setAlias(
GiftCardOrPromotionApplicatorInterface::class,
'setono_sylius_gift_card.gift_card_or_promotion_applicator.applicator'
);
}
}
25 changes: 25 additions & 0 deletions src/DependencyInjection/Compiler/UseSameInputPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class UseSameInputPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
/** @var bool $useSameInputForGiftCardAndCoupon */
$useSameInputForGiftCardAndCoupon = $container->getParameter(
'setono_sylius_gift_card.cart.use_same_input_for_promotion_and_gift_card'
);

if (!$useSameInputForGiftCardAndCoupon) {
if ($container->hasDefinition('setono_sylius_gift_card.applicator.promotion')) {
$container->removeDefinition('setono_sylius_gift_card.applicator.promotion');
}
}
}
}
7 changes: 7 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public function getConfigTreeBuilder(): TreeBuilder
->min(1)
->max(255)
->example(16)
->end()
->arrayNode('cart')
->addDefaultsIfNotSet()
->children()
->booleanNode('use_same_input_for_promotion_and_gift_card')->defaultFalse()->end()
->end()
->end()
;

$this->addResourcesSection($rootNode);
Expand Down
10 changes: 10 additions & 0 deletions src/DependencyInjection/SetonoSyliusGiftCardExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

$container->setParameter('setono_sylius_gift_card.code_length', $config['code_length']);
$useSameInputForPromotionAndGiftCard = false;
if (array_key_exists('cart', $config)
&& array_key_exists('use_same_input_for_promotion_and_gift_card', $config['cart'])
) {
$useSameInputForPromotionAndGiftCard = $config['cart']['use_same_input_for_promotion_and_gift_card'];
}
$container->setParameter(
'setono_sylius_gift_card.cart.use_same_input_for_promotion_and_gift_card',
$useSameInputForPromotionAndGiftCard
);

$this->registerResources('setono_sylius_gift_card', $config['driver'], $config['resources'], $container);

Expand Down
1 change: 1 addition & 0 deletions src/Exception/GiftCardNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class GiftCardNotFoundException extends InvalidArgumentException implement

public function __construct(string $giftCard)
{
$this->giftCard = $giftCard;
$message = sprintf('The gift card with code "%s" was not found', $giftCard);

parent::__construct($message);
Expand Down
Loading