コード例 #1
0
ファイル: GameManager.php プロジェクト: Neirda24/bees
 /**
  * @param bool $forceNew
  *
  * @return Game
  * @throws Exception
  */
 public function getCurrentGame($forceNew = false)
 {
     if (true === $forceNew) {
         $this->currentGame = null;
     }
     if (!$this->currentGame instanceof Game) {
         if (false === $forceNew && $this->session->has(static::SESSION_CURRENT_GAME)) {
             $this->currentGame = $this->gameRepository->find($this->session->get(static::SESSION_CURRENT_GAME));
         } else {
             $queenBee = $this->queenBeeRepository->findOneBy(['name' => static::DEFAULT_QUEEN_BEE_NAME]);
             if (!$queenBee instanceof QueenBee) {
                 throw new Exception('? did you change the database ?');
             }
             $newGame = new Game();
             $newGame->setQueenBee($queenBee);
             $this->entityManagerInterface->persist($newGame);
             $this->entityManagerInterface->flush($newGame);
             $this->session->set(static::SESSION_CURRENT_GAME, $newGame->getId());
             $this->currentGame = $newGame;
         }
     }
     return $this->currentGame;
 }