move controller service logics into a service
This commit is contained in:
90
src/Service/Admin/TagAdminService.php
Normal file
90
src/Service/Admin/TagAdminService.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user