Start with snips content abstraction

This commit is contained in:
Tim 2023-04-15 13:20:07 +02:00
parent 980ea6af94
commit a405578f93
6 changed files with 53 additions and 11 deletions

View File

@ -25,7 +25,7 @@ class HistoryController extends AbstractController
return $this->render('history/index.html.twig', [ return $this->render('history/index.html.twig', [
'snip' => $snip, 'snip' => $snip,
'commits' => $this->snipServiceFactory->create($snip)->getRepo()->getAllCommits(), 'commits' => $this->snipServiceFactory->create($snip)->getHistory(),
]); ]);
} }
@ -34,7 +34,7 @@ class HistoryController extends AbstractController
{ {
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$this->snipServiceFactory->create($snip)->getRepo()->checkout($commit); $this->snipServiceFactory->create($snip)->setCommit($commit);
$this->addFlash('success', 'Snip version set to ' . $commit); $this->addFlash('success', 'Snip version set to ' . $commit);
return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]); return $this->redirectToRoute('snip_single', ['snip' => $snip->getId()]);
} }

View File

@ -52,7 +52,7 @@ class SnipController extends AbstractController
return $this->render('snip/single.html.twig', [ return $this->render('snip/single.html.twig', [
'snip' => $snip, 'snip' => $snip,
'content' => (new Pipeline())->parse($snipService->get()), 'content' => (new Pipeline())->parse($snipService->get()),
'branch' => $snipService->getRepo()->getCurrentBranchName(), 'branch' => $snipService->getCommit(),
]); ]);
} }
@ -130,7 +130,7 @@ class SnipController extends AbstractController
$form = $this->createForm(ConfirmationType::class); $form = $this->createForm(ConfirmationType::class);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$this->snipServiceFactory->create($snip)->deleteRepo(); $this->snipServiceFactory->create($snip)->delete();
$this->repository->remove($snip); $this->repository->remove($snip);
$this->addFlash('success', sprintf('Snip "%s" deleted', $snip)); $this->addFlash('success', sprintf('Snip "%s" deleted', $snip));
return $this->redirectToRoute('snip_index'); return $this->redirectToRoute('snip_index');

View File

@ -0,0 +1,13 @@
<?php
namespace App\Service\SnipContent;
class SnipContentDB implements SnipContentInterface
{
public function __construct()
{
}
}

View File

@ -1,12 +1,12 @@
<?php <?php
namespace App\Service; namespace App\Service\SnipContent;
use App\Entity\User; use App\Entity\User;
use App\Git\CustomGitRepository; use App\Git\CustomGitRepository;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
class SnipService class SnipContentGit implements SnipContentInterface
{ {
private const SNIP_FILE_NAME = 'snip.txt'; private const SNIP_FILE_NAME = 'snip.txt';
private const MASTER_BRANCH_NAME = 'master'; private const MASTER_BRANCH_NAME = 'master';
@ -51,12 +51,22 @@ class SnipService
return file_get_contents($this->getSnipPath()); return file_get_contents($this->getSnipPath());
} }
public function getRepo(): CustomGitRepository public function getHistory(): array
{ {
return $this->repo; return $this->repo->getAllCommits();
} }
public function deleteRepo(): void public function setCommit(string $commit): void
{
$this->repo->checkout($commit);
}
public function getCommit(): string
{
return $this->repo->getCurrentBranchName();
}
public function delete(): void
{ {
system("rm -rf " . escapeshellarg($this->repo->getRepositoryPath())); system("rm -rf " . escapeshellarg($this->repo->getRepositoryPath()));
} }

View File

@ -0,0 +1,18 @@
<?php
namespace App\Service\SnipContent;
interface SnipContentInterface
{
public function update(string $snipContents): void;
public function get(): string;
public function getHistory(): array;
public function setCommit(string $commit): void;
public function getCommit(): string;
public function delete(): void;
}

View File

@ -4,6 +4,7 @@ namespace App\Service;
use App\Entity\Snip; use App\Entity\Snip;
use App\Git\CustomGit; use App\Git\CustomGit;
use App\Service\SnipContent\SnipContentGit;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
class SnipServiceFactory class SnipServiceFactory
@ -16,7 +17,7 @@ class SnipServiceFactory
{ {
} }
public function create(Snip $snip): SnipService public function create(Snip $snip): SnipContentGit
{ {
$git = new CustomGit(); $git = new CustomGit();
$repoPath = sprintf('%s/%s', $this->snipBasePath, $snip->getId()); $repoPath = sprintf('%s/%s', $this->snipBasePath, $snip->getId());
@ -28,6 +29,6 @@ class SnipServiceFactory
} else { } else {
$repo = $git->open($repoPath); $repo = $git->open($repoPath);
} }
return new SnipService($repo, $this->security->getUser()); return new SnipContentGit($repo, $this->security->getUser());
} }
} }