private function processForm($request, $snippetId = null, $articleNumber = null, $languageCode = null)
{
// XXX It breaks using PATCH
$em = $this->container->get('em');
$patch = false;
if (!$snippetId) {
$templateId = $request->request->get('template');
if (!is_numeric($templateId)) {
throw new InvalidArgumentException("Parameter 'template' is not numeric");
}
$snippetTemplate = $em->getRepository('Newscoop\\Entity\\Snippet\\SnippetTemplate')->getTemplateById($templateId);
if (is_null($snippetTemplate)) {
$snippetTemplate = $em->getRepository('Newscoop\\Entity\\Snippet\\SnippetTemplate')->getTemplateById($templateId, 'all');
if (is_null($snippetTemplate)) {
throw new InvalidArgumentException("Template with ID: '" . $templateId . "' does not exist.");
}
throw new InvalidArgumentException("Template with ID: '" . $templateId . "' is not enabled.");
}
$snippet = new Snippet($snippetTemplate);
$statusCode = 201;
} else {
$snippet = $em->getRepository('Newscoop\\Entity\\Snippet')->getSnippetById($snippetId, 'all');
$statusCode = 200;
$patch = true;
if (is_null($snippet)) {
throw new NotFoundHttpException("Snippet with ID: '" . $snippetId . "' was not found");
}
}
$article = null;
if (!is_null($articleNumber) && !is_null($languageCode)) {
$article = $em->getRepository('Newscoop\\Entity\\Article')->getArticle($articleNumber, $languageCode)->getOneOrNullResult();
if (!$article) {
throw new NotFoundHttpException('Article with number:"' . $articleNumber . '" and language: "' . $languageCode . '" was not found.');
}
}
$form = $this->container->get('form.factory')->create(new SnippetType(array('patch' => $patch)), $snippet);
$form->handleRequest($request);
if ($form->isValid()) {
$snippet = $form->getData();
if ($article) {
$snippet->addArticle($article);
}
$em->getRepository('Newscoop\\Entity\\Snippet')->save($snippet);
$response = new Response();
$response->setStatusCode($statusCode);
$response->headers->set('X-Location', $this->generateUrl('newscoop_gimme_snippets_getsnippet', array('snippetId' => $snippet->getId()), true));
return $response;
}
return $form;
}