Example #1
0
 /**
  * Sets to the profile with given uniqueness, the given amount of
  * sms to the balance.
  *
  * @param string $uniqueness
  * @param int    $amount
  *
  * @throws NonExistentUniquenessInternalException
  */
 public function set($uniqueness, $amount)
 {
     $result = $this->connectToStorageInternalWorker->connect()->update(array('uniqueness' => $uniqueness), array('$set' => array('balance' => $amount)));
     if ($result['n'] == 0) {
         throw new NonExistentUniquenessInternalException($uniqueness);
     }
 }
 /**
  * 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);
     }
 }
Example #3
0
 /**
  * Picks the profile with given uniqueness.
  *
  * @param string $uniqueness
  *
  * @return array An array with the following keys:
  *               balance
  *
  * @throws NonExistentUniquenessApiException
  */
 public function pick($uniqueness)
 {
     $profile = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness], ['_id' => 0]);
     if (!$profile) {
         throw new NonExistentUniquenessApiException();
     }
     return $profile;
 }
 /**
  * Creates an profile.
  *
  * @param string   $uniqueness
  * @param int|null $balance
  *
  * @return string The id
  *
  * @throws ExistentUniquenessSharedException
  * @throws \MongoCursorException
  */
 public function create($uniqueness, $balance = 0)
 {
     try {
         $this->connectToStorageInternalWorker->connect()->insert(['uniqueness' => $uniqueness, 'balance' => $balance]);
     } catch (\MongoCursorException $e) {
         if (11000 == $e->getCode()) {
             throw new ExistentUniquenessSharedException();
         }
         throw $e;
     }
 }
 /**
  * Decreases to the profile with given uniqueness, the given amount of
  * messages.
  *
  * @param string $uniqueness
  * @param int    $amount
  *
  * @throws NonExistentUniquenessInternalException
  * @throws InsufficientBalanceInternalException
  */
 public function decrease($uniqueness, $amount)
 {
     $item = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness]);
     if (!$item) {
         throw new NonExistentUniquenessInternalException($uniqueness);
     }
     if ($amount > $item['balance']) {
         throw new InsufficientBalanceInternalException();
     }
     $this->connectToStorageInternalWorker->connect()->update(['uniqueness' => $uniqueness], ['$inc' => ['balance' => -1 * $amount]]);
 }
 /**
  * Collects profiles.
  *
  * @return \Iterator
  */
 public function collect()
 {
     return $this->connectToStorageInternalWorker->connect()->find()->fields(['_id' => 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;
 }