/** * 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); } }
/** * Increases the debt 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 NonExistentUniquenessInternalException */ public function increase($uniqueness, $amount, $description) { $result = $this->connectToStorageInternalWorker->connect()->update(['uniqueness' => $uniqueness], ['$inc' => ['debt' => $amount]]); if ($result['n'] == 0) { throw new NonExistentUniquenessInternalException($uniqueness); } $this->logOperationInternalWorker->log($uniqueness, $amount, LogOperationInternalWorker::IMPACT_POSITIVE, $description); }
/** * @param string $uniqueness * @param string[] $cards * * @throws NonExistentUniquenessInternalException */ public function assign($uniqueness, $cards) { $profile = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness]); if (!$profile) { throw new NonExistentUniquenessInternalException($uniqueness); } foreach ($cards as $i => $card) { $this->connectToStorageInternalWorker->connect()->update(['uniqueness' => $uniqueness], ['$set' => ['cards' => array_merge($profile['cards'], $cards)]]); } }
/** * Picks the profile with given uniqueness. * * @param string $uniqueness * * @return array An array with the following keys: * uniqueness, debt and cards. * * @throws NonExistentUniquenessApiException */ public function pick($uniqueness) { $profile = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness], ['_id' => 0]); if (!$profile) { throw new NonExistentUniquenessApiException(); } foreach ($profile['cards'] as $i => $card) { $profile['cards'][$i] = $this->pickCardInternalWorker->pick($card); } return $profile; }
/** * Creates a profile. * * @param string $uniqueness * @param int $debt * @param string[] $cards * * @throws ExistentUniquenessSharedException * @throws \MongoCursorException */ public function create($uniqueness, $debt, $cards) { try { $this->connectToStorageInternalWorker->connect()->insert(['uniqueness' => $uniqueness, 'debt' => $debt, 'cards' => $cards]); } catch (\MongoCursorException $e) { if (11000 == $e->getCode()) { throw new ExistentUniquenessSharedException(); } throw $e; } }
/** * Decreases the debt to the profile with given uniqueness, the given amount. * * @param string $uniqueness * @param int $amount * @param string $description * * @throws NonExistentUniquenessInternalException * @throws LowerDebtInternalException */ public function decrease($uniqueness, $amount, $description) { $item = $this->connectToStorageInternalWorker->connect()->findOne(['uniqueness' => $uniqueness], ['debt']); if (!$item) { throw new NonExistentUniquenessInternalException($uniqueness); } if ($amount > $item['debt']) { throw new LowerDebtInternalException(); } $this->connectToStorageInternalWorker->connect()->update(['uniqueness' => $uniqueness], ['$inc' => ['debt' => -1 * $amount]]); $this->logOperationInternalWorker->log($uniqueness, $amount, LogOperationInternalWorker::IMPACT_NEGATIVE, $description); }
/** * Collects profiles. * * @return \Iterator */ public function collect() { return $this->connectToStorageInternalWorker->connect()->find()->fields(['_id' => 0]); }
/** * Increases the debt 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' => ['debt' => $amount]]); }