src/EventListener/UserTermsOfServiceSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\CgvUserSignature;
  4. use App\Event\UserAcceptedTermsOfServiceForSale;
  5. use App\Repository\CgvUserSignatureRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class UserTermsOfServiceSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private readonly EntityManagerInterface $em, private readonly CgvUserSignatureRepository $cgvRepository)
  11.     {
  12.     }
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             UserAcceptedTermsOfServiceForSale::NAME => 'termsOfServiceAccepted',
  17.         ];
  18.     }
  19.     public function termsOfServiceAccepted(UserAcceptedTermsOfServiceForSale $event): void
  20.     {
  21.         $user $event->getUser();
  22.         $sale $event->getSale();
  23.         $termsOfService $this->cgvRepository->findValidByUserAndCountry($user$sale);
  24.         // Terms of service are valid, don't change them
  25.         if ($termsOfService) {
  26.             return;
  27.         }
  28.         $signature = new CgvUserSignature();
  29.         $signature->setUser($user);
  30.         $signature->setCountry($sale->getCountry());
  31.         $this->em->persist($signature);
  32.         $this->em->flush();
  33.     }
  34. }