Start on creating snipContent entities for the db content

This commit is contained in:
Tim
2023-04-20 23:23:04 +02:00
parent a405578f93
commit 506a0e8dec
11 changed files with 323 additions and 14 deletions

View File

@ -2,12 +2,60 @@
namespace App\Service\SnipContent;
use App\Entity\Snip;
use App\Entity\SnipContent;
use App\Entity\User;
use App\Repository\SnipContentRepository;
class SnipContentDB implements SnipContentInterface
{
public function __construct()
public function __construct(
private readonly Snip $snip,
private readonly User $user,
private readonly SnipContentRepository $repo,
)
{
}
public function update(string $snipContents): void
{
// Create new snipContent entity with previous one as parent
$content = new SnipContent();
$content->setText($snipContents);
$content->setSnip($this->snip);
if ($this->snip->getSnipContents()->count() > 0) {
$content->setParent($this->snip->getSnipContents()->last());
}
$this->repo->save($content);
}
public function get(): string
{
// Return the content of the latest snipContent entity
return $this->snip->getSnipContents()->last()->getText();
}
public function getHistory(): array
{
// Return all snipContent entities (by parent)
return array_map(fn(SnipContent $content) => $content->getId(), $this->snip->getSnipContents()->toArray());
}
public function setCommit(string $commit): void
{
// return to previous history commit
// maybe store the current commit in the snip content
}
public function getCommit(): string
{
// return the current commit
return '';
}
public function delete(): void
{
// Cleanup the history
}
}

View File

@ -51,6 +51,9 @@ class SnipContentGit implements SnipContentInterface
return file_get_contents($this->getSnipPath());
}
/**
* @return array<\App\Git\SimpleCommit>
*/
public function getHistory(): array
{
return $this->repo->getAllCommits();

View File

@ -4,6 +4,8 @@ namespace App\Service;
use App\Entity\Snip;
use App\Git\CustomGit;
use App\Repository\SnipContentRepository;
use App\Service\SnipContent\SnipContentDB;
use App\Service\SnipContent\SnipContentGit;
use Symfony\Bundle\SecurityBundle\Security;
@ -11,13 +13,14 @@ class SnipServiceFactory
{
public function __construct(
private readonly string $snipBasePath,
private readonly Security $security,
private readonly string $snipBasePath,
private readonly Security $security,
private readonly SnipContentRepository $snipContentRepository,
)
{
}
public function create(Snip $snip): SnipContentGit
public function createGit(Snip $snip): SnipContentGit
{
$git = new CustomGit();
$repoPath = sprintf('%s/%s', $this->snipBasePath, $snip->getId());
@ -31,4 +34,9 @@ class SnipServiceFactory
}
return new SnipContentGit($repo, $this->security->getUser());
}
public function createDB(Snip $snip): SnipContentDB
{
return new SnipContentDB($snip, $this->security->getUser(), $this->snipContentRepository);
}
}