AdminController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Counter;
  4. use App\Form\CounterType;
  5. use App\Repository\CounterRepository;
  6. use App\Service\CounterManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Attribute\Route;
  13. use Symfony\Component\Workflow\Exception\LogicException;
  14. use Symfony\Component\Workflow\WorkflowInterface;
  15. #[Route('/admin')]
  16. class AdminController extends AbstractController
  17. {
  18. public function __construct(protected WorkflowInterface $countdownStateMachine)
  19. {
  20. }
  21. #[Route('/', name: 'app_admin_index', methods: ['GET'])]
  22. public function index(
  23. CounterRepository $counterRepository,
  24. ): Response
  25. {
  26. return $this->render('admin/index.html.twig', [
  27. 'counters' => $counterRepository->findAll()
  28. ]);
  29. }
  30. #[Route('/new', name: 'app_admin_new', methods: ['GET', 'POST'])]
  31. public function new(Request $request, CounterManager $counterManager): Response
  32. {
  33. $counter = new Counter();
  34. $form = $this->createForm(CounterType::class, $counter);
  35. $form->handleRequest($request);
  36. if ($form->isSubmitted() && $form->isValid()) {
  37. $this->countdownStateMachine->getMarking($counter);
  38. return $this->redirectToRoute('app_admin_index', [], Response::HTTP_SEE_OTHER);
  39. }
  40. return $this->render('admin/new.html.twig', [
  41. 'counter' => $counter,
  42. 'form' => $form,
  43. ]);
  44. }
  45. #[Route('{id}/reset', name: 'app_admin_reset', methods: ['GET'])]
  46. public function reset(Counter $counter): RedirectResponse
  47. {
  48. try {
  49. $this->countdownStateMachine->apply($counter, Counter::TRANSITION_TO_READY);
  50. } catch (LogicException $e) {
  51. }
  52. return $this->redirectToRoute('app_admin_index');
  53. }
  54. #[Route('/{id}/start', name: 'app_admin_start', methods: ['GET'])]
  55. public function start(Counter $counter): RedirectResponse
  56. {
  57. try {
  58. $this->countdownStateMachine->apply($counter, Counter::TRANSITION_TO_STARTED);
  59. } catch(LogicException $e) {
  60. }
  61. return $this->redirectToRoute('app_admin_index');
  62. }
  63. #[Route('/{id}', name: 'app_admin_show', methods: ['GET'])]
  64. public function show(Counter $counter): Response
  65. {
  66. return $this->render('admin/show.html.twig', [
  67. 'counter' => $counter,
  68. ]);
  69. }
  70. #[Route('/{id}/edit', name: 'app_admin_edit', methods: ['GET', 'POST'])]
  71. public function edit(Request $request, Counter $counter, EntityManagerInterface $entityManager): Response
  72. {
  73. $form = $this->createForm(CounterType::class, $counter);
  74. $form->handleRequest($request);
  75. if ($form->isSubmitted() && $form->isValid()) {
  76. $entityManager->flush();
  77. return $this->redirectToRoute('app_admin_index', [], Response::HTTP_SEE_OTHER);
  78. }
  79. return $this->render('admin/edit.html.twig', [
  80. 'counter' => $counter,
  81. 'form' => $form,
  82. ]);
  83. }
  84. #[Route('/{id}', name: 'app_admin_delete', methods: ['POST'])]
  85. public function delete(Request $request, Counter $counter, EntityManagerInterface $entityManager): Response
  86. {
  87. if ($this->isCsrfTokenValid('delete'.$counter->getId(), $request->request->get('_token'))) {
  88. $entityManager->remove($counter);
  89. $entityManager->flush();
  90. }
  91. return $this->redirectToRoute('app_admin_index', [], Response::HTTP_SEE_OTHER);
  92. }
  93. }