lib/boab/cms-bundle/src/EventListener/UserRelationListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\EventListener;
  3. use Doctrine\ORM\Event\LifecycleEventArgs;
  4. use Boab\CmsBundle\Entity\ContentInterface;
  5. use Boab\CmsBundle\Entity\UserRelationInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Boab\CmsBundle\Entity\User;
  8. class UserRelationListener
  9. {
  10.     private $tokenStorage;
  11.     public function __construct(TokenStorageInterface $tokenStorage)
  12.     {
  13.         $this->tokenStorage $tokenStorage;
  14.     }
  15.     public function prePersist(LifecycleEventArgs $args
  16.     {
  17.         $entity $args->getEntity();
  18.         if(null == $this->tokenStorage->getToken()){
  19.             return;
  20.         }
  21.         $user $this->tokenStorage->getToken()->getUser();
  22.         if(!$entity instanceof UserRelationInterface || !$user){
  23.             return;
  24.         }
  25.         
  26.         $entityManager $args->getEntityManager();
  27.         $userReference $entityManager->getReference(User::class,$user->getId());
  28.         $entity->setUser($userReference);
  29.     }    
  30.     public function preUpdate(LifecycleEventArgs $args)
  31.     {
  32.     }   
  33. }