<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Response, Request};
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mailer\MailerInterface;
use App\Entity\Contact;
use App\Form\ContactType;
use App\Services\{ContactService as Service};
class ContactController extends AbstractController
{
#[Route('/contact', name: 'app_contact')]
public function index(Request $request, Service $service): Response
{
$contact = new Contact();
$contactForm = $this->createForm(ContactType::class, $contact);
$contactForm->handleRequest($request);
if ($contactForm->isSubmitted() && $contactForm->isValid()) {
try {
$contact->setCreatedAt(new \DateTime());
$contact->setType(Contact::TYPE_FORMCONTACT);
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
$service->notifyResponsible($contact, $this->getParameter('contact_form.subject'), $this->getParameter('app_mailer_name'));
$this->addFlash(
'success',
'Message envoyé !'
);
} catch (\Throwable $th) {
$this->addFlash(
'warning',
'Erreur d\'envoie !'
);
}
}
return $this->render('contact/index.html.twig', [
'contactForm' => $contactForm->createView(),
]);
}
}