Start on removing git and better integrate db
This commit is contained in:
74
src/Service/SnipContent/SnipContentService.php
Normal file
74
src/Service/SnipContent/SnipContentService.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?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->setActiveCommit($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->getActiveCommit())->getText();
|
||||
}
|
||||
|
||||
/** @return array{id: string, name: string} */
|
||||
public function getVersions(): array
|
||||
{
|
||||
// Return all snipContent entities (by parent)
|
||||
return array_map(fn(SnipContent $content) => [
|
||||
'id' => (string)$content->getId(),
|
||||
'name' => $content->getId()->getDateTime()->format('Y-m-d H:i:s'),
|
||||
], $this->snip->getSnipContents()->toArray());
|
||||
}
|
||||
|
||||
public function setVersion(string $version): void
|
||||
{
|
||||
$this->snip->setActiveCommit($version);
|
||||
$this->em->persist($this->snip);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
public function getActiveVersion(): string
|
||||
{
|
||||
return $this->snip->getActiveCommit();
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
// Cleanup the versions
|
||||
}
|
||||
|
||||
public function getLatestVersion(): string
|
||||
{
|
||||
return $this->snip->getSnipContents()->last()->getId();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user