Ejemplo n.º 1
0
 /**
  * Check if any of coupons applied in an order are from active referral
  * program line, being the customer, the invited.
  *
  * As invited, you can only have one ReferralLine enabled and not closed, so
  * this method search for this one
  *
  * @param CustomerInterface $customer Customer
  * @param Collection        $coupons  Coupons
  *
  * @return $this self Object
  */
 protected function checkCouponsUsedAsInvited(CustomerInterface $customer, Collection $coupons)
 {
     $referralLine = $this->referralLineRepository->findOneByInvited($customer);
     if (!$referralLine instanceof ReferralLineInterface) {
         return $this;
     }
     foreach ($coupons as $coupon) {
         /**
          * @var ReferralLineInterface $referralLine
          */
         if ($referralLine->getInvitedAssignedCoupon() == $coupon) {
             $referralLine->setInvitedCouponUsed(true);
             $this->manager->flush($referralLine);
         }
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }