/** * Resolves a cart given a customer cart and a session cart. * * @param CustomerInterface $customer Customer * @param CartInterface|null $cartFromCustomer Customer Cart * @param CartInterface|null $cartFromSession Cart loaded from session * * @return CartInterface Cart resolved */ private function resolveCarts(CustomerInterface $customer, CartInterface $cartFromCustomer = null, CartInterface $cartFromSession = null) { if ($cartFromCustomer) { return $cartFromCustomer; } else { if (!$cartFromSession) { /** * Customer has no pending carts, and there is no cart in * session. * * We create a new Cart */ $cart = $this->cartFactory->create(); } else { /** * Customer has no pending carts, and there is a cart loaded * in session. * * If customer is not a pristine entity since it has already * been flushed, we associate this cart with customer */ $cart = $cartFromSession; if ($customer->getId()) { $cart->setCustomer($customer); $customer->addCart($cart); } } return $cart; } }
/** * Resolve carts given customer cart and session cart * * @param CustomerInterface $customer Customer * @param CartInterface $cartFromCustomer Customer Cart * @param CartInterface $cartFromSession Cart loaded from session * * @return CartInterface Cart resolved */ protected function resolveCarts(CustomerInterface $customer, CartInterface $cartFromCustomer = null, CartInterface $cartFromSession = null) { if ($cartFromCustomer) { return $cartFromCustomer; } else { if (!$cartFromSession) { /** * Customer has any cart not ordered, and there is no cart in * session. * * We create a new Cart */ $cart = $this->cartFactory->create(); } else { /** * Customer has any cart not ordered, and there is a cart loaded * in session. * * If customer exists as a persisted entity, we save this cart * as Customer cart */ $cart = $cartFromSession; if ($customer->getId()) { $cart->setCustomer($customer); $customer->addCart($cart); } } return $cart; } }
/** * 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; }