From b2bc519e263498c4e2165c387ddff78fdf987e08 Mon Sep 17 00:00:00 2001 From: tim Date: Sun, 24 Dec 2023 01:06:30 +0100 Subject: [PATCH] Dont make new version if content didnt change --- src/Service/SnipContent/SnipContentService.php | 7 +++++-- src/Service/SnipParser/Stages/ReplaceStage.php | 6 +++--- src/Service/SnipParser/Stages/UrlReferenceStage.php | 8 ++++---- src/Service/SnipServiceFactory.php | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Service/SnipContent/SnipContentService.php b/src/Service/SnipContent/SnipContentService.php index 4528a04..4291d63 100644 --- a/src/Service/SnipContent/SnipContentService.php +++ b/src/Service/SnipContent/SnipContentService.php @@ -15,14 +15,17 @@ readonly class SnipContentService public function update(string $snipContents): void { + if ($this->snip->getActiveVersion()?->getText() === $snipContents) { + return; + } // 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()); + if ($this->snip->getActiveVersion() !== null) { + $content->setParent($this->snip->getActiveVersion()); } $this->em->persist($content); diff --git a/src/Service/SnipParser/Stages/ReplaceStage.php b/src/Service/SnipParser/Stages/ReplaceStage.php index f9e4f45..bff13f8 100644 --- a/src/Service/SnipParser/Stages/ReplaceStage.php +++ b/src/Service/SnipParser/Stages/ReplaceStage.php @@ -4,12 +4,12 @@ namespace App\Service\SnipParser\Stages; use League\Pipeline\StageInterface; -class ReplaceStage implements StageInterface +readonly class ReplaceStage implements StageInterface { // replaces a string with another string public function __construct( - public readonly string $search, - public readonly string $replace, + public string $search, + public string $replace, ) {} public function __invoke(mixed $payload): string diff --git a/src/Service/SnipParser/Stages/UrlReferenceStage.php b/src/Service/SnipParser/Stages/UrlReferenceStage.php index 84e8f03..038845d 100644 --- a/src/Service/SnipParser/Stages/UrlReferenceStage.php +++ b/src/Service/SnipParser/Stages/UrlReferenceStage.php @@ -9,12 +9,12 @@ use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -class UrlReferenceStage implements StageInterface +readonly class UrlReferenceStage implements StageInterface { public function __construct( - #[Autowire(lazy: true)] private readonly UrlGeneratorInterface $router, - #[Autowire(lazy: true)] private readonly Security $security, - #[Autowire(lazy: true)] private readonly SnipRepository $snipRepository, + #[Autowire(lazy: true)] private UrlGeneratorInterface $router, + #[Autowire(lazy: true)] private Security $security, + #[Autowire(lazy: true)] private SnipRepository $snipRepository, ) {} public function __invoke(mixed $payload): string diff --git a/src/Service/SnipServiceFactory.php b/src/Service/SnipServiceFactory.php index 1359beb..1a95314 100644 --- a/src/Service/SnipServiceFactory.php +++ b/src/Service/SnipServiceFactory.php @@ -6,10 +6,10 @@ use App\Entity\Snip; use App\Service\SnipContent\SnipContentService; use Doctrine\ORM\EntityManagerInterface; -class SnipServiceFactory +readonly class SnipServiceFactory { public function __construct( - private readonly EntityManagerInterface $em, + private EntityManagerInterface $em, ) {} public function create(Snip $snip): SnipContentService