/**
  * Check if this coupon is already applied to the cart
  *
  * @param CartCouponOnApplyEvent $event Event
  *
  * @throws CouponAlreadyAppliedException
  */
 public function checkDuplicates(CartCouponOnApplyEvent $event)
 {
     $cartCoupon = $this->cartCouponRepository->findOneBy(['cart' => $event->getCart(), 'coupon' => $event->getCoupon()]);
     if (null !== $cartCoupon) {
         throw new CouponAlreadyAppliedException();
     }
 }
 /**
  * Applies Coupon in Cart, and flushes it
  *
  * @param CartCouponOnApplyEvent $event Event
  *
  * @throws CouponRulesNotValidateException Invalid rules
  */
 public function onCartCouponApply(CartCouponOnApplyEvent $event)
 {
     $cart = $event->getCart();
     $coupon = $event->getCoupon();
     if (!$this->cartCouponRuleManager->checkCouponValidity($cart, $coupon)) {
         $this->cartCouponEventDispatcher->dispatchCartCouponOnRejectedEvent($cart, $coupon);
         $event->stopPropagation();
     }
 }
 /**
  * Applies Coupon in Cart, and flushes it
  *
  * @param CartCouponOnApplyEvent $event Event
  */
 public function onCartCouponApply(CartCouponOnApplyEvent $event)
 {
     $cart = $event->getCart();
     $coupon = $event->getCoupon();
     /**
      * We create a new instance of CartCoupon.
      * We also persist and flush relation
      */
     $cartCoupon = $this->cartCouponFactory->create()->setCart($cart)->setCoupon($coupon);
     $this->cartCouponObjectManager->persist($cartCoupon);
     $this->cartCouponObjectManager->flush($cartCoupon);
     $event->setCartCoupon($cartCoupon);
 }
 /**
  * Avoid applying a manual coupon if another is being applied
  *
  * @param CartCouponOnApplyEvent $event
  *
  * @throws CouponIncompatibleException
  */
 public function assertJustOneManualCoupon(CartCouponOnApplyEvent $event)
 {
     if (!$this->isManual($event->getCoupon())) {
         return null;
     }
     /**
      * @var CartCouponInterface[] $cartCoupons
      */
     $cartCoupons = $this->cartCouponRepository->findBy(['cart' => $event->getCart()]);
     foreach ($cartCoupons as $cartCoupon) {
         if ($this->isManual($cartCoupon->getCoupon())) {
             throw new CouponIncompatibleException();
         }
     }
 }
 /**
  * Check if this coupon can be applied when other coupons had previously
  * been applied
  *
  * @param CartCouponOnApplyEvent $event Event
  *
  * @return null
  *
  * @throws CouponNotStackableException
  */
 public function checkStackableCoupon(CartCouponOnApplyEvent $event)
 {
     $cartCoupons = $this->cartCouponRepository->findBy(['cart' => $event->getCart()]);
     /**
      * If there are no previously applied coupons we can skip the check
      */
     if (0 == count($cartCoupons)) {
         return null;
     }
     $coupon = $event->getCoupon();
     $appliedCouponsCanBeStacked = array_reduce($cartCoupons, function ($previousCouponsAreStackable, CartCouponInterface $cartCoupon) {
         return $previousCouponsAreStackable && $cartCoupon->getCoupon()->getStackable();
     }, true);
     /**
      * Checked coupon can be stackable and everithing that was
      * previously applied is also stackable
      */
     if ($coupon->getStackable() && $appliedCouponsCanBeStacked) {
         return null;
     }
     throw new CouponNotStackableException();
 }
 /**
  * Creates a new CartCoupon instance.
  *
  * @param CartCouponOnApplyEvent $event Event
  */
 public function createAndSaveCartCoupon(CartCouponOnApplyEvent $event)
 {
     $cartCoupon = $this->cartCouponManager->createAndSaveCartCoupon($event->getCart(), $event->getCoupon());
     $event->setCartCoupon($cartCoupon);
 }
 /**
  * Checks if a Coupon is applicable to a Cart
  *
  * @param CartCouponOnApplyEvent $event Event
  */
 public function validateCoupon(CartCouponOnApplyEvent $event)
 {
     $this->cartCouponDispatcher->dispatchCartCouponOnCheckEvent($event->getCart(), $event->getCoupon());
 }
 /**
  * Check if this coupon is already applied to the cart
  *
  * @param CartCouponOnApplyEvent $event Event
  *
  * @throws CouponAlreadyAppliedException Coupon already applied
  */
 public function validateDuplicatedCoupon(CartCouponOnApplyEvent $event)
 {
     $this->duplicatedCouponValidator->validateDuplicatedCoupon($event->getCart(), $event->getCoupon());
 }
 /**
  * Check if this coupon can be applied when other coupons had previously
  * been applied.
  *
  * @param CartCouponOnApplyEvent $event Event
  */
 public function validateStackableCoupon(CartCouponOnApplyEvent $event)
 {
     $this->stackableCouponValidator->validateStackableCoupon($event->getCart(), $event->getCoupon());
 }
 /**
  * Check if this coupon can be applied when other coupons had previously
  * been applied
  *
  * @param CartCouponOnApplyEvent $event Event
  *
  * @return null
  *
  * @throws CouponNotStackableException
  */
 public function checkStackableCoupon(CartCouponOnApplyEvent $event)
 {
     $this->stackableCouponChecker->checkStackableCoupon($event->getCart(), $event->getCoupon());
 }