Skip to content

Commit

Permalink
[BC] keep previous services implementation in Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
SirDomin committed Dec 8, 2021
1 parent 05916ee commit db86ccc
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
</service>

<service id="sylius.promotion_action.fixed_discount" class="Sylius\Component\Core\Promotion\Action\FixedDiscountPromotionActionCommand">
<argument type="service" id="sylius.proportional_integer_distributor" />
<argument type="service" id="sylius.promotion.units_promotion_adjustments_applicator" />
<argument type="service" id="Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface" />
<argument type="service" id="sylius.currency_converter" />
Expand All @@ -72,6 +73,7 @@
<tag name="sylius.promotion_action" type="unit_fixed_discount" label="sylius.form.promotion_action.item_fixed_discount" form-type="Sylius\Bundle\CoreBundle\Form\Type\Promotion\Action\ChannelBasedUnitFixedDiscountConfigurationType" />
</service>
<service id="sylius.promotion_action.percentage_discount" class="Sylius\Component\Core\Promotion\Action\PercentageDiscountPromotionActionCommand">
<argument type="service" id="sylius.proportional_integer_distributor" />
<argument type="service" id="sylius.promotion.units_promotion_adjustments_applicator" />
<argument type="service" id="Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface" />
<tag name="sylius.promotion_action" type="order_percentage_discount" label="sylius.form.promotion_action.order_percentage_discount" form-type="Sylius\Bundle\PromotionBundle\Form\Type\Action\PercentageDiscountConfigurationType" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Webmozart\Assert\Assert;

