first commit
This commit is contained in:
@@ -1,29 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Document;
|
||||
use App\Entity\DocumentVersion;
|
||||
use App\Entity\User;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use RuntimeException;
|
||||
|
||||
class DocumentService
|
||||
final readonly class DocumentService
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
) {}
|
||||
private TagRebuildJobService $tagRebuildJobService,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt ein neues Dokument inkl. Version 1
|
||||
* Creates a new document including version 1.
|
||||
*/
|
||||
public function createDocument(
|
||||
string $title,
|
||||
string $filePath,
|
||||
User $user
|
||||
): Document {
|
||||
|
||||
$document = new Document();
|
||||
$document->setTitle($title);
|
||||
$document->setTitle(trim($title));
|
||||
$document->setCreatedBy($user);
|
||||
|
||||
$version = new DocumentVersion();
|
||||
@@ -44,14 +48,13 @@ class DocumentService
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt neue Version hinzu (immutable)
|
||||
* Adds a new immutable version to an existing document.
|
||||
*/
|
||||
public function addVersion(
|
||||
Document $document,
|
||||
string $filePath,
|
||||
User $user
|
||||
): DocumentVersion {
|
||||
|
||||
$nextVersionNumber = $this->getNextVersionNumber($document);
|
||||
|
||||
$version = new DocumentVersion();
|
||||
@@ -70,7 +73,7 @@ class DocumentService
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktiviert eine Version
|
||||
* Activates a document version and marks it for re-ingest.
|
||||
*/
|
||||
public function activateVersion(DocumentVersion $version): void
|
||||
{
|
||||
@@ -82,41 +85,77 @@ class DocumentService
|
||||
|
||||
$version->setActive(true);
|
||||
$document->setCurrentVersion($version);
|
||||
|
||||
$version->setIngestStatus(DocumentVersion::INGEST_PENDING);
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Archiviert Dokument
|
||||
* Archives a document.
|
||||
*
|
||||
* If the document had tag assignments, the tag index is rebuilt so the
|
||||
* routing layer no longer works with an outdated active document set.
|
||||
*/
|
||||
public function archive(Document $document): void
|
||||
{
|
||||
if ($document->getStatus() === Document::STATUS_ARCHIVED) {
|
||||
return;
|
||||
}
|
||||
|
||||
$shouldRebuildTags = $this->hasTagAssignments($document);
|
||||
|
||||
$document->archive();
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
public function delete(Document $document): void
|
||||
{
|
||||
$this->em->remove($document);
|
||||
$this->em->flush();
|
||||
if ($shouldRebuildTags) {
|
||||
$this->triggerTagRebuildIfIdle();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet SHA256 Checksum
|
||||
* Deletes a document.
|
||||
*
|
||||
* If the document had tag assignments, the tag index is rebuilt after the
|
||||
* removal so stale document references disappear from tag-based routing.
|
||||
*/
|
||||
public function delete(Document $document): void
|
||||
{
|
||||
$shouldRebuildTags = $this->hasTagAssignments($document);
|
||||
|
||||
$this->em->remove($document);
|
||||
$this->em->flush();
|
||||
|
||||
if ($shouldRebuildTags) {
|
||||
$this->triggerTagRebuildIfIdle();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the SHA256 checksum for a file path.
|
||||
*/
|
||||
private function calculateChecksum(string $filePath): string
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new \RuntimeException('File not found for checksum.');
|
||||
$filePath = trim($filePath);
|
||||
|
||||
if ($filePath === '') {
|
||||
throw new RuntimeException('File path must not be empty.');
|
||||
}
|
||||
|
||||
return hash_file('sha256', $filePath);
|
||||
if (!is_file($filePath)) {
|
||||
throw new RuntimeException('File not found for checksum.');
|
||||
}
|
||||
|
||||
$checksum = hash_file('sha256', $filePath);
|
||||
|
||||
if ($checksum === false) {
|
||||
throw new RuntimeException('Could not calculate file checksum.');
|
||||
}
|
||||
|
||||
return $checksum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittelt nächste Versionsnummer
|
||||
* Determines the next version number for a document.
|
||||
*/
|
||||
private function getNextVersionNumber(Document $document): int
|
||||
{
|
||||
@@ -128,4 +167,16 @@ class DocumentService
|
||||
|
||||
return $max + 1;
|
||||
}
|
||||
}
|
||||
|
||||
private function hasTagAssignments(Document $document): bool
|
||||
{
|
||||
return $document->getDocumentTags()->count() > 0;
|
||||
}
|
||||
|
||||
private function triggerTagRebuildIfIdle(): void
|
||||
{
|
||||
if (!$this->tagRebuildJobService->hasActiveJob()) {
|
||||
$this->tagRebuildJobService->enqueueAndStartAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user