Example #1
0
 /**
  * Given a ReferralRule, disable all other entities and enabled it.
  * Second parameter lets you disable the entity
  *
  * Changed content is flushed
  *
  * @param ReferralRuleInterface $referralRule Referral Rule
  * @param boolean               $enable       Enable
  *
  * @return $this self Object
  */
 public function enableReferralRule(ReferralRuleInterface $referralRule, $enable = true)
 {
     $referralRule->setEnabled($enable);
     $referralRules = $this->referralRuleRepository->findAll();
     $referralRules->removeElement($referralRule);
     $referralRules->map(function (ReferralRuleInterface $referralRule) {
         $referralRule->setEnabled(false);
     });
     $this->manager->flush();
     return $this;
 }
 /**
  * Resolve a customer in referral program engine.
  *
  * If given hash is empty or not exists, if ReferralRule
  * $autoReferralAssignment field is set to false, return null. If is set to
  * true and there is one, and only one ReferralLine with desired Customer
  * email, this Line will be taken.
  *
  * Otherwise, system tries to find referral line related to invitation.
  * If exists, return related ReferralLine
  * If not, new Direct ReferralLine is created
  *
  * In both cases, current customer is set as Invited element in line.
  *
  * This method do not flush changes.
  *
  * @param CustomerInterface $invited Customer
  * @param string            $hash    Hash
  *
  * @return null|ReferralLine Related Referral Line
  */
 public function resolve(CustomerInterface $invited, $hash)
 {
     /**
      * Possibilities to get a referralLine
      *
      * * Hash belongs to an active ReferralHash containing given email
      * * Hash belongs to an active ReferralHash, but email is not inserted
      * * Hash do not belongs to any active ReferralHash or is empty, but
      *   given email is contained into one, and only one ReferralLine, and
      *   $autoReferralAssignment value is true
      * * Hash do not belongs to any active ReferralHash or is empty, but
      *   given email is contained into one active referral line
      *
      * @var $referralHash ReferralHash
      */
     $invitedEmail = $invited->getEmail();
     $referralHash = $this->referralHashManager->getReferralHashByHash($hash);
     if (!$referralHash instanceof ReferralHashInterface) {
         $referralLines = new ArrayCollection();
         $referralLine = $this->referralLineRepository->findOneBy(array('enabled' => true, 'closed' => false, 'invitedEmail' => $invitedEmail));
         if (!$referralLine instanceof ReferralLineInterface) {
             /**
              * Only tries to retrieve current Line if
              * $autoReferralAssignment is true
              */
             if (!$this->autoReferralAssignment) {
                 return null;
             }
             $referralLines = $this->referralLineRepository->findBy(array('closed' => false, 'invitedEmail' => $invitedEmail));
             /**
              * No result is found or more than one. It means than this user
              * is not invited and the hash is trying to use is not valid.
              */
             if (!$referralLines instanceof ArrayCollection || $referralLines->count() > 1) {
                 return null;
             }
             $referralLine = $referralLines->first();
         }
     } else {
         /**
          * @var ArrayCollection $referralLines
          */
         $referralLines = $this->referralLineRepository->findByInvitedEmail($invitedEmail);
         /**
          * @var ReferralLine $referralLine
          */
         $referralLine = $this->referralLineRepository->findOneByReferralHashAndInvitedEmail($referralHash, $invited->getEmail());
     }
     /**
      * @var ArrayCollection $otherReferralLines
      */
     $manager = $this->manager;
     $purgeDisabledLines = $this->purgeDisabledLines;
     if (!$referralLines->isEmpty()) {
         $referralLines->removeElement($referralLine);
         $referralLines->map(function (ReferralLineInterface $otherReferralLine) use($purgeDisabledLines, $manager) {
             if ($purgeDisabledLines) {
                 $manager->remove($otherReferralLine);
             } else {
                 $otherReferralLine->setEnabled(false);
             }
         });
     }
     /**
      * ReferralLine is not created, so we create new one with type direct
      */
     if (!$referralLine instanceof ReferralLineInterface) {
         $referralRule = $this->referralRuleRepository->findEnabledReferralRule();
         if (!$referralRule instanceof ReferralRuleInterface) {
             return null;
         }
         $referralLine = $this->referralLineFactory->create();
         $referralLine->setReferralHash($referralHash)->setInvitedEmail($invited->getEmail())->setInvitedName($invited->getFullName())->setSource(ElcodiReferralProgramSources::DIRECT)->setReferralRule($referralRule)->setReferrerType($referralRule->getReferrerType())->setReferrerCoupon($referralRule->getReferrerCoupon())->setInvitedType($referralRule->getInvitedType())->setInvitedCoupon($referralRule->getInvitedCoupon());
         $this->manager->persist($referralLine);
     }
     $referralLine->setInvited($invited)->setEnabled(true);
     $this->manager->flush();
     return $referralLine;
 }