public function create(Application $app, $article_id)
 {
     $data = array("article_id" => $article_id);
     $commentForm = $app["form.factory"]->create(new \App\Form\Comment(), $data);
     if ("POST" === $app['request']->getMethod()) {
         $commentForm->bindRequest($app["request"]);
         if ($commentForm->isValid()) {
             $commentManager = $app['comment_manager'];
             $commentDatas = $commentForm->getData();
             /** @var $comment App\Model\Entity\Comment **/
             $comment = new \App\Model\Entity\Comment($commentDatas);
             $comment->ip = $app['request']->getClientIp();
             if (false == $this->spamManager->ipIsSpammer($comment->ip)) {
                 // if comment is not a spam
                 $status = $commentManager->insertComment($comment, $article_id);
                 $app["session"]->setFlash("success", "new comment added");
             }
         } else {
             $app["session"]->setFlash("error", "Error in the comment");
             $app['session']->setFlash('comment_errors', array_map(function ($formError) {
                 return $formError->getMessageTemplate();
             }, $commentForm->getErrors()));
         }
         return $app->redirect($app['request']->headers->get('referer'));
     }
     return $app["twig"]->render("comment/create.twig", array('article_id' => $article_id, "form" => $commentForm->createView()));
 }
 function doSignUp(Application $app)
 {
     $registrationForm = $app['form.factory']->create(new \App\Form\Register());
     $registrationForm->bindRequest($app['request']);
     if ($registrationForm->isValid()) {
         $datas = $registrationForm->getData();
         $userManager = $app['user_manager'];
         //username must be unique
         if ($userManager->usernameExists($datas['username']) == true) {
             $registrationForm->addError(new FormError('username already exists'));
         }
         //email must be unique
         if ($userManager->emailExists($datas['email']) == true) {
             $registrationForm->addError(new FormError('email already exists'));
         }
         if ($registrationForm->isValid()) {
             $user = new User();
             $user['username'] = $datas['username'];
             $user['firstname'] = $datas['firstname'];
             $user['lastname'] = $datas['lastname'];
             $user['email'] = $datas['email'];
             $user['roles'] = array($app['config.default_user_role']);
             # must be an array
             $user['password'] = self::encodePassword($user['username'], $datas['password_repeated'], $app);
             $user['ip'] = $app['request']->getClientIp();
             if (false == $this->spamManager->ipIsSpammer($user['ip'])) {
                 # protect from spammers
                 $userManager->registerUser($user);
                 //add flash success
                 $app['session']->setFlash('success', 'Your account was successfully created, please login');
                 return $app->redirect($app['url_generator']->generate('index.index'));
             }
         }
     }
     $app['session']->setFlash('error', 'The form contains errors');
     return $app['twig']->render('user/register.twig', array('registrationForm' => $registrationForm->createView()));
 }