<?php
namespace Boab\CmsBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Boab\CmsBundle\Entity\ContentInterface;
use Boab\CmsBundle\Entity\UserRelationInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Boab\CmsBundle\Entity\User;
class UserRelationListener
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if(null == $this->tokenStorage->getToken()){
return;
}
$user = $this->tokenStorage->getToken()->getUser();
if(!$entity instanceof UserRelationInterface || !$user){
return;
}
$entityManager = $args->getEntityManager();
$userReference = $entityManager->getReference(User::class,$user->getId());
$entity->setUser($userReference);
}
public function preUpdate(LifecycleEventArgs $args)
{
}
}