/**
  * @inheritDoc
  */
 public function format(Thread $thread)
 {
     $postFormatter = new PostFormatter();
     $jsonResponse = ['thread' => ['id' => $thread->getId()], 'posts' => []];
     foreach ($thread->getPosts() as $post) {
         $jsonResponse['posts'][] = $postFormatter->format($post);
     }
     return $jsonResponse;
 }
 /**
  * @inheritDoc
  */
 public function createNewThread($boardId, $params)
 {
     $board = $this->repositories->boards()->find($boardId);
     if (!$board instanceof Board) {
         throw new \OutOfBoundsException(sprintf('Board with ID `%s` not found', $boardId));
     }
     $thread = new Thread();
     $thread->setBoard($board);
     $post = new Post();
     $post->setEmail($params['post']['email']);
     $post->setAuthor($params['post']['author']);
     $post->setContent($params['post']['content']);
     $post->setThread($thread);
     $thread->getPosts()->add($post);
     $em = $this->repositories->getEntityManager();
     $em->persist($post);
     $em->persist($thread);
     $em->flush();
     return $thread;
 }
 /**
  * Converts thread to JSON
  * @param Thread $thread
  * @return array
  */
 private function convertThreadToJSON(Thread $thread)
 {
     return ['id' => $thread->getId()];
 }