src/Controller/External/WebImpactCalculatorController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller\External;
  3. use App\Entity\Report;
  4. use App\Form\NewReportFormType;
  5. use App\Message\Report as ReportMessage;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Form\FormError;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Messenger\MessageBusInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class WebImpactCalculatorController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/web-impact-calculator", name="app_web_impact_calculator", priority="10")
  17.      */
  18.     public function index(): Response
  19.     {
  20.         if ($this->isGranted('ROLE_USER')) {
  21.             return $this->redirectToRoute('app_user_dashboard');
  22.         }
  23.         return $this->redirectToRoute('app_index');
  24.     } 
  25.     /**
  26.      * @Route("/web-impact-calculator/estimate/{hash}/waiting", name="app_web_impact_calculator_estimate_waiting")
  27.      */
  28.     public function waiting(Report $report): Response
  29.     {
  30.         return $this->render(
  31.             'external/web_impact_calculator/waiting.html.twig', [
  32.                 'report' => $report,
  33.             ]
  34.         );
  35.     }
  36.     /**
  37.      * @Route("/web-impact-calculator/estimate/new", name="app_web_impact_calculator_estimate_new")
  38.      */
  39.     public function new(Request $requestEntityManagerInterface $entityManagerMessageBusInterface $bus): Response
  40.     {
  41.         $report = new Report();
  42.         $form $this->createForm(NewReportFormType::class, $report);
  43.         if ($formData $request->get('new_report_form')) {
  44.             $form->submit($formData);
  45.         } else {
  46.             $form->handleRequest($request);
  47.         }
  48.         if ($form->isSubmitted() && $form->isValid()) {
  49.             $existingAnonymousReport $entityManager->getRepository(Report::class)->findOneBy(
  50.                 [
  51.                     'user' => null,
  52.                     'domain' => $report->getDomain(),
  53.                 ]
  54.             );
  55.             if (null !== $existingAnonymousReport) {
  56.                 $report $existingAnonymousReport;
  57.             } else {
  58.                 $entityManager->persist($report);
  59.                 $entityManager->flush();
  60.             }
  61.             $bus->dispatch(
  62.                 new ReportMessage(
  63.                     $report->getId()
  64.                 )
  65.             );
  66.             return $this->redirectToRoute('app_web_impact_calculator_estimate_waiting', [
  67.                 'hash' => $report->getHash(),
  68.             ]);
  69.         }
  70.         $formErrors = [];
  71.         /** @var FormError $error */
  72.         foreach ($form->getErrors(true) as $error) {
  73.             $formErrors[] = $error->getMessage();
  74.         }
  75.         $this->addFlash('error'implode(','$formErrors));
  76.         return $this->redirectToRoute('app_web_impact_calculator');
  77.     }
  78. }