/** * Marks the card as consumed. * * @param string $code * * @throws NonExistentCodeInternalException */ public function punch($code) { $result = $this->connectToStorageInternalWorker->connect()->update(array('code' => $code), array('$set' => array('consumed' => true))); if ($result['n'] == 0) { throw new NonExistentCodeInternalException($code); } }
/** * Picks the card with given code. * * @param string $code * * @return array An array with the following keys: * code, category and consumed. * * @throws NonExistentCodeInternalException */ public function pick($code) { $card = $this->connectToStorageInternalWorker->connect()->findOne(['code' => $code], ['_id' => 0]); if (!$card) { throw new NonExistentCodeInternalException($code); } return $card; }
/** * Generates a code, verifying that no card use it. * * @return string */ private function generateCode() { $code = $this->codeGenerator->generate(); while ($this->connectToStorageInternalWorker->connect()->findOne(array('code' => $code))) { $code = $this->codeGenerator->generate(); } return $code; }
/** * Creates a card. * * @param string $code * @param string $category * @param string $consumed */ public function create($code, $category, $consumed) { $this->connectToStorageInternalWorker->connect()->insert(array('code' => $code, 'category' => $category, 'consumed' => $consumed)); }
/** * Collects cards. */ public function collect() { return $this->connectToStorageInternalWorker->connect()->find()->fields(['_id' => 0]); }