src/EventSubscriber/InjectUserSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\CheckUserInListInterface;
  6. use App\Entity\UserTokenInterface;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use ReflectionException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Security\Core\Security;
  15. class InjectUserSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var Security
  19.      */
  20.     private $security;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private $manager;
  25.     public function __construct(Security $securityEntityManagerInterface $manager)
  26.     {
  27.         $this->security $security;
  28.         $this->manager $manager;
  29.     }
  30.     /**
  31.      * @return void
  32.      *
  33.      * @throws ReflectionException
  34.      */
  35.     public function setUser(ViewEvent $event)
  36.     {
  37.         $object $event->getControllerResult();
  38.         if (($object) && (!is_array($object))) {
  39.             $reflectionClass = new \ReflectionClass($object);
  40.             if ($reflectionClass->implementsInterface(UserTokenInterface::class)) {
  41.                 if ($event->getRequest()->isMethod(Request::METHOD_POST) && $object->getId() === null) {
  42.                     $uniqRelation $object->setUserToken($this->security->getUser());
  43.                     if ($uniqRelation !== null) {
  44.                         $parentMethod 'get'.ucfirst($uniqRelation['parent_name']);
  45.                         $old_object $this->manager->getRepository(get_class($object))->findOneBy([
  46.                             $uniqRelation['property_name'] => $this->security->getUser(),
  47.                             $uniqRelation['parent_name'] => $object->$parentMethod(),
  48.                         ]);
  49.                         if ($old_object) {
  50.                             $this->manager->remove($old_object);
  51.                             $this->manager->flush();
  52.                             if (isset($uniqRelation['uniq']) && $uniqRelation['uniq']) {
  53.                                 $event->setResponse(new JsonResponse([
  54.                                     'data' => [],
  55.                                     'message' => 'success',
  56.                                     'code' => 204,
  57.                                 ]));
  58.                                 return;
  59.                             }
  60.                         }
  61.                     }
  62.                 }
  63.                 if (method_exists($object'checkByUserToken')) {
  64.                     $object->checkByUserToken($this->security->getUser());
  65.                 }
  66.             }
  67.             if ($object instanceof Paginator && count($object) > 0) {
  68.                 foreach ($object as $objet) {
  69.                     $reflectionClass = new \ReflectionClass($objet);
  70.                     if (!$reflectionClass->implementsInterface(CheckUserInListInterface::class)) {
  71.                         return;
  72.                     }
  73.                     if (method_exists($objet'checkByUserToken')) {
  74.                         $objet->checkByUserToken($this->security->getUser());
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     /**
  81.      * @return array<string, mixed>
  82.      */
  83.     public static function getSubscribedEvents(): array
  84.     {
  85.         return [
  86.             KernelEvents::VIEW => ['setUser'EventPriorities::PRE_WRITE],
  87.         ];
  88.     }
  89. }