private function processForm($request, $comment = null, $articleNumber = null, $languageCode = null)
{
$publicationService = $this->get('newscoop.publication_service');
$publication = $publicationService->getPublication();
if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') && false === (bool) $publication->getPublicCommentsEnabled()) {
throw new AccessDeniedException('Public comments are disabled');
}
$em = $this->container->get('em');
$commentService = $this->container->get('comment');
if (!$comment) {
$comment = new Comment();
$statusCode = 201;
$patch = false;
} else {
$statusCode = 200;
$comment = $em->getRepository('Newscoop\\Entity\\Comment')->findOneById($comment);
if (!$comment) {
throw new EntityNotFoundException('Result was not found.');
}
$patch = true;
}
$form = $this->createForm(new CommentType(array('patch' => $patch)), array(), array('method' => $request->getMethod()));
$form->handleRequest($request);
if ($form->isValid()) {
$attributes = $form->getData();
$user = $this->getUser();
if ($comment->getId() !== null) {
// update comment
$comment = $commentService->updateComment($comment, $attributes);
} else {
// create new comment
if ($user) {
$attributes['user'] = $user->getId();
} else {
if (!$attributes['name']) {
throw new InvalidArgumentException('When user is not logged in, then commenter name is required.');
}
}
if ($articleNumber) {
$attributes['thread'] = $articleNumber;
}
if ($languageCode) {
$attributes['language'] = $languageCode;
}
$attributes['time_created'] = new \DateTime();
$attributes['ip'] = $request->getClientIp();
$comment = $commentService->save($comment, $attributes, $user);
}
$response = new Response();
$response->setStatusCode($statusCode);
$response->headers->set('X-Location', $this->generateUrl('newscoop_gimme_comments_getcomment', array('id' => $comment->getId()), true));
return $response;
}
return $form;
}