<?php
namespace App\Controller\BackOffice;
use App\Entity\Assay\Test;
use App\Form\Type\BackOffice\TestType;
use App\Model\PaginatedSearchCriteria;
use App\Service\TestsService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(path="tests")
*/
class TestsController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var FormFactoryInterface
*/
private $formFactory;
/**
* @var TestsService
*/
private $testsService;
public function __construct(
EntityManagerInterface $entityManager,
FormFactoryInterface $formFactory,
TestsService $testsService
) {
$this->entityManager = $entityManager;
$this->formFactory = $formFactory;
$this->testsService = $testsService;
}
/**
* @param Request $request
* @return Response
* @Route(path="", methods={"GET"})
*/
public function indexAction(Request $request): Response
{
$criteria = new PaginatedSearchCriteria($request->query);
$tests = $this->entityManager
->getRepository(Test::class)
->search($criteria);
return $this->render(
'backOffice/tests/index.html.twig',
[
'tests' => $tests,
]
);
}
/**
* @Route(path="/new", methods={"GET", "POST"})
* @param Request $request
* @return RedirectResponse|Response
*/
public function newAction(Request $request)
{
$test = new Test();
$testForm = $this->formFactory
->create(TestType::class, $test)
->add('submit', SubmitType::class);
$testForm->handleRequest($request);
if($testForm->isSubmitted() && $testForm->isValid()) {
/** @var Test $test */
$test = $testForm->getData();
$this->testsService->persistTest($test);
return $this->redirectToRoute(
'app_backoffice_tests_edit',
[
'testId' => $test->getTestId(),
]
);
}
return $this->render(
'backOffice/tests/new.html.twig',
[
'test' => $test,
'testForm' => $testForm->createView(),
]
);
}
/**
* @Route(path="/{testId}", methods={"GET", "POST"})
* @param $testId
* @param Request $request
* @return Response
*/
public function editAction($testId, Request $request): Response
{
$test = $this->loadTest($testId);
$testForm = $this->formFactory
->create(TestType::class, $test)
->add(
'submit',
SubmitType::class,
[
'label' => 'Save',
]
);
$testForm->handleRequest($request);
if($testForm->isSubmitted() && $testForm->isValid()) {
/** @var Test $test */
$test = $testForm->getData();
$this->testsService->persistTest($test);
return $this->redirectToRoute('app_backoffice_tests_index');
}
return $this->render(
'backOffice/tests/edit.html.twig',
[
'test' => $test,
'testForm' => $testForm->createView(),
]
);
}
/**
* @param $testId
* @return Test
*/
protected function loadTest($testId): object
{
$test = $this->entityManager
->getRepository(Test::class)
->find($testId);
if(!$test) {
throw $this->createNotFoundException();
}
return $test;
}
}