vendor/cofondateur/socle-technique/src/EventSubscriber/EasyAdminSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace Cofondateur\SocleTechniqueBundle\EventSubscriber;
  3. use Cofondateur\SocleTechniqueBundle\Traits\UpdatableInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. class EasyAdminSubscriber implements EventSubscriberInterface
  10. {
  11.     /** @var Security  */
  12.     private $security;
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             BeforeEntityPersistedEvent::class => ['prePersist'],
  21.             BeforeEntityUpdatedEvent::class => ['preUpdate']
  22.         ];
  23.     }
  24.     public function prePersist(BeforeEntityPersistedEvent $event)
  25.     {
  26.         $entity $event->getEntityInstance();
  27.         if ($entity instanceof UpdatableInterface) {
  28.             $entity->setCreatedAt(new \DateTime());
  29.             $entity->setCreatedBy($this->security->getUser());
  30.         }
  31.     }
  32.     public function preUpdate(BeforeEntityUpdatedEvent $event)
  33.     {
  34.         $entity $event->getEntityInstance();
  35.         if ($entity instanceof UpdatableInterface) {
  36.             $entity->setUpdatedAt(new \DateTime());
  37.             $entity->setUpdatedBy($this->security->getUser());
  38.         }
  39.     }
  40. }