Integrate snipContent more tightly into snip and cleanup old code

This commit is contained in:
Tim
2023-12-20 22:37:06 +01:00
parent 64bd7e3642
commit 5624fc3a74
9 changed files with 67 additions and 45 deletions

View File

@ -53,8 +53,6 @@ class SnipController extends AbstractController
return $this->render('snip/single.html.twig', [
'snip' => $snip,
'content' => $pl->parse($snipService->getActiveText()),
'activeVersion' => $snipService->getActiveVersion(),
'latestVersion' => $snipService->getLatestVersion(),
]);
}
@ -114,7 +112,7 @@ class SnipController extends AbstractController
public function new(Request $request): Response
{
$snip = new Snip();
$snip->setCreatedAtTodayNoSeconds()
$snip->setCreatedAtNow()
->setCreatedBy($this->getUser());
return $this->edit($snip, $request);

View File

@ -21,12 +21,9 @@ class VersionController extends AbstractController
public function index(Snip $snip): Response
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$snipService = $this->snipServiceFactory->create($snip);
return $this->render('version/index.html.twig', [
'snip' => $snip,
'versions' => $snipService->getVersions(),
'latestVersion' => $snipService->getLatestVersion(),
]);
}
@ -35,7 +32,7 @@ class VersionController extends AbstractController
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$this->snipServiceFactory->create($snip)->setVersion($version->getId());
$this->snipServiceFactory->create($snip)->setVersion($version);
$this->addFlash('success', 'Snip version set to ' . $version->getId());
return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]);
}

View File

@ -40,10 +40,17 @@ trait TrackedTrait
return $this;
}
public function setCreatedAtTodayNoSeconds(): self
public function setCreatedAtNowNoSeconds(): self
{
$this->setCreatedAt(DateTime::createFromFormat('Y-m-d H:i', date('Y-m-d H:i')));
return $this;
}
public function setCreatedAtNow(): self
{
$this->setCreatedAt(new DateTime());
return $this;
}
}

View File

@ -27,8 +27,8 @@ class Snip
#[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)]
private Collection $snipContents;
#[ORM\Column(length: 255, nullable: true)]
private ?string $activeCommit = null;
#[ORM\ManyToOne]
private ?SnipContent $activeVersion = null;
public function __construct()
{
@ -99,14 +99,19 @@ class Snip
return $this;
}
public function getActiveCommit(): ?string
public function getLatestVersion(): ?SnipContent
{
return $this->activeCommit;
return $this->snipContents->last();
}
public function setActiveCommit(?string $activeCommit): static
public function getActiveVersion(): ?SnipContent
{
$this->activeCommit = $activeCommit;
return $this->activeVersion;
}
public function setActiveVersion(?SnipContent $activeVersion): static
{
$this->activeVersion = $activeVersion;
return $this;
}

View File

@ -28,7 +28,7 @@ readonly class SnipContentService
$this->em->persist($content);
$this->em->flush();
$this->snip->setActiveCommit($content->getId());
$this->snip->setActiveVersion($content->getId());
$this->em->persist($this->snip);
$this->em->flush();
}
@ -37,38 +37,18 @@ readonly class SnipContentService
public function getActiveText(): string
{
$contentRepo = $this->em->getRepository(SnipContent::class);
return $contentRepo->find($this->snip->getActiveCommit())->getText();
return $contentRepo->find($this->snip->getActiveVersion())->getText();
}
/** @return array{id: string, name: string} */
public function getVersions(): array
public function setVersion(SnipContent $version): void
{
// 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->snip->setActiveVersion($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();
}
}

View File

@ -41,7 +41,7 @@ class IncludeReferenceStage implements StageInterface
} else {
$snip = $this->snipRepository->find($id);
if ($snip) {
$content = $this->snipContentRepository->find($snip->getActiveCommit());
$content = $this->snipContentRepository->find($snip->getActiveVersion());
}
}
if ($content === null) {