/** * Send form * * @Route("/send-form") * @Method("POST") */ public function sendForm(Request $request) { $gifts = array('day05' => 1, 'day12' => 1, 'day15' => 1, 'day19' => 3, 'day20' => 1, 'day21' => 3, 'day27' => 5, 'day29' => 3); if (null === ($content = $request->getContent())) { throw new BadRequestHttpException('No content given'); } $content = json_decode($content, true); if (null === $content) { throw new BadRequestHttpException('Unparsable content given'); } if (substr($content['day'], 3, 2) !== date('j')) { return new JsonResponse(array('errors' => array('This is not the good day !'))); } $repo = $this->getDoctrine()->getRepository('AppBundle:Player'); $player = new Player(); $player->setEmail(isset($content['email']) ? $content['email'] : null)->setFirstname(isset($content['firstname']) ? $content['firstname'] : null)->setLastname(isset($content['lastname']) ? $content['lastname'] : null)->setDay(isset($content['day']) ? $content['day'] : null); $validator = $this->get('validator'); $errors = $validator->validate($player); if (count($errors) > 0) { $messages = array(); foreach ($errors as $error) { $messages[] = $error->getMessage(); } return new JsonResponse(array('errors' => $messages), 200); } $checksum = hash('md5', $player->getEmail() . $content['day']); $hacker = count($repo->findBy(array('checksum' => $checksum, 'day' => $content['day']))); $winnersOfTheDay = count($repo->findBy(array('winner' => true, 'day' => $content['day']))); $win = false; if ($hacker == 0 && $winnersOfTheDay < $gifts[$content['day']]) { /* Algorithme selection vainqueur */ $rand = mt_rand(1, 10); if ($rand == 1) { $win = true; $message = \Swift_Message::newInstance()->setSubject('Beautiful Wishes')->setFrom('*****@*****.**')->setTo(array('*****@*****.**', '*****@*****.**'))->setBody($this->renderView('AppBundle:Calendar:email.txt.twig', array('player' => $player))); $this->get('mailer')->send($message); } } $player->setWinner($win)->setChecksum($checksum); $em = $this->getDoctrine()->getManager(); $em->persist($player); $em->flush(); return new JsonResponse(array('errors' => $errors, 'win' => $win), 200); }