stash light
This commit is contained in:
123
src/Service/DocumentService.php
Normal file
123
src/Service/DocumentService.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Document;
|
||||
use App\Entity\DocumentVersion;
|
||||
use App\Entity\User;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class DocumentService
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Erstellt ein neues Dokument inkl. Version 1
|
||||
*/
|
||||
public function createDocument(
|
||||
string $title,
|
||||
string $filePath,
|
||||
User $user
|
||||
): Document {
|
||||
|
||||
$document = new Document();
|
||||
$document->setTitle($title);
|
||||
$document->setCreatedBy($user);
|
||||
|
||||
$version = new DocumentVersion();
|
||||
$version->setVersionNumber(1);
|
||||
$version->setFilePath($filePath);
|
||||
$version->setChecksum($this->calculateChecksum($filePath));
|
||||
$version->setCreatedBy($user);
|
||||
$version->setActive(true);
|
||||
|
||||
$document->addVersion($version);
|
||||
$document->setCurrentVersion($version);
|
||||
|
||||
$this->em->persist($document);
|
||||
$this->em->persist($version);
|
||||
$this->em->flush();
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt neue Version hinzu (immutable)
|
||||
*/
|
||||
public function addVersion(
|
||||
Document $document,
|
||||
string $filePath,
|
||||
User $user
|
||||
): DocumentVersion {
|
||||
|
||||
$nextVersionNumber = $this->getNextVersionNumber($document);
|
||||
|
||||
$version = new DocumentVersion();
|
||||
$version->setVersionNumber($nextVersionNumber);
|
||||
$version->setFilePath($filePath);
|
||||
$version->setChecksum($this->calculateChecksum($filePath));
|
||||
$version->setCreatedBy($user);
|
||||
$version->setActive(false);
|
||||
|
||||
$document->addVersion($version);
|
||||
|
||||
$this->em->persist($version);
|
||||
$this->em->flush();
|
||||
|
||||
return $version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktiviert eine Version (setzt andere inaktiv)
|
||||
*/
|
||||
public function activateVersion(DocumentVersion $version): void
|
||||
{
|
||||
$document = $version->getDocument();
|
||||
|
||||
foreach ($document->getVersions() as $existingVersion) {
|
||||
$existingVersion->setActive(false);
|
||||
}
|
||||
|
||||
$version->setActive(true);
|
||||
$document->setCurrentVersion($version);
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Archiviert Dokument
|
||||
*/
|
||||
public function archive(Document $document): void
|
||||
{
|
||||
$document->archive();
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet SHA256 Checksum
|
||||
*/
|
||||
private function calculateChecksum(string $filePath): string
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new \RuntimeException('File not found for checksum.');
|
||||
}
|
||||
|
||||
return hash_file('sha256', $filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittelt nächste Versionsnummer
|
||||
*/
|
||||
private function getNextVersionNumber(Document $document): int
|
||||
{
|
||||
$max = 0;
|
||||
|
||||
foreach ($document->getVersions() as $version) {
|
||||
$max = max($max, $version->getVersionNumber());
|
||||
}
|
||||
|
||||
return $max + 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user