src/Controller/BackOffice/TestsController.php line 112

Open in your IDE?
  1. <?php
  2. namespace App\Controller\BackOffice;
  3. use App\Entity\Assay\Test;
  4. use App\Form\Type\BackOffice\TestType;
  5. use App\Model\PaginatedSearchCriteria;
  6. use App\Service\TestsService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  10. use Symfony\Component\Form\FormFactoryInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @Route(path="tests")
  17.  */
  18. class TestsController extends AbstractController
  19. {
  20.     /**
  21.      * @var EntityManagerInterface
  22.      */
  23.     private $entityManager;
  24.     /**
  25.      * @var FormFactoryInterface
  26.      */
  27.     private $formFactory;
  28.     /**
  29.      * @var TestsService
  30.      */
  31.     private $testsService;
  32.     public function __construct(
  33.         EntityManagerInterface $entityManager,
  34.         FormFactoryInterface $formFactory,
  35.         TestsService $testsService
  36.     ) {
  37.         $this->entityManager $entityManager;
  38.         $this->formFactory $formFactory;
  39.         $this->testsService $testsService;
  40.     }
  41.     /**
  42.      * @param Request $request
  43.      * @return Response
  44.      * @Route(path="", methods={"GET"})
  45.      */
  46.     public function indexAction(Request $request): Response
  47.     {
  48.         $criteria = new PaginatedSearchCriteria($request->query);
  49.         $tests $this->entityManager
  50.             ->getRepository(Test::class)
  51.             ->search($criteria);
  52.         return $this->render(
  53.             'backOffice/tests/index.html.twig',
  54.             [
  55.                 'tests' => $tests,
  56.             ]
  57.         );
  58.     }
  59.     /**
  60.      * @Route(path="/new", methods={"GET", "POST"})
  61.      * @param Request $request
  62.      * @return RedirectResponse|Response
  63.      */
  64.     public function newAction(Request $request)
  65.     {
  66.         $test = new Test();
  67.         $testForm $this->formFactory
  68.             ->create(TestType::class, $test)
  69.             ->add('submit'SubmitType::class);
  70.         $testForm->handleRequest($request);
  71.         if($testForm->isSubmitted() && $testForm->isValid()) {
  72.             /** @var Test $test */
  73.             $test $testForm->getData();
  74.             $this->testsService->persistTest($test);
  75.             return $this->redirectToRoute(
  76.                 'app_backoffice_tests_edit',
  77.                 [
  78.                     'testId' => $test->getTestId(),
  79.                 ]
  80.             );
  81.         }
  82.         return $this->render(
  83.             'backOffice/tests/new.html.twig',
  84.             [
  85.                 'test' => $test,
  86.                 'testForm' => $testForm->createView(),
  87.             ]
  88.         );
  89.     }
  90.     /**
  91.      * @Route(path="/{testId}", methods={"GET", "POST"})
  92.      * @param $testId
  93.      * @param Request $request
  94.      * @return Response
  95.      */
  96.     public function editAction($testIdRequest $request): Response
  97.     {
  98.         $test $this->loadTest($testId);
  99.         $testForm $this->formFactory
  100.             ->create(TestType::class, $test)
  101.             ->add(
  102.                 'submit',
  103.                 SubmitType::class,
  104.                 [
  105.                     'label' => 'Save',
  106.                 ]
  107.             );
  108.         $testForm->handleRequest($request);
  109.         if($testForm->isSubmitted() && $testForm->isValid()) {
  110.             /** @var Test $test */
  111.             $test $testForm->getData();
  112.             $this->testsService->persistTest($test);
  113.             return $this->redirectToRoute('app_backoffice_tests_index');
  114.         }
  115.         return $this->render(
  116.             'backOffice/tests/edit.html.twig',
  117.             [
  118.                 'test' => $test,
  119.                 'testForm' => $testForm->createView(),
  120.             ]
  121.         );
  122.     }
  123.     /**
  124.      * @param $testId
  125.      * @return Test
  126.      */
  127.     protected function loadTest($testId): object
  128.     {
  129.         $test $this->entityManager
  130.             ->getRepository(Test::class)
  131.             ->find($testId);
  132.         if(!$test) {
  133.             throw $this->createNotFoundException();
  134.         }
  135.         return $test;
  136.     }
  137. }