wrapped potential error in try catch

This commit is contained in:
Charles 2025-12-10 12:07:20 +01:00
parent 07bd064faa
commit 87ecf70d95
1 changed files with 16 additions and 6 deletions

View File

@ -2,6 +2,7 @@
namespace App\Service;
use App\Service\LoggerService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Entity\Cgu;
@ -9,7 +10,7 @@ use App\Entity\CguUser;
class CguUserService
{
public function __construct(private EntityManagerInterface $entityManager)
public function __construct(private EntityManagerInterface $entityManager, private readonly LoggerService $loggerService)
{
}
@ -40,11 +41,20 @@ class CguUserService
$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);
try{
// Create a new CguUser relation if it doesn't exist
$cguUser = new CguUser();
$cguUser->setUsers($user);
$cguUser->setCgu($latestCgu);
$this->entityManager->persist($cguUser);
}catch (\Exception $e){
$this->loggerService->logError('CguUserService', [
'acceptLatestCgu' => 'Failed to create CguUser relation',
'exception' => $e,
'targer_user_id' => $user->getId(),]);
throw new \RuntimeException('Failed to create CguUser relation: ' . $e->getMessage());
}
}
$cguUser->setIsAccepted(true);