/** * @ParamConverter("user", class="CoreUserBundle:User", options={"mapping":{"user_username" = "username"}}) * @ParamConverter("idea", class="AppBundle:Idea", options={"mapping":{"idea_slug" = "slug"}}) * @ParamConverter("comment", class="AppBundle:Comment", options={"mapping":{"comment_id" = "id"}}) */ public function addAction(Request $request, User $user, Idea $idea, Comment $comment) { if ($idea->getUserId() !== $user->getId() || $comment->getIdeaId() !== $idea->getId()) { throw new $this->createNotFoundException(); } $doctrine = $this->getDoctrine(); $manager = $doctrine->getManager(); $voteUser = $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY') ? $this->getUser() : null; $hash = $voteUser ? null : $this->get('core.base.form')->getUserHash($request); if ($voteUser && $voteUser->getId() === $comment->getUserId()) { $this->get('session')->getFlashBag()->add('error', "You can't vote for your own idea."); } else { $exist = $doctrine->getRepository('AppBundle:Vote')->findOneBy(['user' => $voteUser, 'hash' => $hash, 'comment' => $comment]); if ($exist) { $this->get('session')->getFlashBag()->add('error', 'You have already voted for this comment.'); } else { $vote = new Vote(); $vote->setComment($comment); if ($voteUser) { $vote->setUser($voteUser); } else { $vote->setHash($hash); } $manager->persist($vote); $manager->flush(); } } return $this->end($request, $user, $idea, $comment); }