src/EventSubscriber/BlameableSubscriber.php line 21

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Question;
  4. use App\Entity\User;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  7. use Symfony\Component\Security\Core\Security;
  8. use function Symfony\Component\String\u;
  9. class BlameableSubscriber implements EventSubscriberInterface
  10. {
  11.     private Security $security;
  12.     public function __construct(Security $security)
  13.     {
  14.         $this->security $security;
  15.     }
  16.     public function onBeforeEntityUpdatedEvent(BeforeEntityUpdatedEvent $event)
  17.     {
  18.         $question $event->getEntityInstance();
  19.         if (!$question instanceof Question) {
  20.             return;
  21.         }
  22.         $user $this->security->getUser();
  23.         if (!$user instanceof User) {
  24.             throw new \LogicException('Currently logged in user is not an instance of User?');
  25.         }
  26.         $question->setUpdatedBy($user);
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             BeforeEntityUpdatedEvent::class => 'onBeforeEntityUpdatedEvent',
  32.         ];
  33.     }
  34. }