private function processForm($request, $image = null)
{
$em = $this->container->get('em');
$imageService = $this->container->get('image');
if (!$image) {
$statusCode = 201;
} else {
$statusCode = 200;
$image = $em->getRepository('Newscoop\\Image\\LocalImage')->findOneById($image);
if (!$image) {
throw new EntityNotFoundException('Result was not found.');
}
}
$form = $this->createForm(new ImageType(), array(), array('image' => $image, 'method' => $request->getMethod()));
$form->handleRequest($request);
if ($form->isValid()) {
$file = $form['image']->getData();
$attributes = $form->getData();
$user = $this->getUser();
if ($user) {
$attributes['user'] = $user;
}
if ($file) {
$image = $imageService->upload($file, $attributes, $image);
} else {
$imageService->fillImage($image, $attributes);
$em->flush();
}
return new FOSView\View($image, $statusCode, array('X-Location' => $this->generateUrl('newscoop_gimme_images_getimage', array('number' => $image->getId()), true)));
}
return $form;
}