/**
  * Write a Gallery, this method is used by both POST and PUT action methods.
  *
  * @param Request  $request Symfony request
  * @param int|null $id      A Gallery identifier
  *
  * @return \FOS\RestBundle\View\View|FormInterface
  */
 protected function handleWriteGallery($request, $id = null)
 {
     $gallery = $id ? $this->getGallery($id) : null;
     $form = $this->formFactory->createNamed(null, 'sonata_media_api_form_gallery', $gallery, array('csrf_protection' => false));
     $form->bind($request);
     if ($form->isValid()) {
         $gallery = $form->getData();
         $this->galleryManager->save($gallery);
         $view = FOSRestView::create($gallery);
         $serializationContext = SerializationContext::create();
         $serializationContext->setGroups(array('sonata_api_read'));
         $serializationContext->enableMaxDepthChecks();
         $view->setSerializationContext($serializationContext);
         return $view;
     }
     return $form;
 }