/**
  * Publishes a gif and posts a link on social networks
  * @param Gif $gif
  * @return bool
  */
 public function publish(Gif $gif)
 {
     if (!$gif) {
         return false;
     }
     if (!$gif->getGifStatus() == GifState::ACCEPTED) {
         return false;
     }
     $gif->setPublishDate(new DateTime());
     $gif->setGifStatus(GifState::PUBLISHED);
     $gif->generateUrlReadyPermalink();
     // Check if permalink is unique
     /** @var GifRepository $gifsRepo */
     $gifsRepo = $this->em->getRepository('LjdsBundle:Gif');
     $permalink = $gif->getPermalink();
     $i = 1;
     while (!empty($gifsRepo->findBy(['permalink' => $gif->getPermalink(), 'gifStatus' => GifState::PUBLISHED]))) {
         // Generate a new permalink
         $gif->setPermalink($permalink . $i);
         $i++;
     }
     $this->em->flush();
     if ($this->facebookAutopost) {
         $this->facebookService->postGif($gif);
     }
     if ($this->twitterAutopost) {
         $this->twitterService->postGif($gif);
     }
     return true;
 }
 /**
  * @Route("/submit", name="submit", options={"sitemap"=true})
  */
 public function submitAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $response = new Response();
     $gifSubmitted = false;
     $gifSubmittedError = false;
     // Form is submitted
     $post = $request->request;
     if ($post->has('caption')) {
         // Check if mandatory fields are filled up
         if (trim($post->get('submittedBy')) == '' || trim($post->get('caption')) == '' || trim($post->get('gifUrl')) == '') {
             $gifSubmittedError = "un des champs requis n'est pas renseigné, veuillez rééssayer.";
         }
         // Check if URL is a gif/mp4 video
         $allowedFilesTypes = ['gif', 'mp4', 'webm', 'ogg'];
         $gifUrl = $post->get('gifUrl');
         if (!in_array(Util::getFileExtension($gifUrl), $allowedFilesTypes)) {
             $gifSubmittedError = "l'URL ne semble pas être celle d'un fichier gif. Les types autorisés sont : gif, mp4, webm et ogg.";
         }
         $gifSubmitted = true;
         $submittedBy = $post->get('submittedBy');
         $caption = $post->get('caption');
         $source = $post->get('source');
         $label = $post->get('label');
         // Create cookie with submittedBy value
         $cookie = new Cookie('submittedBy', $submittedBy, time() + 60 * 60 * 24 * 30);
         $response->headers->setCookie($cookie);
         if ($gifSubmittedError === false) {
             $gif = new Gif();
             $gif->setCaption($caption);
             $gif->setGifUrl($gifUrl);
             $gif->setReportStatus(ReportState::NONE);
             $gif->setGifStatus(GifState::SUBMITTED);
             $gif->generateUrlReadyPermalink();
             $gif->setSubmissionDate(new \DateTime());
             $gif->setSubmittedBy($submittedBy);
             $gif->setSource($source);
             $gif->setLabel($label);
             $em->persist($gif);
             $em->flush();
             $gifRepo = $this->getDoctrine()->getRepository('LjdsBundle:Gif');
             $params['estimatedPublishDate'] = $gifRepo->getEstimatedPublicationDate();
         } else {
             $params['submitError'] = $gifSubmittedError;
         }
     }
     $params['submittedBy'] = $request->cookies->has('submittedBy') ? $request->cookies->get('submittedBy') : '';
     $params['submitted'] = $gifSubmitted;
     $response->setContent($this->renderView('LjdsBundle:Gifs:submit.html.twig', $params));
     return $response;
 }