<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Form\ContactType;
use App\Notification\ContactNotification;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ContactController extends AbstractController
{
#[Route('/contact', name:"contact")]
public function contact(Request $request, ContactNotification $contactNotification)
{
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contactNotification->sendEmail($contact);
$this->addFlash('success', 'Message envoyé avec succès');
return $this->redirectToRoute('contact');
}
return $this->render('pages/contact.html.twig', [
'form' => $form->createView()
], new Response(null, $form->isSubmitted() && !$form->isValid() ? 422 : 200));
}
}