예제 #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;
 }
예제 #2
0
 /**
  * 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]);
 }
예제 #3
0
 /**
  * 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);
 }
예제 #4
0
 /**
  * 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;
     }
 }
예제 #5
0
 /**
  * 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);
 }
예제 #6
0
 /**
  * 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]]);
 }
예제 #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;
 }
예제 #8
0
 /**
  * Collects profiles.
  *
  * @return \Iterator
  */
 public function collect()
 {
     return $this->connectToStorageInternalWorker->connect()->find()->fields(['_id' => 0]);
 }