/**
  * Check if any of coupons applied in an order are from active referral
  * program line, being the customer, the referrer.
  *
  * As referrer, you can have lot of ReferralLine enabled and not closed, so
  * this method search for all of them
  *
  * @param CustomerInterface $customer Customer
  * @param Collection        $coupons  Coupons
  *
  * @return $this self Object
  */
 protected function checkCouponsUsedAsReferrer(CustomerInterface $customer, Collection $coupons)
 {
     $referralLines = $this->referralHashManager->getReferralHashByCustomer($customer)->getReferralLines();
     foreach ($coupons as $coupon) {
         $this->checkCouponInReferralLineCollection($referralLines, $coupon);
     }
     return $this;
 }
 /**
  * Adds a user into de referral program table, given the Referrer Customer,
  * the invited email and the invited Name
  *
  * If current email is currently inserted in the table with enabled flag and
  * purgeInvalidLines is also set as true, Line will not be inserted.
  *
  * @param ReferralRuleInterface $referralRule ReferralRule
  * @param CustomerInterface     $referrer     Referrer
  * @param InvitationInterface   $invitation   Invitation
  *
  * @throws ReferralProgramEmailIsUserException
  * @throws ReferralProgramLineExistsException
  *
  * @return $this self Object
  */
 protected function inviteLine(ReferralRuleInterface $referralRule, CustomerInterface $referrer, InvitationInterface $invitation)
 {
     $referralHash = $this->referralHashManager->getReferralHashByCustomer($referrer);
     /**
      * If purgeInvalidLines is enabled, we try to find if any ReferralLine
      * is enabled. If it is, we skip this email.
      */
     if ($this->purgeDisabledLines) {
         $enabledReferralLine = $this->referralLineRepository->findOneBy(array('enabled' => true, 'invitedEmail' => $invitation->getEmail()));
         if ($enabledReferralLine instanceof ReferralLineInterface) {
             return $this;
         }
     }
     /**
      * @var ReferralLine $referralLine
      */
     $referralLine = $this->referralLineRepository->findOneBy(array('invitedEmail' => $invitation->getEmail(), 'referralHash' => $referralHash));
     if ($referralLine instanceof ReferralLine) {
         throw new ReferralProgramLineExistsException();
     }
     /** @var $customer Customer */
     $customer = $this->customerRepository->findOneByEmail($invitation->getEmail());
     if ($customer instanceof Customer) {
         throw new ReferralProgramEmailIsUserException();
     }
     /**
      * New referral line
      */
     $referralLine = $this->referralLineFactory->create();
     $referralLine->setReferralHash($referralHash)->setInvitedEmail($invitation->getEmail())->setInvitedName($invitation->getName())->setSource($invitation->getSource())->setReferralRule($referralRule)->setReferrerType($referralRule->getReferrerType())->setReferrerCoupon($referralRule->getReferrerCoupon())->setInvitedType($referralRule->getInvitedType())->setInvitedCoupon($referralRule->getInvitedCoupon());
     /**
      * Persists and flushes new entity
      */
     $this->manager->persist($referralLine);
     $this->manager->flush($referralLine);
     $referralLink = $this->referralRouteManager->generateControllerRoute($referralHash);
     /**
      * Invitation done event is raised
      */
     $event = new ReferralProgramInvitationEvent($referralLine, $referralLink);
     $this->eventDispatcher->dispatch(ElcodiReferralProgramEvents::REFERRAL_PROGRAM_INVITATION, $event);
     return $this;
 }