entityManager->getRepository(Cgu::class)->findLatestCgu(); if (!$latestCgu) { // If no CGU exists, set to false return false; } $cguUser = $this->entityManager->getRepository(CguUser::class)->findOneBy(['users' => $user, 'cgu' => $latestCgu]); if (!$cguUser) { // If the relation doesn't exist, the user hasn't seen or accepted the latest CGU return false; } return $cguUser->isAccepted(); } public function acceptLatestCgu(UserInterface $user): void { $latestCgu = $this->entityManager->getRepository(Cgu::class)->findLatestCgu(); if (!$latestCgu) { // No CGU to accept return; } $cguUser = $this->entityManager->getRepository(CguUser::class)->findOneBy(['users' => $user, 'cgu' => $latestCgu]); if (!$cguUser) { // Create a new CguUser relation if it doesn't exist $cguUser = new CguUser(); $cguUser->setUsers($user); $cguUser->setCgu($latestCgu); $this->entityManager->persist($cguUser); } $cguUser->setIsAccepted(true); $this->entityManager->flush(); } //Function can only be called if the user has already accepted the CGU public function declineCgu(UserInterface $user, Cgu $cgu): void { $cguUser = $this->entityManager->getRepository(CguUser::class)->findOneBy(['users' => $user, 'cgu' => $cgu]); $cguUser->setIsAccepted(false); $this->entityManager->flush(); } }