src/EventSubscriber/UpdateUserSubscriber.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\User;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ViewEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class UpdateUserSubscriber implements EventSubscriberInterface
  9. {
  10.     public function onKernelView(ViewEvent $event): void
  11.     {
  12.         $user $event->getControllerResult();
  13.         $method $event->getRequest()->getMethod();
  14.         
  15.         if ($user instanceof User && $method === "PUT") {
  16.             $user->setUpdatedAt(new \DateTime());
  17.         }
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_WRITE]
  23.         ];
  24.     }
  25. }