src/Controller/AnalyzeReportController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\OpenAIClient;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  12. #[Route("/analyze")]
  13. class AnalyzeReportController extends AbstractController
  14. {
  15.     #[Route("/"methods: ["GET""POST"])]
  16.     public function analyze(
  17.         OpenAIClient $client,
  18.         SessionInterface $session,
  19.         Request $request
  20.     ): Response
  21.     {
  22.         $form $this->createFormBuilder()
  23.             ->add('content'TextareaType::class, ['required' => true])
  24.             ->add('save_results'CheckboxType::class, ['required' => false])
  25.             ->getForm();
  26.         $form->handleRequest($request);
  27.         if ($form->isSubmitted() && $form->isValid()) {
  28.             try {
  29.                 $response $client->analyzeReport($form->get('content')->getData());
  30.             } catch (ClientExceptionInterface $e) {
  31.                 $response $e->getResponse()->getContent(false);
  32.             }
  33.             $session->set('analyzed'$response);
  34.             return $this->redirectToRoute('app_analyzereport_results');
  35.         }
  36.         return $this->render('analyze_report/analyze.html.twig', ['form' => $form->createView()]);
  37.     }
  38.     #[Route("/results"methods: ["GET"])]
  39.     public function results(SessionInterface $session): Response
  40.     {
  41.         $analyzed $session->get('analyzed');
  42.         $session->set('analyzed'null);
  43.         return $this->render('analyze_report/results.html.twig', [
  44.             'analyzed' => $analyzed,
  45.         ]);
  46.     }
  47. }