src/EventSubscriber/ProjectCrudSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Project;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class ProjectCrudSubscriber implements EventSubscriberInterface
  8. {
  9.     public function onBeforeEntityPersistedEvent(BeforeEntityPersistedEvent $event): void
  10.     {
  11.         /** @var Project $entity */
  12.         $entity $event->getEntityInstance();
  13.         if (!$entity instanceof Project) {
  14.             return;
  15.         }
  16.         $this->setAutoHash($entity);
  17.     }
  18.     public function onBeforeEntityUpdateEvent(BeforeEntityUpdatedEvent $event): void
  19.     {
  20.         /** @var Project $entity */
  21.         $entity $event->getEntityInstance();
  22.         if (!$entity instanceof Project) {
  23.             return;
  24.         }
  25.         $this->setAutoHash($entity);
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             BeforeEntityPersistedEvent::class => 'onBeforeEntityPersistedEvent',
  31.             BeforeEntityUpdatedEvent::class => 'onBeforeEntityUpdateEvent',
  32.         ];
  33.     }
  34.     public function setAutoHash(Project $entity): void
  35.     {
  36.         if (empty($entity->getHash())) {
  37.             $entity->setHash(null);
  38.         }
  39.     }
  40. }