src/Controller/FrontController/DemoController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller\FrontController;
  3. use App\Repository\ProductRepository;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\Mime\Email;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class DemoController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/demonstration", name="app_demo", methods={"GET", "POST"})
  15.      */
  16.     public function index(Request $request, MailerInterface $mailer, ProductRepository $productRepository): Response
  17.     {
  18.         if ($request->isMethod('POST')) {
  19.             if (!$this->isCsrfTokenValid('demo_form', $request->request->get('_token'))) {
  20.                 throw $this->createAccessDeniedException('Jeton CSRF invalide.');
  21.             }
  22.             $name = trim((string) $request->request->get('name'));
  23.             $email = trim((string) $request->request->get('email'));
  24.             $phone = trim((string) $request->request->get('phone'));
  25.             $productId = (int) $request->request->get('product_id');
  26.             $requesterType = trim((string) $request->request->get('requester_type'));
  27.             $location = trim((string) $request->request->get('location'));
  28.             $role = trim((string) $request->request->get('role'));
  29.             $productName = trim((string) $request->request->get('product_name'));
  30.             $productType = trim((string) $request->request->get('product_type'));
  31.             $selectedProduct = $productId > 0 ? $productRepository->find($productId) : null;
  32.             if ($selectedProduct) {
  33.                 $productName = $selectedProduct->getTitle();
  34.                 $productType = implode(' / ', $selectedProduct->getStatetypeLabels());
  35.             }
  36.             if ($name === '' || !filter_var($email, FILTER_VALIDATE_EMAIL) || $requesterType === '' || $location === '' || $role === '' || $productName === '' || $productType === '') {
  37.                 $this->addFlash('error', 'Veuillez remplir correctement les champs obligatoires.');
  38.                 return $this->redirectToRoute('app_demo');
  39.             }
  40.             $mailData = [
  41.                 'name' => $name,
  42.                 'email' => $email,
  43.                 'phone' => $phone !== '' ? $phone : 'Non renseigné',
  44.                 'requester_type' => $requesterType,
  45.                 'location' => $location,
  46.                 'role' => $role,
  47.                 'product_name' => $productName,
  48.                 'product_type' => $productType,
  49.             ];
  50.             $mail = (new Email())
  51.                 ->from((string) $this->getParameter('mailer_from'))
  52.                 ->to((string) $this->getParameter('contact_recipient'))
  53.                 ->replyTo($email)
  54.                 ->subject('Demande de démonstration - ' . $productName)
  55.                 ->html($this->renderView('emails/demo.html.twig', $mailData))
  56.                 ->text($this->renderView('emails/demo.txt.twig', $mailData));
  57.             try {
  58.                 $mailer->send($mail);
  59.             } catch (TransportExceptionInterface $exception) {
  60.                 $this->addFlash('error', 'L’envoi de votre demande a échoué. Veuillez réessayer.');
  61.                 return $this->redirectToRoute('app_demo');
  62.             }
  63.             $this->addFlash('success', 'Votre demande de démonstration a bien été envoyée.');
  64.             return $this->redirectToRoute('app_demo');
  65.         }
  66.         $productId = (int) $request->query->get('product_id');
  67.         $selectedProduct = $productId > 0 ? $productRepository->find($productId) : null;
  68.         return $this->render('frontView/demo.html.twig', [
  69.             'selected_product' => $selectedProduct,
  70.         ]);
  71.     }
  72. }