<?php
namespace App\EventListener;
use App\Entity\CgvUserSignature;
use App\Event\UserAcceptedTermsOfServiceForSale;
use App\Repository\CgvUserSignatureRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserTermsOfServiceSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly EntityManagerInterface $em, private readonly CgvUserSignatureRepository $cgvRepository)
{
}
public static function getSubscribedEvents()
{
return [
UserAcceptedTermsOfServiceForSale::NAME => 'termsOfServiceAccepted',
];
}
public function termsOfServiceAccepted(UserAcceptedTermsOfServiceForSale $event): void
{
$user = $event->getUser();
$sale = $event->getSale();
$termsOfService = $this->cgvRepository->findValidByUserAndCountry($user, $sale);
// Terms of service are valid, don't change them
if ($termsOfService) {
return;
}
$signature = new CgvUserSignature();
$signature->setUser($user);
$signature->setCountry($sale->getCountry());
$this->em->persist($signature);
$this->em->flush();
}
}