<?php
namespace App\Controller;
use App\Entity\Applicant;
use App\Events;
use App\Form\ApplicantType;
use Boab\CmsBundle\Controller\BaseController;
use Boab\CmsBundle\Security\RandomGeneratorInterface;
use Boab\CmsBundle\Security\RandomStringGenerator;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class RegisterController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
private RouterInterface $router,
private LoggerInterface $logger,
private EventDispatcherInterface $eventDispatcher
){}
#[Route('/register', name: 'app_register')]
public function index(Request $request, RandomGeneratorInterface $generator, string $_route): Response
{
$form = $this->createForm(ApplicantType::class, new Applicant, [
"method" => 'POST',
"action" => $this->router->generate($_route)
]);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$applicant = $form->getData();
$applicant->setCreatedAt(new DateTimeImmutable('now'));
$uniqueId = $generator->generateNumbers(8);
$applicant->setUniqueId($uniqueId);
try{
$this->entityManager->persist($applicant);
$this->entityManager->flush();
$this->eventDispatcher->dispatch($applicant, Events::APPLICANT_REGISTERED);
}catch(\Exception $e){
$this->logger->error($e->getMessage(), ['exception'=>$e]);
}
$this->addFlash("success", "Thank you for submitting your application. If you have any inquiries contact us (239) 555-0108");
return new RedirectResponse($this->router->generate($_route,['status'=>'success']));
}
return $this->render('register/index.html.twig', [
'controller_name' => 'RegisterController',
'pageTitle' => 'Register',
"form" => $form->createView()
]);
}
}