src/EventSubscriber/AddOwnerEntityToItemSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Block;
  5. use App\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUser;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. // use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. final class AddOwnerEntityToItemSubscriber implements EventSubscriberInterface
  17. {
  18.     private $security;
  19.     private $doctrine;
  20.     public function __construct(Security $securityManagerRegistry $doctrine)
  21.     {
  22.         $this->security $security;
  23.         $this->doctrine $doctrine;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::VIEW => ['attachOwner'EventPriorities::PRE_WRITE],
  29.         ];
  30.     }
  31.     public function attachOwner(ViewEvent $event)
  32.     {
  33.         $item $event->getControllerResult();
  34.         $method $event->getRequest()->getMethod();
  35.         if (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method) {
  36.             // On ne gère que la création et la modification
  37.             // die('PAS LA BONE METHODE');
  38.             return;
  39.         }
  40.         if ( ! method_exists $item 'getOwner' ) || !method_exists $item 'getEntity' ) )
  41.         {
  42.             // throw new \Exception('PAS DE METHODE getowner');
  43.             return;
  44.         }
  45.         // maybe these extra null checks are not even needed
  46.         $owner $this->security->getUser();
  47.         /*
  48.         $token = $this->tokenStorage->getToken();
  49.         if (!$token) {
  50.             // throw new \Exception('PAS DE TOKEN');
  51.             return;
  52.         }
  53.         $owner = $token->getUser();
  54.        */
  55.         if (!$owner instanceof JWTUser) {
  56.             // throw new \Exception('PAS UN JWTUSER');
  57.             return;
  58.         }
  59.         $username $owner->getUsername();
  60.         $owner $this->doctrine->getRepository('App\\Entity\\User')->findOneBy(['username' => $username]);
  61.         $entity $owner->getEntity();
  62.         // En création on renseigne le owner et son entité
  63.         if (Request::METHOD_POST === $method) {
  64.             // throw new \Exception('YES ON MAJ');
  65.             // On ne gère que la création et la modification
  66.             // Attach the user to the not yet persisted Article
  67.             $item->setOwner($owner);
  68.             // l'entité du user
  69.             $item->setEntity($entity);
  70.             // la date de création
  71.             if (method_exists $item 'setCreatedAt' ) )
  72.                 $item->setCreatedAt(new \DateTime());
  73.         } else {
  74.             // throw new \Exception('PAS UN POST UN MAJ');
  75.             $item->setOwner($owner);
  76.         }
  77.         // Dans tous les cas on renseigne la date de modification
  78.         if (method_exists $item 'setUpdatedAt' ) )
  79.             $item->setUpdatedAt(new \DateTime());
  80.     }
  81. }