/**
  * Get rule type
  *
  * @param RuleInterface $rule
  *
  * @return null|string
  */
 protected function getRegistryIdentifier($rule)
 {
     if ($rule instanceof RuleInterface && null !== $rule->getType()) {
         return $rule->getType();
     }
     if (null !== $this->registryIdentifier) {
         return $this->registryIdentifier;
     }
     return null;
 }
 function it_does_not_check_more_rules_if_one_has_returned_false(RuleCheckerInterface $cartQuantityRuleChecker, RuleCheckerInterface $itemTotalRuleChecker, RuleInterface $cartQuantityRule, RuleInterface $itemTotalRule, PromotionInterface $promotion, PromotionSubjectInterface $subject, ServiceRegistryInterface $rulesRegistry)
 {
     $promotion->hasRules()->willReturn(true);
     $promotion->getRules()->willReturn([$cartQuantityRule, $itemTotalRule]);
     $cartQuantityRule->getType()->willReturn(RuleInterface::TYPE_CART_QUANTITY);
     $cartQuantityRule->getConfiguration()->willReturn([]);
     $itemTotalRule->getType()->willReturn(RuleInterface::TYPE_ITEM_TOTAL);
     $itemTotalRule->getConfiguration()->willReturn([]);
     $rulesRegistry->get(RuleInterface::TYPE_CART_QUANTITY)->willReturn($cartQuantityRuleChecker);
     $rulesRegistry->get(RuleInterface::TYPE_ITEM_TOTAL)->willReturn($itemTotalRuleChecker);
     $cartQuantityRuleChecker->isEligible($subject, [])->willReturn(false);
     $itemTotalRuleChecker->isEligible($subject, [])->shouldNotBeCalled();
     $this->isEligible($subject, $promotion)->shouldReturn(false);
 }
 function it_does_not_check_more_rules_if_one_has_returned_false(ServiceRegistryInterface $rulesRegistry, RuleCheckerInterface $firstRuleChecker, RuleCheckerInterface $secondRuleChecker, RuleInterface $firstRule, RuleInterface $secondRule, PromotionInterface $promotion, PromotionSubjectInterface $subject)
 {
     $promotion->hasRules()->willReturn(true);
     $promotion->getRules()->willReturn([$firstRule, $secondRule]);
     $firstRule->getType()->willReturn('first_rule');
     $firstRule->getConfiguration()->willReturn([]);
     $secondRule->getType()->willReturn('second_rule');
     $secondRule->getConfiguration()->willReturn([]);
     $rulesRegistry->get('first_rule')->willReturn($firstRuleChecker);
     $rulesRegistry->get('second_rule')->willReturn($secondRuleChecker);
     $firstRuleChecker->isEligible($subject, [])->willReturn(false);
     $secondRuleChecker->isEligible($subject, [])->shouldNotBeCalled();
     $this->isEligible($subject, $promotion)->shouldReturn(false);
 }
 /**
  * Checks is a promotion is eligible to a subject for a given rule.
  *
  * @param PromotionSubjectInterface $subject
  * @param PromotionInterface        $promotion
  * @param RuleInterface             $rule
  *
  * @return Boolean
  */
 protected function isEligibleToRule(PromotionSubjectInterface $subject, PromotionInterface $promotion, RuleInterface $rule)
 {
     $checker = $this->registry->get($rule->getType());
     if ($checker->isEligible($subject, $rule->getConfiguration())) {
         return true;
     }
     if (!$promotion->isCouponBased()) {
         return false;
     }
     if ($subject instanceof PromotionCouponAwareSubjectInterface) {
         if (null !== ($coupon = $subject->getPromotionCoupon() && $promotion === $coupon->getPromotion())) {
             $this->dispatcher->dispatch(SyliusPromotionEvents::COUPON_NOT_ELIGIBLE, new GenericEvent($promotion));
         }
     }
     if ($subject instanceof PromotionCouponsAwareSubjectInterface) {
         foreach ($subject->getPromotionCoupons() as $coupon) {
             if ($promotion === $coupon->getPromotion()) {
                 $this->dispatcher->dispatch(SyliusPromotionEvents::COUPON_NOT_ELIGIBLE, new GenericEvent($promotion));
             }
         }
     }
     return false;
 }
 function it_recognizes_subject_as_not_eligible_if_promotion_subject_is_not_coupon_aware($registry, RuleCheckerInterface $checker, PromotionSubjectInterface $subject, PromotionInterface $promotion, CouponInterface $coupon, RuleInterface $rule)
 {
     $promotion->getStartsAt()->willReturn(null);
     $promotion->getEndsAt()->willReturn(null);
     $promotion->getUsageLimit()->willReturn(null);
     $promotion->hasRules()->willReturn(true);
     $promotion->isCouponBased()->willReturn(true);
     $coupon->getPromotion()->willReturn($promotion);
     $registry->get(RuleInterface::TYPE_ITEM_TOTAL)->willReturn($checker);
     $promotion->getRules()->willReturn(array($rule));
     $rule->getType()->willReturn(RuleInterface::TYPE_ITEM_TOTAL);
     $rule->getConfiguration()->willReturn(array());
     $registry->get(RuleInterface::TYPE_ITEM_TOTAL)->willReturn($checker);
     $checker->isEligible($subject, array())->willReturn(false);
     $this->isEligible($subject, $promotion)->shouldReturn(false);
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 public function removeRule(RuleInterface $rule)
 {
     $rule->setPromotion(null);
     $this->rules->removeElement($rule);
     return $this;
 }
 /**
  * @param PromotionSubjectInterface $subject
  * @param RuleInterface $rule
  *
  * @return bool
  */
 protected function isEligibleToRule(PromotionSubjectInterface $subject, RuleInterface $rule)
 {
     /** @var RuleCheckerInterface $checker */
     $checker = $this->ruleRegistry->get($rule->getType());
     return $checker->isEligible($subject, $rule->getConfiguration());
 }