123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Controller;
- use App\Entity\Counter;
- use App\Form\CounterType;
- use App\Repository\CounterRepository;
- use App\Service\CounterManager;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Routing\Attribute\Route;
- use Symfony\Component\Workflow\Exception\LogicException;
- use Symfony\Component\Workflow\WorkflowInterface;
- #[Route('/admin')]
- class AdminController extends AbstractController
- {
- public function __construct(protected WorkflowInterface $countdownStateMachine)
- {
-
- }
- #[Route('/', name: 'app_admin_index', methods: ['GET'])]
- public function index(
- CounterRepository $counterRepository,
- ): Response
- {
- return $this->render('admin/index.html.twig', [
- 'counters' => $counterRepository->findAll()
- ]);
- }
- #[Route('/new', name: 'app_admin_new', methods: ['GET', 'POST'])]
- public function new(Request $request, CounterManager $counterManager): Response
- {
- $counter = new Counter();
- $form = $this->createForm(CounterType::class, $counter);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- $this->countdownStateMachine->getMarking($counter);
- return $this->redirectToRoute('app_admin_index', [], Response::HTTP_SEE_OTHER);
- }
- return $this->render('admin/new.html.twig', [
- 'counter' => $counter,
- 'form' => $form,
- ]);
- }
- #[Route('{id}/reset', name: 'app_admin_reset', methods: ['GET'])]
- public function reset(Counter $counter): RedirectResponse
- {
- try {
- $this->countdownStateMachine->apply($counter, Counter::TRANSITION_TO_READY);
- } catch (LogicException $e) {
- }
- return $this->redirectToRoute('app_admin_index');
- }
- #[Route('/{id}/start', name: 'app_admin_start', methods: ['GET'])]
- public function start(Counter $counter): RedirectResponse
- {
- try {
- $this->countdownStateMachine->apply($counter, Counter::TRANSITION_TO_STARTED);
- } catch(LogicException $e) {
- }
- return $this->redirectToRoute('app_admin_index');
- }
- #[Route('/{id}', name: 'app_admin_show', methods: ['GET'])]
- public function show(Counter $counter): Response
- {
- return $this->render('admin/show.html.twig', [
- 'counter' => $counter,
- ]);
- }
- #[Route('/{id}/edit', name: 'app_admin_edit', methods: ['GET', 'POST'])]
- public function edit(Request $request, Counter $counter, EntityManagerInterface $entityManager): Response
- {
- $form = $this->createForm(CounterType::class, $counter);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- $entityManager->flush();
- return $this->redirectToRoute('app_admin_index', [], Response::HTTP_SEE_OTHER);
- }
- return $this->render('admin/edit.html.twig', [
- 'counter' => $counter,
- 'form' => $form,
- ]);
- }
- #[Route('/{id}', name: 'app_admin_delete', methods: ['POST'])]
- public function delete(Request $request, Counter $counter, EntityManagerInterface $entityManager): Response
- {
- if ($this->isCsrfTokenValid('delete'.$counter->getId(), $request->request->get('_token'))) {
- $entityManager->remove($counter);
- $entityManager->flush();
- }
- return $this->redirectToRoute('app_admin_index', [], Response::HTTP_SEE_OTHER);
- }
- }
|