Easy_solution/src/Service/CguUserService.php

63 lines
2.0 KiB
PHP

<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Entity\Cgu;
use App\Entity\CguUser;
class CguUserService
{
public function __construct(private EntityManagerInterface $entityManager)
{
}
public function isLatestCguAccepted(UserInterface $user): bool
{
$latestCgu = $this->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();
}
}