src/Controller/RegisterController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Applicant;
  4. use App\Events;
  5. use App\Form\ApplicantType;
  6. use Boab\CmsBundle\Controller\BaseController;
  7. use Boab\CmsBundle\Security\RandomGeneratorInterface;
  8. use Boab\CmsBundle\Security\RandomStringGenerator;
  9. use DateTimeImmutable;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Routing\RouterInterface;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. class RegisterController extends AbstractController
  20. {
  21.     public function __construct(
  22.         private EntityManagerInterface $entityManager,
  23.         private RouterInterface $router,
  24.         private LoggerInterface $logger,
  25.         private EventDispatcherInterface $eventDispatcher
  26.     ){}
  27.     #[Route('/register'name'app_register')]
  28.     public function index(Request $requestRandomGeneratorInterface $generatorstring $_route): Response
  29.     {
  30.         $form $this->createForm(ApplicantType::class, new Applicant, [
  31.             "method" => 'POST',
  32.             "action" => $this->router->generate($_route)
  33.         ]);
  34.         $form->handleRequest($request);
  35.         if($form->isSubmitted() && $form->isValid()){
  36.             $applicant $form->getData();
  37.             $applicant->setCreatedAt(new DateTimeImmutable('now'));
  38.             $uniqueId $generator->generateNumbers(8);
  39.             $applicant->setUniqueId($uniqueId);
  40.             try{
  41.                 $this->entityManager->persist($applicant);
  42.                 $this->entityManager->flush();
  43.                 $this->eventDispatcher->dispatch($applicantEvents::APPLICANT_REGISTERED);
  44.             }catch(\Exception $e){
  45.                 $this->logger->error($e->getMessage(), ['exception'=>$e]);
  46.             }
  47.             $this->addFlash("success""Thank you for submitting your application. If you have any inquiries contact us (239) 555-0108");
  48.             
  49.             return new RedirectResponse($this->router->generate($_route,['status'=>'success']));
  50.         }
  51.         return $this->render('register/index.html.twig', [
  52.             'controller_name' => 'RegisterController',
  53.             'pageTitle' => 'Register',
  54.             "form" => $form->createView()
  55.         ]);
  56.     }
  57. }