Snips/src/Service/SnipContent/SnipContentService.php

54 lines
1.4 KiB
PHP

<?php
namespace App\Service\SnipContent;
use App\Entity\Snip;
use App\Entity\SnipContent;
use Doctrine\ORM\EntityManagerInterface;
readonly class SnipContentService
{
public function __construct(
private Snip $snip,
private EntityManagerInterface $em,
) {}
public function update(string $snipContents): void
{
// Create new snipContent entity with previous one as parent
$content = new SnipContent();
$content
->setText($snipContents)
->setSnip($this->snip)
;
if ($this->snip->getSnipContents()->count() > 0) {
$content->setParent($this->snip->getSnipContents()->last());
}
$this->em->persist($content);
$this->em->flush();
$this->snip->setActiveVersion($content->getId());
$this->em->persist($this->snip);
$this->em->flush();
}
// Shortcut to get the active text
public function getActiveText(): string
{
$contentRepo = $this->em->getRepository(SnipContent::class);
return $contentRepo->find($this->snip->getActiveVersion())->getText();
}
public function setVersion(SnipContent $version): void
{
$this->snip->setActiveVersion($version);
$this->em->persist($this->snip);
$this->em->flush();
}
public function delete(): void
{
// Cleanup the versions
}
}