/** * Start a new or existing game and redirect to it. * * @return \Zend\Http\Response */ public function startAction() { $id = (int) $this->params()->fromRoute('id'); $gameRepo = $this->getObjectManager()->getRepository(Game::class); $user = $this->getUser(); $game = null; if (null != $id) { $game = $gameRepo->findOneBy(['id' => $id, 'user' => $user->getId(), 'finishedAt' => null]); } if (!$game instanceof Game) { $game = new Game(); $game->setUser($user); $word = Word::getRandomWord($this->getObjectManager()); $game->setWord($word->getWord()); } $game->setLastActionAt(new DateTime()); $this->getObjectManager()->persist($user); $this->getObjectManager()->persist($game); $this->getObjectManager()->flush(); // put the game's id into the session $container = new Container('game'); $container->gameId = $game->getId(); return $this->redirect()->toRoute('application/default', ['controller' => 'game', 'action' => 'play']); }