<?php
namespace App\Controller;
use App\Entity\RescanConfig;
use App\Form\Type\RescanConfigType;
use App\Service\RescanConfigService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(path="rescan-config")
*/
class RescanConfigController extends AbstractController
{
const CONFIG_ID = 'configId';
const NOTIFICATION_SUCCESS = 'notification-success';
/**
* @var RescanConfigService
*/
private RescanConfigService $rescanConfigService;
/**
* @param RescanConfigService $rescanConfigService
*/
public function __construct(RescanConfigService $rescanConfigService)
{
$this->rescanConfigService = $rescanConfigService;
}
/**
* Rescan Config index
*
* @Route(path="/", name="rescan_config_index", methods={"GET"})
* @return Response
*/
public function indexAction(): Response
{
$configs = $this->rescanConfigService->getAll();
return $this->render('rescan/list.html.twig', [
'configs' => $configs,
]);
}
/**
* @Route(path="/new", name="rescan_config_new", methods={"GET", "POST"})
*
* @param Request $request
* @return Response
*/
public function newAction(Request $request): Response
{
$config = new RescanConfig();
$form = $this->createForm(RescanConfigType::class, $config);
$form->handleRequest($request);
$errors = $this->rescanConfigService->validate($config);
if ($errors->count() === 0) {
if ($form->isSubmitted() && $form->isValid()) {
$this->rescanConfigService->save($config);
$this->addFlash(self::NOTIFICATION_SUCCESS, 'Configuration created');
return $this->redirectToRoute('rescan_config_index');
}
}
return $this->renderForm('rescan/new.html.twig', [
'form' => $form,
'error' => $errors,
]);
}
/**
* @Route(path="/edit/{id}", name="rescan_config_edit", methods={"GET", "POST"})
*
* @param Request $request
* @param int $id
* @return Response
*/
public function editAction(Request $request, int $id): Response
{
$config = $this->rescanConfigService->getById($id);
if (empty($config)) {
throw new NotFoundHttpException('Configuration not found!');
}
$form = $this->createForm(RescanConfigType::class, $config);
$form->handleRequest($request);
$errors = $this->rescanConfigService->validate($config);
if ($errors->count() === 0) {
if ($form->isSubmitted() && $form->isValid()) {
$this->rescanConfigService->save($config);
$this->addFlash(self::NOTIFICATION_SUCCESS, 'Configuration updated');
return $this->redirectToRoute('rescan_config_index');
}
}
return $this->renderForm('rescan/edit.html.twig', [
'form' => $form,
'error' => $errors,
]);
}
/**
* @Route(path="/delete", name="rescan_config_delete", methods={"POST"})
*
* @param Request $request
* @return Response
*/
public function deleteAction(Request $request): Response
{
$id = $request->request->all()[self::CONFIG_ID] ?? null;
if (is_null($id)) {
throw new \InvalidArgumentException();
}
$variation = $this->rescanConfigService->getById($id);
if (empty($variation)) {
throw new NotFoundHttpException('Configuration not found!');
}
$this->rescanConfigService->delete($variation);
$this->addFlash(self::NOTIFICATION_SUCCESS, 'Configuration removed');
return $this->redirectToRoute('rescan_config_index');
}
}