move controller service logics into a service
This commit is contained in:
72
src/Service/Admin/DocumentTagAdminService.php
Normal file
72
src/Service/Admin/DocumentTagAdminService.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin;
|
||||
|
||||
use App\Entity\Document;
|
||||
use App\Entity\Tag;
|
||||
use App\Service\TagRebuildJobService;
|
||||
use App\Tag\TagService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
final class DocumentTagAdminService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly TagService $tagService,
|
||||
private readonly TagRebuildJobService $jobs,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* document: Document,
|
||||
* allTags: list<Tag>,
|
||||
* latestJob: mixed
|
||||
* }
|
||||
*/
|
||||
public function getEditData(string $documentId): array
|
||||
{
|
||||
$document = $this->em->getRepository(Document::class)->find($documentId);
|
||||
if (!$document instanceof Document) {
|
||||
throw new \RuntimeException('Document not found');
|
||||
}
|
||||
|
||||
/** @var list<Tag> $allTags */
|
||||
$allTags = $this->em->createQueryBuilder()
|
||||
->select('t')
|
||||
->from(Tag::class, 't')
|
||||
->orderBy('t.label', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
$latestJob = $this->jobs->getLatestJob();
|
||||
|
||||
return [
|
||||
'document' => $document,
|
||||
'allTags' => $allTags,
|
||||
'latestJob' => $latestJob,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Speichert die Tag-Auswahl für ein Dokument (inkl. Sync-Logik).
|
||||
*/
|
||||
public function saveTags(string $documentId, array $selectedTagIds): void
|
||||
{
|
||||
$document = $this->em->getRepository(Document::class)->find($documentId);
|
||||
if (!$document instanceof Document) {
|
||||
throw new \RuntimeException('Document not found');
|
||||
}
|
||||
|
||||
// Delegation an deine Domain-Logik (bleibt dort, wo sie hingehört)
|
||||
$this->tagService->syncDocumentTags($document, $selectedTagIds);
|
||||
}
|
||||
|
||||
public function getLatestRebuildStatus(): ?string
|
||||
{
|
||||
$job = $this->jobs->getLatestJob();
|
||||
|
||||
return $job?->getStatus();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user