final class MinimumPriceDistributor implements MinimumPriceDistributorInterface
{
Expand All @@ -28,6 +29,8 @@ public function __construct(ProportionalIntegerDistributorInterface $proportiona

public function distribute(array $orderItems, int $amount, ChannelInterface $channel): array
{
Assert::allImplementsInterface($orderItems, OrderItemInterface::class);

$orderItemsToProcess = [];
foreach ($orderItems as $index => $orderItem) {
/** @var ProductVariantInterface $variant */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\Component\Core\Promotion\Action;

use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface;
use Sylius\Component\Core\Distributor\ProportionalIntegerDistributorInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Promotion\Applicator\UnitsPromotionAdjustmentsApplicatorInterface;
use Sylius\Component\Promotion\Model\PromotionInterface;
Expand All @@ -24,14 +25,18 @@ final class FixedDiscountPromotionActionCommand extends DiscountPromotionActionC
{
public const TYPE = 'order_fixed_discount';

private ProportionalIntegerDistributorInterface $distributor;

private UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator;

private MinimumPriceDistributorInterface $minimumPriceDistributor;
private ?MinimumPriceDistributorInterface $minimumPriceDistributor;

public function __construct(
ProportionalIntegerDistributorInterface $distributor,
UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator,
MinimumPriceDistributorInterface $minimumPriceDistributor
?MinimumPriceDistributorInterface $minimumPriceDistributor = null
) {
$this->distributor = $distributor;
$this->unitsPromotionAdjustmentsApplicator = $unitsPromotionAdjustmentsApplicator;
$this->minimumPriceDistributor = $minimumPriceDistributor;
}
Expand Down Expand Up @@ -65,7 +70,16 @@ public function execute(PromotionSubjectInterface $subject, array $configuration
return false;
}

$splitPromotion = $this->minimumPriceDistributor->distribute($subject->getItems()->toArray(), $promotionAmount, $subject->getChannel());
if ($this->minimumPriceDistributor !== null) {
$splitPromotion = $this->minimumPriceDistributor->distribute($subject->getItems()->toArray(), $promotionAmount, $subject->getChannel());
} else {
$itemsTotal = [];
foreach ($subject->getItems() as $orderItem) {
$itemsTotal[] = $orderItem->getTotal();
}

$splitPromotion = $this->distributor->distribute($itemsTotal, $promotionAmount);
}

$this->unitsPromotionAdjustmentsApplicator->apply($subject, $promotion, $splitPromotion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\Component\Core\Promotion\Action;

use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface;
use Sylius\Component\Core\Distributor\ProportionalIntegerDistributorInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Promotion\Applicator\UnitsPromotionAdjustmentsApplicatorInterface;
use Sylius\Component\Promotion\Action\PromotionActionCommandInterface;
Expand All @@ -25,14 +26,18 @@ final class PercentageDiscountPromotionActionCommand extends DiscountPromotionAc
{
public const TYPE = 'order_percentage_discount';

private ProportionalIntegerDistributorInterface $distributor;

private UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator;

private MinimumPriceDistributorInterface $minimumPriceDistributor;
private ?MinimumPriceDistributorInterface $minimumPriceDistributor;

public function __construct(
ProportionalIntegerDistributorInterface $distributor,
UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator,
MinimumPriceDistributorInterface $minimumPriceDistributor
?MinimumPriceDistributorInterface $minimumPriceDistributor = null
) {
$this->distributor = $distributor;
$this->unitsPromotionAdjustmentsApplicator = $unitsPromotionAdjustmentsApplicator;
$this->minimumPriceDistributor = $minimumPriceDistributor;
}
Expand All @@ -57,7 +62,16 @@ public function execute(PromotionSubjectInterface $subject, array $configuration
return false;
}

$splitPromotion = $this->minimumPriceDistributor->distribute($subject->getItems()->toArray(), $promotionAmount, $subject->getChannel());
if ($this->minimumPriceDistributor !== null) {
$splitPromotion = $this->minimumPriceDistributor->distribute($subject->getItems()->toArray(), $promotionAmount, $subject->getChannel());
} else {
$itemsTotal = [];
foreach ($subject->getItems() as $orderItem) {
$itemsTotal[] = $orderItem->getTotal();
}

$splitPromotion = $this->distributor->distribute($itemsTotal, $promotionAmount);
}

$this->unitsPromotionAdjustmentsApplicator->apply($subject, $promotion, $splitPromotion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface;
use Sylius\Component\Core\Distributor\ProportionalIntegerDistributorInterface;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
Expand All @@ -32,10 +33,12 @@
final class FixedDiscountPromotionActionCommandSpec extends ObjectBehavior
{
function let(
ProportionalIntegerDistributorInterface $distributor,
UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator,
MinimumPriceDistributorInterface $minimumPriceDistributor
): void {
$this->beConstructedWith(
$distributor,
$unitsPromotionAdjustmentsApplicator,
$minimumPriceDistributor
);
Expand Down Expand Up @@ -90,6 +93,58 @@ function it_uses_a_distributor_and_applicator_to_execute_promotion_action(
$this->execute($order, ['WEB_US' => ['amount' => 1000]], $promotion)->shouldReturn(true);
}

function it_distributes_promotion_using_regular_distributor_if_minimum_price_distributor_is_not_provided(
ChannelInterface $channel,
OrderInterface $order,
OrderItemInterface $firstItem,
OrderItemInterface $secondItem,
PromotionInterface $promotion,
MinimumPriceDistributorInterface $minimumPriceDistributor,
ProportionalIntegerDistributorInterface $distributor,
UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator,
ProductVariantInterface $productVariantOne,
ProductVariantInterface $productVariantTwo,
ChannelPricingInterface $channelPricingOne,
ChannelPricingInterface $channelPricingTwo
): void {
$this->beConstructedWith(
$distributor,
$unitsPromotionAdjustmentsApplicator
);

$order->getCurrencyCode()->willReturn('USD');
$order->getChannel()->willReturn($channel);
$channel->getCode()->willReturn('WEB_US');

$order->countItems()->willReturn(2);

$order
->getItems()
->willReturn(new ArrayCollection([$firstItem->getWrappedObject(), $secondItem->getWrappedObject()]))
;

$firstItem->getVariant()->willReturn($productVariantOne);
$secondItem->getVariant()->willReturn($productVariantTwo);
$productVariantOne->getChannelPricingForChannel($channel)->willReturn($channelPricingOne);
$productVariantTwo->getChannelPricingForChannel($channel)->willReturn($channelPricingTwo);

$channelPricingOne->getMinimumPrice()->willReturn(5800);
$channelPricingTwo->getMinimumPrice()->willReturn(0);

$order->getPromotionSubjectTotal()->willReturn(10000);
$firstItem->getTotal()->willReturn(6000);
$firstItem->getQuantity()->willReturn(1);
$secondItem->getTotal()->willReturn(4000);
$secondItem->getQuantity()->willReturn(1);

$minimumPriceDistributor->distribute([$firstItem, $secondItem], -1000, $channel)->shouldNotBeCalled();
$distributor->distribute([6000, 4000], -1000)->willReturn([-200, -800]);

$unitsPromotionAdjustmentsApplicator->apply($order, $promotion, [-200, -800])->shouldBeCalled();

$this->execute($order, ['WEB_US' => ['amount' => 1000]], $promotion)->shouldReturn(true);
}

function it_distributes_promotion_up_to_minimum_price_of_variant(
ChannelInterface $channel,
OrderInterface $order,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface;
use Sylius\Component\Core\Distributor\ProportionalIntegerDistributorInterface;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
Expand All @@ -32,10 +33,11 @@
final class PercentageDiscountPromotionActionCommandSpec extends ObjectBehavior
{
function let(
ProportionalIntegerDistributorInterface $distributor,
UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator,
MinimumPriceDistributorInterface $minimumPriceDistributor
): void {
$this->beConstructedWith($unitsPromotionAdjustmentsApplicator, $minimumPriceDistributor);
$this->beConstructedWith($distributor, $unitsPromotionAdjustmentsApplicator, $minimumPriceDistributor);
}

function it_implements_a_promotion_action_interface(): void
Expand Down Expand Up @@ -82,6 +84,50 @@ function it_uses_distributor_and_applicator_to_execute_promotion_action(
$this->execute($order, ['percentage' => 0.1], $promotion)->shouldReturn(true);
}

function it_distributes_promotion_using_regular_distributor_if_minimum_price_distributor_is_not_provided(
OrderInterface $order,
OrderItemInterface $firstItem,
OrderItemInterface $secondItem,
PromotionInterface $promotion,
ProportionalIntegerDistributorInterface $distributor,
MinimumPriceDistributorInterface $minimumPriceDistributor,
UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator,
ProductVariantInterface $productVariantOne,
ProductVariantInterface $productVariantTwo,
ChannelPricingInterface $channelPricingOne,
ChannelPricingInterface $channelPricingTwo,
ChannelInterface $channel
): void {
$this->beConstructedWith($distributor, $unitsPromotionAdjustmentsApplicator);

$order->countItems()->willReturn(2);
$order->getChannel()->willReturn($channel);

$order->getItems()->willReturn(new ArrayCollection([$firstItem->getWrappedObject(), $secondItem->getWrappedObject()]));

$firstItem->getTotal()->willReturn(200);
$firstItem->getQuantity()->willReturn(1);
$secondItem->getTotal()->willReturn(800);
$secondItem->getQuantity()->willReturn(1);

$firstItem->getVariant()->willReturn($productVariantOne);
$secondItem->getVariant()->willReturn($productVariantTwo);
$productVariantOne->getChannelPricingForChannel($channel)->willReturn($channelPricingOne);
$productVariantTwo->getChannelPricingForChannel($channel)->willReturn($channelPricingTwo);

$channelPricingOne->getMinimumPrice()->willReturn(0);
$channelPricingTwo->getMinimumPrice()->willReturn(0);

$order->getPromotionSubjectTotal()->willReturn(10000);

$minimumPriceDistributor->distribute([$firstItem, $secondItem], -1000, $channel)->shouldNotBeCalled();
$distributor->distribute([200, 800], -1000)->willReturn([-200, -800]);

$unitsPromotionAdjustmentsApplicator->apply($order, $promotion, [-200, -800])->shouldBeCalled();

$this->execute($order, ['percentage' => 0.1], $promotion)->shouldReturn(true);
}

function it_distributes_promotion_amount_taking_minimum_price_into_account(
OrderInterface $order,
OrderItemInterface $firstItem,
Expand Down

0 comments on commit db86ccc

Please sign in to comment.