/**
  * Generates a code, verifying that no card use it.
  *
  * @return string
  */
 private function generateCode()
 {
     $code = $this->codeGenerator->generate('xx-xx-xx');
     while ($this->connectToStorageInternalWorker->connect()->findOne(['code' => $code])) {
         $code = $this->codeGenerator->generate();
     }
     return $code;
 }
 /**
  * Picks the card with given code.
  *
  * @param string $code
  *
  * @return array An array with the following keys:
  *               code, role 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;
 }
 /**
  * Consumes card with given code.
  *
  * @param string $code
  *
  * @throws NonExistentCodeSharedException
  * @throws AlreadyConsumedSharedException
  */
 public function consume($code)
 {
     try {
         $card = $this->pickCardSharedWorker->pick($code);
     } catch (NonExistentCodeInternalException $e) {
         throw new NonExistentCodeSharedException();
     }
     if ($card['consumed']) {
         throw new AlreadyConsumedSharedException();
     }
     $this->connectToStorageInternalWorker->connect()->update(array('code' => $code), array('$set' => array('consumed' => true)));
 }
 /**
  * Collects invitation cards.
  *
  * @return \Iterator
  */
 public function collect()
 {
     return $this->connectToStorageInternalWorker->connect()->find()->fields(['_id' => 0]);
 }