/** * Save the referral to the database. * * @param ReferralInterface $referral * * @return ReferralInterface */ public function save(ReferralInterface $referral) { $result = $this->_query->run(' INSERT INTO refer_a_friend_referral ( reward_config_id, status, referrer_id, referred_email, referred_name, created_at, created_by, updated_at, updated_by ) VALUES ( :rewardConfigID?i, :status?s, :referrerID?i, :referredEmail?s, :referredName?s, :createdAt?d, :createdBy?in, :createdAt?d, :createdBy?in ) ', ['rewardConfigID' => $referral->getRewardConfig()->getID(), 'status' => $referral->getStatus(), 'referrerID' => $referral->getReferrer()->id, 'referredEmail' => $referral->getReferredEmail(), 'referredName' => $referral->getReferredName(), 'createdAt' => $referral->getCreatedAt() ?: new \DateTime(), 'createdBy' => $this->_currentUser->id]); $referral->setID($result->id()); return $referral; }
private function _isValid(Referral\ReferralInterface $referral) { foreach ($referral->getRewardConfig()->getConstraints() as $constraint) { if (false === $constraint->isValid($referral)) { return false; } } return true; }
/** * Create a discount object with a unique code from a referral. * If there is no valid discount type (i.e. percentage or set amount), it will throw an exception * * @param ReferralInterface $referral * @throws Exception\DiscountBuildException * * @return Discount */ public function build(ReferralInterface $referral) { $discount = new Discount(); $discount->code = $this->_getCode(); $discount->emails = [$referral->getReferrer()->email]; $rewardOptions = $referral->getRewardConfig()->getRewardOptions(); $type = $rewardOptions->get('discount_reward_discount_type')->getValue(); switch ($type) { case DiscountType::SET_AMOUNT: $discount->discountAmounts = []; foreach ($rewardOptions as $rewardOption) { if ($rewardOption instanceof Config\RewardOption\SetAmount) { $discount->discountAmounts[$rewardOption->getCurrency()] = $rewardOption->getValue(); } } break; case DiscountType::PERCENTAGE: $discount->percentage = $rewardOptions->get('discount_reward_percentage_value')->getValue(); break; default: throw new Exception\DiscountBuildException('No valid discount type set on discount referral'); } return $discount; }