move controller service logics into a service

This commit is contained in:
team2
2026-02-27 20:24:58 +01:00
parent 3a1b30f1ea
commit 66e16a4ae9
2 changed files with 124 additions and 79 deletions

View File

@@ -4,13 +4,8 @@ declare(strict_types=1);
namespace App\Controller\Admin; namespace App\Controller\Admin;
use App\Entity\Document;
use App\Entity\DocumentTag;
use App\Entity\Tag;
use App\Entity\TagRebuildJob; use App\Entity\TagRebuildJob;
use App\Tag\TagService; use App\Service\Admin\TagAdminService;
use App\Service\TagRebuildJobService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@@ -21,18 +16,12 @@ use Symfony\Component\Routing\Attribute\Route;
final class TagController extends AbstractController final class TagController extends AbstractController
{ {
#[Route('', name: 'admin_tags_index', methods: ['GET'])] #[Route('', name: 'admin_tags_index', methods: ['GET'])]
public function index( public function index(TagAdminService $svc): Response
EntityManagerInterface $em, {
TagRebuildJobService $jobs $data = $svc->getIndexData();
): Response {
$tags = $em->getRepository(Tag::class)
->findBy([], ['label' => 'ASC']);
return $this->render('admin/tag/index.html.twig', [ return $this->render('admin/tag/index.html.twig', [
'tags' => $tags, ...$data,
'latestJob' => $jobs->getLatestJob(),
'hasActiveJob' => $jobs->hasActiveJob(),
'statusRunning' => TagRebuildJob::STATUS_RUNNING, 'statusRunning' => TagRebuildJob::STATUS_RUNNING,
'statusQueued' => TagRebuildJob::STATUS_QUEUED, 'statusQueued' => TagRebuildJob::STATUS_QUEUED,
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED, 'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
@@ -41,19 +30,18 @@ final class TagController extends AbstractController
} }
#[Route('/create', name: 'admin_tags_create', methods: ['POST'])] #[Route('/create', name: 'admin_tags_create', methods: ['POST'])]
public function create( public function create(Request $request, TagAdminService $svc): RedirectResponse
Request $request, {
TagService $tagService if (!$this->isCsrfTokenValid(
): RedirectResponse { 'admin_tag_create',
$token = (string) $request->request->get('_token', ''); $request->request->get('_token')
)) {
if (!$this->isCsrfTokenValid('admin_tag_create', $token)) {
$this->addFlash('danger', 'Ungültiges CSRF Token.'); $this->addFlash('danger', 'Ungültiges CSRF Token.');
return $this->redirectToRoute('admin_tags_index'); return $this->redirectToRoute('admin_tags_index');
} }
try { try {
$tagService->create( $svc->create(
(string)$request->request->get('slug', ''), (string)$request->request->get('slug', ''),
(string)$request->request->get('label', ''), (string)$request->request->get('label', ''),
$request->request->get('description') $request->request->get('description')
@@ -61,7 +49,7 @@ final class TagController extends AbstractController
: null : null
); );
$this->addFlash('success', 'Tag wurde erstellt. Rebuild läuft im Hintergrund.'); $this->addFlash('success', 'Tag wurde erstellt.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage()); $this->addFlash('danger', $e->getMessage());
} }
@@ -70,21 +58,19 @@ final class TagController extends AbstractController
} }
#[Route('/{id}/delete', name: 'admin_tags_delete', methods: ['POST'])] #[Route('/{id}/delete', name: 'admin_tags_delete', methods: ['POST'])]
public function delete( public function delete(string $id, Request $request, TagAdminService $svc): RedirectResponse
string $id, {
Request $request, if (!$this->isCsrfTokenValid(
TagService $tagService 'admin_tag_delete_' . $id,
): RedirectResponse { $request->request->get('_token')
$token = (string) $request->request->get('_token', ''); )) {
if (!$this->isCsrfTokenValid('admin_tag_delete_' . $id, $token)) {
$this->addFlash('danger', 'Ungültiges CSRF Token.'); $this->addFlash('danger', 'Ungültiges CSRF Token.');
return $this->redirectToRoute('admin_tags_index'); return $this->redirectToRoute('admin_tags_index');
} }
try { try {
$tagService->deleteById($id); $svc->delete($id);
$this->addFlash('success', 'Tag wurde gelöscht. Rebuild läuft im Hintergrund.'); $this->addFlash('success', 'Tag wurde gelöscht.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage()); $this->addFlash('danger', $e->getMessage());
} }
@@ -95,64 +81,33 @@ final class TagController extends AbstractController
#[Route('/{id}/assign', name: 'admin_tags_assign', methods: ['GET', 'POST'])] #[Route('/{id}/assign', name: 'admin_tags_assign', methods: ['GET', 'POST'])]
public function assign( public function assign(
string $id, string $id,
EntityManagerInterface $em,
Request $request, Request $request,
TagService $tagService, TagAdminService $svc
TagRebuildJobService $jobs
): Response { ): Response {
$tag = $em->getRepository(Tag::class)->find($id);
if (!$tag instanceof Tag) {
throw $this->createNotFoundException('Tag nicht gefunden.');
}
// Alle Dokumente laden
$documents = $em->getRepository(Document::class)->findAll();
$documentsData = array_map(
fn(Document $d) => [
'id' => (string) $d->getId(),
'title' => $d->getTitle(),
],
$documents
);
// Aktuell zugewiesene Dokumente ermitteln
$existingRelations = $em
->getRepository(DocumentTag::class)
->findBy(['tag' => $tag]);
$assignedDocIds = array_map(
fn(DocumentTag $dt) => (string) $dt->getDocument()->getId(),
$existingRelations
);
if ($request->isMethod('POST')) { if ($request->isMethod('POST')) {
if (!$this->isCsrfTokenValid( if (!$this->isCsrfTokenValid(
'assign_tag_' . $tag->getId(), 'assign_tag_' . $id,
$request->request->get('_token') $request->request->get('_token')
)) { )) {
throw $this->createAccessDeniedException(); throw $this->createAccessDeniedException();
} }
$selectedIds = $request->request->all('documents') ?? []; $svc->syncAssignments(
$id,
$tagService->syncTagDocuments($tag, $selectedIds); $request->request->all('documents') ?? []
);
$this->addFlash('success', 'Zuweisungen aktualisiert.'); $this->addFlash('success', 'Zuweisungen aktualisiert.');
return $this->redirectToRoute('admin_tags_assign', [ return $this->redirectToRoute('admin_tags_assign', ['id' => $id]);
'id' => $tag->getId()
]);
} }
$data = $svc->getAssignData($id);
return $this->render('admin/tag/assign.html.twig', [ return $this->render('admin/tag/assign.html.twig', [
'tag' => $tag, ...$data,
'documents' => $documentsData,
'assignedDocIds' => $assignedDocIds,
'latestJob' => $jobs->getLatestJob(),
'hasActiveJob' => $jobs->hasActiveJob(),
'statusRunning' => TagRebuildJob::STATUS_RUNNING, 'statusRunning' => TagRebuildJob::STATUS_RUNNING,
'statusQueued' => TagRebuildJob::STATUS_QUEUED, 'statusQueued' => TagRebuildJob::STATUS_QUEUED,
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED, 'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,

View File

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace App\Service\Admin;
use App\Entity\Document;
use App\Entity\DocumentTag;
use App\Entity\Tag;
use App\Service\TagRebuildJobService;
use App\Tag\TagService;
use Doctrine\ORM\EntityManagerInterface;
final class TagAdminService
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly TagService $tagService,
private readonly TagRebuildJobService $jobs,
) {}
public function getIndexData(): array
{
$tags = $this->em->getRepository(Tag::class)
->findBy([], ['label' => 'ASC']);
return [
'tags' => $tags,
'latestJob' => $this->jobs->getLatestJob(),
'hasActiveJob' => $this->jobs->hasActiveJob(),
];
}
public function create(string $slug, string $label, ?string $description): void
{
$this->tagService->create($slug, $label, $description);
}
public function delete(string $id): void
{
$this->tagService->deleteById($id);
}
public function getAssignData(string $tagId): array
{
$tag = $this->em->getRepository(Tag::class)->find($tagId);
if (!$tag instanceof Tag) {
throw new \RuntimeException('Tag nicht gefunden.');
}
$documents = $this->em->getRepository(Document::class)->findAll();
$documentsData = array_map(
fn(Document $d) => [
'id' => (string)$d->getId(),
'title' => $d->getTitle(),
],
$documents
);
$existingRelations = $this->em
->getRepository(DocumentTag::class)
->findBy(['tag' => $tag]);
$assignedDocIds = array_map(
fn(DocumentTag $dt) => (string)$dt->getDocument()->getId(),
$existingRelations
);
return [
'tag' => $tag,
'documents' => $documentsData,
'assignedDocIds' => $assignedDocIds,
'latestJob' => $this->jobs->getLatestJob(),
'hasActiveJob' => $this->jobs->hasActiveJob(),
];
}
public function syncAssignments(string $tagId, array $selectedDocIds): void
{
$tag = $this->em->getRepository(Tag::class)->find($tagId);
if (!$tag instanceof Tag) {
throw new \RuntimeException('Tag nicht gefunden.');
}
$this->tagService->syncTagDocuments($tag, $selectedDocIds);
}
}