Example #1
0
 /**
  * Update rewards properties - availability, distributed,...
  *
  * @param $data
  *
  * @return \Crowdfunding\Reward|null
  */
 protected function updateReward($data)
 {
     // Get reward.
     $keys = array("id" => ArrayHelper::getValue($data, "reward_id"), "project_id" => ArrayHelper::getValue($data, "project_id"));
     $reward = new Crowdfunding\Reward(\JFactory::getDbo());
     $reward->load($keys);
     // DEBUG DATA
     JDEBUG ? $this->log->add(\JText::_($this->textPrefix . "_DEBUG_REWARD_OBJECT"), $this->debugType, $reward->getProperties()) : null;
     // Check for valid reward.
     if (!$reward->getId()) {
         // Log data in the database
         $this->log->add(\JText::_($this->textPrefix . "_ERROR_INVALID_REWARD"), $this->debugType, array("data" => $data, "reward object" => $reward->getProperties()));
         return null;
     }
     // Check for valida amount between reward value and payed by user
     $txnAmount = ArrayHelper::getValue($data, "txn_amount");
     if ($txnAmount < $reward->getAmount()) {
         // Log data in the database
         $this->log->add(\JText::_($this->textPrefix . "_ERROR_INVALID_REWARD_AMOUNT"), $this->debugType, array("data" => $data, "reward object" => $reward->getProperties()));
         return null;
     }
     // Verify the availability of rewards
     if ($reward->isLimited() and !$reward->getAvailable()) {
         // Log data in the database
         $this->log->add(\JText::_($this->textPrefix . "_ERROR_REWARD_NOT_AVAILABLE"), $this->debugType, array("data" => $data, "reward object" => $reward->getProperties()));
         return null;
     }
     // Increase the number of distributed rewards
     // if there is a limit.
     if ($reward->isLimited()) {
         $reward->increaseDistributed();
         $reward->updateDistributed();
     }
     return $reward;
 }
 /**
  * Increase the number of distributed to a user rewards.
  *
  * @param Transaction $transaction
  * @param Reward|null $reward
  *
  * @throws  \RuntimeException
  * @throws  \InvalidArgumentException
  * @throws  \UnexpectedValueException
  *
  * @return void
  */
 protected function increaseDistributedReward(Transaction $transaction, $reward)
 {
     // Check for valid reward.
     if ($reward === null or !$reward->getId()) {
         return;
     }
     // Check for valida amount between reward value and payed by user
     $txnAmount = $transaction->getAmount();
     if ($txnAmount < $reward->getAmount()) {
         return;
     }
     // Check for available rewards.
     if ($reward->isLimited() and !$reward->hasAvailable()) {
         return;
     }
     // Increase the number of distributed rewards.
     $reward->increaseDistributed();
     $reward->updateDistributed();
 }