src/Controller/ContactController.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Entity\Config;
  5. use App\Form\ContactForm;
  6. use App\Services\ContactService;
  7. use App\Services\MailerService;
  8. use App\Services\CallApiServices;
  9. use COM;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  12. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\FormBuilder;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use ReCaptcha\ReCaptcha// Include the recaptcha lib
  21. class ContactController extends AbstractController
  22. {
  23.     #[Route('/contact'name'app_contact')]
  24.     public function index(FlashBagInterface $flashContactService $contactServiceMailerService $mailerServiceRequest $requestCallApiServices $callApiServices): Response
  25.     {
  26.         $serviceId $this->getParameter('app.serviceId');
  27.         $accountId $this->getParameter('app.accountId');
  28.         $contact = new Contact();
  29.         $config $callApiServices->configuratationPlateforme($accountId$serviceId);
  30.         $recaptchaKey $config['recaptchaTab']['sitekey'];
  31.         $secretkey $config['recaptchaTab']['secretkey'];
  32.         $messageError "";
  33.         $form $this->createForm(ContactForm::class, $contact);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             $recaptcha = new ReCaptcha($secretkey);
  37.             $resp $recaptcha->verify($request->request->get('g-recaptcha-response'), $request->getClientIp());
  38.           
  39.             if (!$resp->isSuccess()) {
  40.               // Do something if the submit wasn't valid ! Use the message to show something
  41.               $messageError "Le reCAPTCHA n'a pas été saisi correctement. Réessayez.";
  42.     
  43.             } else {
  44.                 $contact $form->getData();
  45.                 $contactService->sendEmail($contact);
  46.                 return $this->redirectToRoute('app_contact');
  47.             }
  48.         }
  49.         return $this->render('contact/index.html.twig', [
  50.             "contactForm" => $form->createView(),
  51.             "recaptchaKey" => $recaptchaKey,
  52.             "messageError" => $messageError,
  53.         ]);
  54.     }
  55. }