From 7c9f49cdf0b2872f530afd0514402b9f6d62f9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Skowro=C5=84ski?= Date: Sat, 26 Aug 2023 14:35:36 +0200 Subject: [PATCH] Fix issue with mods lost on update --- .../Doctrine/ModGroupUpdatedSubscriber.php | 47 +++---------------- src/Repository/ModList/ModListRepository.php | 19 -------- .../ModListUpdateService.php | 46 ++++++++++++++++++ .../ModListUpdateServiceInterface.php | 12 +++++ 4 files changed, 64 insertions(+), 60 deletions(-) create mode 100644 src/Service/ModListUpdateService/ModListUpdateService.php create mode 100644 src/Service/ModListUpdateService/ModListUpdateServiceInterface.php diff --git a/src/EventSubscriber/Doctrine/ModGroupUpdatedSubscriber.php b/src/EventSubscriber/Doctrine/ModGroupUpdatedSubscriber.php index 04f6f2d4..ad8d13aa 100644 --- a/src/EventSubscriber/Doctrine/ModGroupUpdatedSubscriber.php +++ b/src/EventSubscriber/Doctrine/ModGroupUpdatedSubscriber.php @@ -5,21 +5,17 @@ namespace App\EventSubscriber\Doctrine; use App\Entity\ModGroup\ModGroupInterface; -use App\Entity\ModList\ModList; -use App\Entity\User\User; -use App\Repository\ModList\ModListRepository; +use App\Service\ModListUpdateService\ModListUpdateServiceInterface; use Doctrine\Common\EventSubscriber; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\LifecycleEventArgs; -use Doctrine\ORM\Event\PostFlushEventArgs; use Doctrine\ORM\Events; -use Symfony\Component\Security\Core\Security; class ModGroupUpdatedSubscriber implements EventSubscriber { - private ?ModGroupInterface $updatedModGroup = null; - public function __construct( - private Security $security + private EntityManagerInterface $entityManager, + private ModListUpdateServiceInterface $modListUpdateService, ) { } @@ -27,49 +23,18 @@ public function getSubscribedEvents(): array { return [ Events::preUpdate, - Events::postFlush, ]; } public function preUpdate(LifecycleEventArgs $args): void { - $entityManager = $args->getEntityManager(); $modGroup = $args->getObject(); // Do nothing if updated entity is not a Mod Group or no changes were made to the entity - if (!$modGroup instanceof ModGroupInterface || !$entityManager->getUnitOfWork()->getEntityChangeSet($modGroup)) { - return; - } - - // Save Mod Group for update of associated Mod Lists - $this->updatedModGroup = $modGroup; - } - - public function postFlush(PostFlushEventArgs $args): void - { - // Do nothing if no Mod Group updated - if (!$this->updatedModGroup) { + if (!$modGroup instanceof ModGroupInterface || !$this->entityManager->getUnitOfWork()->getEntityChangeSet($modGroup)) { return; } - $entityManager = $args->getEntityManager(); - - /** @var ModListRepository $modListRepository */ - $modListRepository = $entityManager->getRepository(ModList::class); - - /** @var null|User $currentUser */ - $currentUser = $this->security->getUser(); - - // Get Mod Lists that use this Mod Group and update their "last changed by/at" properties - $modLists = $modListRepository->findModListsContainingModGroup($this->updatedModGroup); - foreach ($modLists as $modList) { - $modList->setLastUpdatedAt(new \DateTimeImmutable()); - $modList->setLastUpdatedBy($currentUser); - } - - // Clear updated Mod Group. This prevents executing this method after next flush - $this->updatedModGroup = null; - - $entityManager->flush(); + $this->modListUpdateService->updateModListsAssociatedWithModGroup($modGroup); } } diff --git a/src/Repository/ModList/ModListRepository.php b/src/Repository/ModList/ModListRepository.php index 1ca700a9..70aea349 100644 --- a/src/Repository/ModList/ModListRepository.php +++ b/src/Repository/ModList/ModListRepository.php @@ -4,7 +4,6 @@ namespace App\Repository\ModList; -use App\Entity\ModGroup\ModGroupInterface; use App\Entity\ModList\ModList; use App\Entity\ModList\ModListInterface; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; @@ -22,22 +21,4 @@ public function __construct(ManagerRegistry $registry) { parent::__construct($registry, ModList::class); } - - /** - * @return ModListInterface[] - */ - public function findModListsContainingModGroup(ModGroupInterface $modGroup): array - { - $qb = $this->getEntityManager()->createQueryBuilder(); - $expr = $qb->expr(); - - $qb - ->addSelect('ml') - ->from(ModList::class, 'ml') - ->join('ml.modGroups', 'mg') - ->andWhere($expr->eq('mg.id', $expr->literal($modGroup->getId()->toString()))) - ; - - return $qb->getQuery()->getResult(); - } } diff --git a/src/Service/ModListUpdateService/ModListUpdateService.php b/src/Service/ModListUpdateService/ModListUpdateService.php new file mode 100644 index 00000000..7cbda98c --- /dev/null +++ b/src/Service/ModListUpdateService/ModListUpdateService.php @@ -0,0 +1,46 @@ +security->getUser(); + if (!$currentUser instanceof UserInterface) { + return; + } + + $queryBuilder = $this->entityManager->createQueryBuilder(); + $expr = $queryBuilder->expr(); + + $queryBuilder + ->update(ModList::class, 'ml') + + ->set('ml.lastUpdatedAt', ':dateTime') + ->setParameter('dateTime', new \DateTimeImmutable()) + + ->set('ml.lastUpdatedBy', ':user') + ->setParameter('user', $currentUser) + + ->andWhere($expr->isMemberOf(':modGroupId', 'ml.modGroups')) + ->setParameter('modGroupId', $modGroup->getId()->toString()) + ; + + $queryBuilder->getQuery()->execute(); + } +} diff --git a/src/Service/ModListUpdateService/ModListUpdateServiceInterface.php b/src/Service/ModListUpdateService/ModListUpdateServiceInterface.php new file mode 100644 index 00000000..90e0b167 --- /dev/null +++ b/src/Service/ModListUpdateService/ModListUpdateServiceInterface.php @@ -0,0 +1,12 @@ +