<?php
declare(strict_types = 1);
namespace App\Action;
use App\Domain\Repository\Interfaces\TextRepositoryInterface;
use App\Form\Handler\ContactFormHandler;
use App\Form\Type\ContactFormType;
use App\Responder\RedirectResponder;
use App\Responder\TemplateResponder;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class ContactAction extends AbstractAction
{
private $templateResponder;
private $redirectResponder;
private $formFactory;
private $contactFormHandler;
public function __construct(
TemplateResponder $templateResponder,
RedirectResponder $redirectResponder,
FormFactoryInterface $formFactory,
ContactFormHandler $contactFormHandler
)
{
$this->templateResponder = $templateResponder;
$this->redirectResponder = $redirectResponder;
$this->formFactory = $formFactory;
$this->contactFormHandler = $contactFormHandler;
}
/**
* @Route("/contact", name="contact")
*/
public function __invoke(Request $request)
{
$contactType = $this->formFactory
->create(ContactFormType::class)
->handleRequest($request);
if ($this->contactFormHandler->handle($contactType)) {
return $this->redirectResponder->__invoke('contact', ["success" => 1]);
}
if($request->query->get('success')){
$request->getSession()->getFlashBag()->add('success', 'Votre message a bien été envoyé.');
}
return $this->templateResponder->__invoke(
'contact.html.twig', [
'form' => $contactType->createView()
]
);
}
}