107 lines
3.8 KiB
PHP
107 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Admin;
|
|
|
|
use App\Entity\ScriptMapping;
|
|
use App\Exception\GitSyncFailedException;
|
|
use App\Form\ScriptMappingType;
|
|
use App\Service\GitSynchronizer;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
#[Route('/admin/mappings')]
|
|
final class ScriptMappingController extends AbstractController
|
|
{
|
|
public function __construct(private readonly EntityManagerInterface $entityManager)
|
|
{
|
|
}
|
|
|
|
#[Route('/new', name: 'admin_mapping_new', methods: ['GET', 'POST'])]
|
|
public function new(Request $request): Response
|
|
{
|
|
$mapping = new ScriptMapping();
|
|
$form = $this->createForm(ScriptMappingType::class, $mapping);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$token = (string) $form->get('accessToken')->getData();
|
|
$mapping->setAccessToken($token);
|
|
|
|
$this->entityManager->persist($mapping);
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Mapping créé.');
|
|
|
|
return $this->redirectToRoute('admin_dashboard');
|
|
}
|
|
|
|
return $this->render('admin/mapping_form.html.twig', [
|
|
'form' => $form,
|
|
'mapping' => $mapping,
|
|
'title' => 'Nouveau mapping',
|
|
], new Response(status: $form->isSubmitted() ? Response::HTTP_UNPROCESSABLE_ENTITY : Response::HTTP_OK));
|
|
}
|
|
|
|
#[Route('/{id}/edit', name: 'admin_mapping_edit', methods: ['GET', 'POST'])]
|
|
public function edit(Request $request, ScriptMapping $mapping): Response
|
|
{
|
|
$form = $this->createForm(ScriptMappingType::class, $mapping);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$token = (string) $form->get('accessToken')->getData();
|
|
if ($token !== '') {
|
|
$mapping->setAccessToken($token);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Mapping mis à jour.');
|
|
|
|
return $this->redirectToRoute('admin_dashboard');
|
|
}
|
|
|
|
return $this->render('admin/mapping_form.html.twig', [
|
|
'form' => $form,
|
|
'mapping' => $mapping,
|
|
'title' => 'Modifier le mapping',
|
|
], new Response(status: $form->isSubmitted() ? Response::HTTP_UNPROCESSABLE_ENTITY : Response::HTTP_OK));
|
|
}
|
|
|
|
#[Route('/{id}/delete', name: 'admin_mapping_delete', methods: ['POST'])]
|
|
public function delete(Request $request, ScriptMapping $mapping): Response
|
|
{
|
|
if (!$this->isCsrfTokenValid('delete_mapping_'.$mapping->getId(), (string) $request->request->get('_token'))) {
|
|
throw $this->createAccessDeniedException();
|
|
}
|
|
|
|
$this->entityManager->remove($mapping);
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Mapping supprimé.');
|
|
|
|
return $this->redirectToRoute('admin_dashboard');
|
|
}
|
|
|
|
#[Route('/{id}/sync', name: 'admin_mapping_sync', methods: ['POST'])]
|
|
public function sync(Request $request, ScriptMapping $mapping, GitSynchronizer $synchronizer): Response
|
|
{
|
|
if (!$this->isCsrfTokenValid('sync_mapping_'.$mapping->getId(), (string) $request->request->get('_token'))) {
|
|
throw $this->createAccessDeniedException();
|
|
}
|
|
|
|
try {
|
|
$synchronizer->sync($mapping);
|
|
$this->addFlash('success', 'Mapping synchronisé.');
|
|
} catch (GitSyncFailedException) {
|
|
$this->addFlash('error', 'La synchronisation du mapping a échoué.');
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
return $this->redirectToRoute('admin_dashboard');
|
|
}
|
|
}
|