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;
 }
 /**
  * Prepare reward object and inject it in the container.
  *
  * <code>
  * $rewardId = 1;
  * $projectId = 2;
  *
  * $this->prepareReward($container, $rewardId, $projectId);
  * </code>
  *
  * @param Container $container
  * @param int $rewardId
  * @param int $projectId
  *
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @throws \UnexpectedValueException
  * @throws \OutOfBoundsException
  *
  * @return Reward
  */
 protected function prepareReward($container, $rewardId, $projectId)
 {
     $rewardId = (int) abs($rewardId);
     $hash = StringHelper::generateMd5Hash(Constants::CONTAINER_REWARD, array($rewardId, $projectId));
     if (!$container->exists($hash) and $rewardId > 0) {
         $reward = new Reward(\JFactory::getDbo());
         $reward->load(array('id' => $rewardId, 'project_id' => $projectId));
         if (!$reward->getId()) {
             $reward = null;
         }
         $container->set($hash, $reward);
     }
 }
 /**
  * Decrease 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 decreaseDistributedReward(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;
     }
     // Decrease the number of distributed rewards.
     $reward->decreaseDistributed();
     $reward->updateDistributed();
 }