Start on property keeping track of git db content

This commit is contained in:
Tim
2023-12-16 01:26:47 +01:00
parent a9e740d018
commit 0fce8ee4fe
7 changed files with 219 additions and 27 deletions

View File

@ -27,6 +27,9 @@ class Snip
#[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)]
private Collection $snipContents;
#[ORM\Column(length: 255, nullable: true)]
private ?string $activeCommit = null;
public function __construct()
{
$this->snipContents = new ArrayCollection();
@ -95,4 +98,16 @@ class Snip
return $this;
}
public function getActiveCommit(): ?string
{
return $this->activeCommit;
}
public function setActiveCommit(?string $activeCommit): static
{
$this->activeCommit = $activeCommit;
return $this;
}
}

View File

@ -7,6 +7,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Ulid;
#[ORM\Entity(repositoryClass: SnipContentRepository::class)]
class SnipContent
@ -14,27 +15,27 @@ class SnipContent
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
private ?Ulid $id = null;
#[ORM\ManyToOne(inversedBy: 'snipContents')]
#[ORM\JoinColumn(nullable: false)]
private ?Snip $snip = null;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'child')]
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
private ?self $parent = null;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
private Collection $child;
private Collection $children;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $text = null;
public function __construct()
{
$this->child = new ArrayCollection();
$this->children = new ArrayCollection();
}
public function getId(): ?int
public function getId(): ?Ulid
{
return $this->id;
}
@ -66,15 +67,15 @@ class SnipContent
/**
* @return Collection<int, self>
*/
public function getChild(): Collection
public function getChildren(): Collection
{
return $this->child;
return $this->children;
}
public function addChild(self $child): self
{
if (!$this->child->contains($child)) {
$this->child->add($child);
if (!$this->children->contains($child)) {
$this->children->add($child);
$child->setParent($this);
}
@ -83,7 +84,7 @@ class SnipContent
public function removeChild(self $child): self
{
if ($this->child->removeElement($child)) {
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);

View File

@ -5,17 +5,15 @@ namespace App\Service\SnipContent;
use App\Entity\Snip;
use App\Entity\SnipContent;
use App\Entity\User;
use App\Repository\SnipContentRepository;
use Doctrine\ORM\EntityManagerInterface;
class SnipContentDB implements SnipContentInterface
readonly class SnipContentDB implements SnipContentInterface
{
public function __construct(
private readonly Snip $snip,
private readonly User $user,
private readonly SnipContentRepository $repo,
)
{
}
private Snip $snip,
private User $user,
private EntityManagerInterface $em,
) {}
public function update(string $snipContents): void
{
@ -27,7 +25,12 @@ class SnipContentDB implements SnipContentInterface
$content->setParent($this->snip->getSnipContents()->last());
}
$this->repo->save($content);
$this->em->persist($content);
$this->em->flush();
$this->snip->setActiveCommit($content->getId());
$this->em->persist($this->snip);
$this->em->flush();
}
public function get(): string
@ -44,14 +47,14 @@ class SnipContentDB implements SnipContentInterface
public function setCommit(string $commit): void
{
// return to previous history commit
// maybe store the current commit in the snip content
$this->snip->setActiveCommit($commit);
$this->em->persist($this->snip);
$this->em->flush();
}
public function getCommit(): string
{
// return the current commit
return '';
return $this->snip->getActiveCommit();
}
public function delete(): void