src/EventListener/UserEventListener.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\User;
  4. use App\Entity\UserProject;
  5. use App\Service\ZpriceHelper;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  8. use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailureException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class UserEventListener implements EventSubscriberInterface
  11. {
  12.     private ZpriceHelper $zPriceHelper;
  13.     private EntityManagerInterface $manager;
  14.     public function __construct(ZpriceHelper $zPriceHelperEntityManagerInterface $manager)
  15.     {
  16.         $this->zPriceHelper $zPriceHelper;
  17.         $this->manager $manager;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             AfterEntityPersistedEvent::class => 'onUserPersisted',
  23.         ];
  24.     }
  25.     /**
  26.      * @throws JWTDecodeFailureException
  27.      */
  28.     public function onUserPersisted(AfterEntityPersistedEvent $event): void
  29.     {
  30.         $entity $event->getEntityInstance();
  31.         if ($entity instanceof User) {
  32.             $currentProject $this->zPriceHelper->getCurrentProject();
  33.             if ($currentProject) {
  34.                 $up = new UserProject($entity$currentProject$entity->getRoles());
  35.                 $this->manager->persist($up);
  36.                 $entity->addUserProject($up);
  37.                 $this->manager->flush();
  38.             }
  39.         }
  40.     }
  41. }