Skip to content

Commit

Permalink
Merge pull request Sylius#4103 from Zales0123/remove-order-item-based…
Browse files Browse the repository at this point in the history
…-promotions

[Core] Removed OrderItem based promotions
  • Loading branch information
michalmarcinkowski committed Feb 11, 2016
2 parents fe6dc45 + 26f3ee5 commit 71107a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 86 deletions.
67 changes: 0 additions & 67 deletions Model/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@

namespace Sylius\Component\Core\Model;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Cart\Model\CartItem;
use Sylius\Component\Order\Model\OrderItemInterface as BaseOrderItemInterface;
use Sylius\Component\Promotion\Model\PromotionInterface as BasePromotionInterface;

/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
Expand All @@ -27,18 +24,6 @@ class OrderItem extends CartItem implements OrderItemInterface
*/
protected $variant;

/**
* @var Collection|BasePromotionInterface[]
*/
protected $promotions;

public function __construct()
{
parent::__construct();

$this->promotions = new ArrayCollection();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -72,56 +57,4 @@ public function equals(BaseOrderItemInterface $item)
&& $item->getVariant() === $this->variant
);
}

/**
* {@inheritdoc}
*/
public function getPromotionSubjectTotal()
{
return $this->getTotal();
}

/**
* {@inheritdoc}
*/
public function getPromotionSubjectCount()
{
return $this->quantity;
}

/**
* {@inheritdoc}
*/
public function hasPromotion(BasePromotionInterface $promotion)
{
return $this->promotions->contains($promotion);
}

/**
* {@inheritdoc}
*/
public function addPromotion(BasePromotionInterface $promotion)
{
if (!$this->hasPromotion($promotion)) {
$this->promotions->add($promotion);
}
}

/**
* {@inheritdoc}
*/
public function removePromotion(BasePromotionInterface $promotion)
{
if ($this->hasPromotion($promotion)) {
$this->promotions->removeElement($promotion);
}
}

/**
* {@inheritdoc}
*/
public function getPromotions()
{
return $this->promotions;
}
}
3 changes: 1 addition & 2 deletions Model/OrderItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
namespace Sylius\Component\Core\Model;

use Sylius\Component\Cart\Model\CartItemInterface;
use Sylius\Component\Promotion\Model\PromotionCountableSubjectInterface;

/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
interface OrderItemInterface extends CartItemInterface, PromotionCountableSubjectInterface
interface OrderItemInterface extends CartItemInterface
{
/**
* @return ProductInterface
Expand Down
55 changes: 41 additions & 14 deletions Promotion/Checker/ContainsProductRuleChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Promotion\Checker\ItemCountRuleChecker;
use Sylius\Component\Promotion\Checker\RuleCheckerInterface;
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;

/**
* Checks if order contains the given variant.
*
* @author Alexandre Bacco <alexandre.bacco@gmail.com>
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class ContainsProductRuleChecker extends ItemCountRuleChecker
class ContainsProductRuleChecker implements RuleCheckerInterface
{
/**
* {@inheritdoc}
Expand All @@ -35,17 +34,11 @@ public function isEligible(PromotionSubjectInterface $subject, array $configurat

/* @var $item OrderItemInterface */
foreach ($subject->getItems() as $item) {
if ($configuration['variant'] == $item->getVariant()->getId()) {
if (!$configuration['exclude']) {
if (isset($configuration['count'])) {
return parent::isEligible($item, $configuration);
}

return true;
}

return false;
if ($configuration['variant'] != $item->getVariant()->getId()) {
continue;
}

return $this->isItemEligible($item, $configuration);
}

return (bool) $configuration['exclude'];
Expand All @@ -58,4 +51,38 @@ public function getConfigurationFormType()
{
return 'sylius_promotion_rule_contains_product_configuration';
}

/**
* @param OrderItemInterface $item
* @param array $configuration
*
* @return bool
*/
private function isItemEligible(OrderItemInterface $item, array $configuration)
{
if (!$configuration['exclude']) {
if (isset($configuration['count'])) {
return $this->isItemQuantityEligible($item->getQuantity(), $configuration);
}

return true;
}

return false;
}

/**
* @param int $quantity
* @param array $configuration
*
* @return bool
*/
private function isItemQuantityEligible($quantity, array $configuration)
{
if (isset($configuration['equal']) && $configuration['equal']) {
return $quantity >= $configuration['count'];
}

return $quantity > $configuration['count'];
}
}
6 changes: 3 additions & 3 deletions spec/Promotion/Checker/ContainsProductRuleCheckerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
class ContainsProductRuleCheckerSpec extends ObjectBehavior
{
function it_is_initialibable()
function it_is_initializable()
{
$this->shouldHaveType(ContainsProductRuleChecker::class);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ function it_returns_true_if_variant_is_included_and_count_is_set_smaller_amount_
$orderItem->getVariant()->willReturn($variant);
$variant->getId()->willReturn(1);

$orderItem->getPromotionSubjectCount()->willReturn(10);
$orderItem->getQuantity()->willReturn(10);

$this->isEligible($subject, ['variant' => 1, 'exclude' => false, 'count' => 2])->shouldReturn(true);
}
Expand All @@ -111,7 +111,7 @@ function it_returns_false_if_variant_is_included_and_count_is_set_bigger_amount_
$orderItem->getVariant()->willReturn($variant);
$variant->getId()->willReturn(1);

$orderItem->getPromotionSubjectCount()->willReturn(1);
$orderItem->getQuantity()->willReturn(1);

$this->isEligible($subject, ['variant' => 1, 'exclude' => false, 'count' => 2])->shouldReturn(false);
}
Expand Down

0 comments on commit 71107a0

Please sign in to comment.