<?php
declare(strict_types=1);
/**
* Copyright (c) 2019. Dreamoov.
*/
namespace App\Controller;
use App\Form\ContactType;
use App\Models\Contact;
use App\Service\EmailService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class ContactController.
*
* @Route("", name="contact_", options={"sitemap": true})
*/
class ContactController extends AbstractController
{
/**
* @Route("/contact/", name="index")
*
* @param MailerInterface $mailer
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function index(Request $request, EmailService $emailService)
{
$contact = new Contact();
$isSent = false;
$form = $this->createForm(ContactType::class, $contact, [
'action' => $this->generateUrl('contact_index'),
'method' => 'POST',
]);
$form->handleRequest($request);
$resultMailer = null;
if ($form->isSubmitted() && $form->isValid()) {
$isSent = true;
$resultMailer = true;
$emailService->sendMailToSauvegarde('Demande via le formulaire de contact pitaxes.ch', 'email/contact.html.twig', ['contact' => $contact]);
unset($contact, $form);
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact, [
'action' => $this->generateUrl('contact_index'),
'method' => 'POST',
]);
}
return $this->render('contact/index.html.twig', [
'form' => $form->createView(),
'email_state' => $resultMailer,
'isSent' => $isSent,
]);
}
}