Пример #1
0
 /**
  * Invited coupon assignment logic.
  *
  * This method try to assign a new `use and drop` coupon, if needed.
  * If a coupon is set into de ReferralLine, new one is created getting
  * same Coupon definition as original, but changing some parameters
  *
  * * Can be used only once
  * * Name is regenerated ( See CouponManager )
  * * Both specific invited and generic event are raised.
  *
  * @param ReferralLineInterface $referralLine Referral Line
  * @param string                $type         Type of rule
  *
  * @return $this self Object
  */
 protected function checkInvitedCouponAssignment(ReferralLineInterface $referralLine, $type)
 {
     if ($referralLine->getInvitedType() != $type) {
         return $this;
     }
     /**
      * @var $couponInvited Coupon
      */
     $couponInvited = $referralLine->getInvitedCoupon();
     /**
      * New coupon MUST be assigned to invited.
      */
     if ($couponInvited instanceof CouponInterface) {
         $newCoupon = $this->couponManager->duplicateCoupon($couponInvited);
         /**
          * As this coupon can be used once, we change number of uses.
          *
          * This variable could be defined in bundle configuration
          */
         $newCoupon->setCount(1)->setEnabled(true);
         $referralLine->setInvitedAssignedCoupon($newCoupon);
         $this->manager->persist($newCoupon);
         $this->manager->flush($newCoupon);
         $this->manager->flush($referralLine);
         /**
          * Coupon assigned to invited event is raised
          */
         $event = new ReferralProgramCouponAssignedEvent($referralLine, $newCoupon);
         $this->eventDispatcher->dispatch(ElcodiReferralProgramEvents::REFERRAL_PROGRAM_COUPON_ASSIGNED_TO_INVITED, $event);
         $this->raiseCommonCouponAssignedEvent($referralLine, $newCoupon);
     }
     return $this;
 }