Example #1
0
 /**
  * Picks the profile with given uniqueness.
  *
  * @param string $uniqueness
  *
  * @return array An array with the following keys:
  *               uniqueness, balance
  *
  * @throws NonExistentUniquenessApiException
  */
 public function pick($uniqueness)
 {
     $profile = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness], ['_id' => 0]);
     if (!$profile) {
         throw new NonExistentUniquenessApiException();
     }
     return $profile;
 }
 /**
  * Deletes the profile with given uniqueness.
  *
  * @param string $uniqueness
  *
  * @throws NonExistentUniquenessSharedException
  */
 public function delete($uniqueness)
 {
     $result = $this->connectToStorageInternalWorker->connect()->remove(['uniqueness' => $uniqueness]);
     if ($result['n'] == 0) {
         throw new NonExistentUniquenessSharedException($uniqueness);
     }
     $this->connectToBalanceOperationStorageInternalWorker->connect()->remove(['uniqueness' => $uniqueness]);
 }
 /**
  * Increases the balance to the profile with given uniqueness, the given
  * amount. It also logs the operation using given description.
  *
  * @param string $uniqueness
  * @param int    $amount
  * @param string $description
  *
  * @throws NonExistentUniquenessSharedException
  */
 public function increase($uniqueness, $amount, $description)
 {
     $result = $this->connectToStorageInternalWorker->connect()->update(['uniqueness' => $uniqueness], ['$inc' => ['balance' => $amount]]);
     if ($result['n'] == 0) {
         throw new NonExistentUniquenessSharedException($uniqueness);
     }
     $this->logOperationInternalWorker->log($uniqueness, $amount, LogOperationInternalWorker::IMPACT_POSITIVE, $description);
 }
 /**
  * Creates a profile.
  *
  * @param string $uniqueness
  * @param int    $balance
  *
  * @throws ExistentUniquenessSharedException
  * @throws \MongoCursorException
  */
 public function create($uniqueness, $balance)
 {
     try {
         $this->connectToStorageInternalWorker->connect()->insert(['uniqueness' => $uniqueness, 'balance' => $balance]);
     } catch (\MongoCursorException $e) {
         if (11000 == $e->getCode()) {
             throw new ExistentUniquenessSharedException();
         }
         throw $e;
     }
 }
 /**
  * Decreases the balance to the profile with given uniqueness, the given
  * amount. It also logs the operation using given description.
  *
  * @param string $uniqueness
  * @param int    $amount
  * @param string $description
  *
  * @throws NonExistentUniquenessSharedException
  * @throws InsufficientBalanceSharedException
  */
 public function decrease($uniqueness, $amount, $description)
 {
     $item = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness], ['balance']);
     if (!$item) {
         throw new NonExistentUniquenessSharedException($uniqueness);
     }
     if ($amount > $item['balance']) {
         throw new InsufficientBalanceSharedException();
     }
     $this->connectToStorageInternalWorker->connect()->update(['uniqueness' => $uniqueness], ['$inc' => ['balance' => -1 * $amount]]);
     $this->logOperationInternalWorker->log($uniqueness, $amount, LogOperationInternalWorker::IMPACT_NEGATIVE, $description);
 }
 /**
  * Increases the balance to the profile with given uniqueness, the given
  * amount.
  *
  * @param string $uniqueness
  * @param int    $amount
  */
 public function increase($uniqueness, $amount)
 {
     $this->connectToStorageInternalWorker->connect()->update(['uniqueness' => $uniqueness], ['$inc' => ['balance' => $amount]]);
 }
Example #7
0
 /**
  * Checks if the profile with given uniqueness has given amount.
  *
  * @param string $uniqueness
  * @param int    $amount
  *
  * @return bool True if balance is greater or equal than given amount, false
  *              otherwise.
  */
 public function check($uniqueness, $amount)
 {
     $item = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness], ['balance']);
     return $item['balance'] >= $amount;
 }
 /**
  * Collects profiles.
  *
  * @return \Iterator
  */
 public function collect()
 {
     return $this->connectToStorageInternalWorker->connect()->find()->fields(['_id' => 0]);
 }