src/Controller/RescanConfigController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\RescanConfig;
  4. use App\Form\Type\RescanConfigType;
  5. use App\Service\RescanConfigService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12.  * @Route(path="rescan-config")
  13.  */
  14. class RescanConfigController extends AbstractController
  15. {
  16.     const CONFIG_ID            'configId';
  17.     const NOTIFICATION_SUCCESS 'notification-success';
  18.     /**
  19.      * @var RescanConfigService
  20.      */
  21.     private RescanConfigService $rescanConfigService;
  22.     /**
  23.      * @param RescanConfigService $rescanConfigService
  24.      */
  25.     public function __construct(RescanConfigService $rescanConfigService)
  26.     {
  27.         $this->rescanConfigService $rescanConfigService;
  28.     }
  29.     /**
  30.      * Rescan Config index
  31.      *
  32.      * @Route(path="/", name="rescan_config_index", methods={"GET"})
  33.      * @return Response
  34.      */
  35.     public function indexAction(): Response
  36.     {
  37.         $configs $this->rescanConfigService->getAll();
  38.         return $this->render('rescan/list.html.twig', [
  39.             'configs' => $configs,
  40.         ]);
  41.     }
  42.     /**
  43.      * @Route(path="/new", name="rescan_config_new", methods={"GET", "POST"})
  44.      *
  45.      * @param Request $request
  46.      * @return Response
  47.      */
  48.     public function newAction(Request $request): Response
  49.     {
  50.         $config = new RescanConfig();
  51.         $form   $this->createForm(RescanConfigType::class, $config);
  52.         $form->handleRequest($request);
  53.         $errors $this->rescanConfigService->validate($config);
  54.         if ($errors->count() === 0) {
  55.             if ($form->isSubmitted() && $form->isValid()) {
  56.                 $this->rescanConfigService->save($config);
  57.                 $this->addFlash(self::NOTIFICATION_SUCCESS'Configuration created');
  58.                 return $this->redirectToRoute('rescan_config_index');
  59.             }
  60.         }
  61.         return $this->renderForm('rescan/new.html.twig', [
  62.             'form' => $form,
  63.             'error' => $errors,
  64.         ]);
  65.     }
  66.     /**
  67.      * @Route(path="/edit/{id}", name="rescan_config_edit", methods={"GET", "POST"})
  68.      *
  69.      * @param Request $request
  70.      * @param int     $id
  71.      * @return Response
  72.      */
  73.     public function editAction(Request $requestint $id): Response
  74.     {
  75.         $config $this->rescanConfigService->getById($id);
  76.         if (empty($config)) {
  77.             throw new NotFoundHttpException('Configuration not found!');
  78.         }
  79.         $form $this->createForm(RescanConfigType::class, $config);
  80.         $form->handleRequest($request);
  81.         $errors $this->rescanConfigService->validate($config);
  82.         if ($errors->count() === 0) {
  83.             if ($form->isSubmitted() && $form->isValid()) {
  84.                 $this->rescanConfigService->save($config);
  85.                 $this->addFlash(self::NOTIFICATION_SUCCESS'Configuration updated');
  86.                 return $this->redirectToRoute('rescan_config_index');
  87.             }
  88.         }
  89.         return $this->renderForm('rescan/edit.html.twig', [
  90.             'form' => $form,
  91.             'error' => $errors,
  92.         ]);
  93.     }
  94.     /**
  95.      * @Route(path="/delete", name="rescan_config_delete", methods={"POST"})
  96.      *
  97.      * @param Request $request
  98.      * @return Response
  99.      */
  100.     public function deleteAction(Request $request): Response
  101.     {
  102.         $id $request->request->all()[self::CONFIG_ID] ?? null;
  103.         if (is_null($id)) {
  104.             throw new \InvalidArgumentException();
  105.         }
  106.         $variation $this->rescanConfigService->getById($id);
  107.         if (empty($variation)) {
  108.             throw new NotFoundHttpException('Configuration not found!');
  109.         }
  110.         $this->rescanConfigService->delete($variation);
  111.         $this->addFlash(self::NOTIFICATION_SUCCESS'Configuration removed');
  112.         return $this->redirectToRoute('rescan_config_index');
  113.     }
  114. }