src/Events/MobilityEventSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Mobility;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Security\Core\Security;
  10. class MobilityEventSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var Security
  14.      */
  15.     private $security;
  16.     /**
  17.      * MobilityEventSubscriber constructor.
  18.      * @param Security $security
  19.      */
  20.     public function __construct(Security $security)
  21.     {
  22.         $this->security $security;
  23.     }
  24.     /**
  25.      * @return array<string, mixed>
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             KernelEvents::VIEW => [ 'setUser'EventPriorities::PRE_WRITE, ],
  31.         ];
  32.     }
  33.     /**
  34.      * @param ViewEvent $event
  35.      * @return void
  36.      */
  37.     public function setUser(ViewEvent $event)
  38.     {
  39.         $object $event->getControllerResult();
  40.         $request $event->getRequest();
  41.         if (!$object instanceof Mobility or Request::METHOD_POST !== $request->getMethod()) {
  42.             return;
  43.         }
  44.         $object->setAuthor($this->security->getUser());
  45.     }
  46. }