src/EventSubscriber/EasyAdminSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Instance;
  4. use App\Service\AppHelper;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityBuiltEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class EasyAdminSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(private AppHelper $appHelper)
  14.     {}
  15.     public static function getSubscribedEvents()
  16.     {
  17.         return [
  18.             BeforeEntityPersistedEvent::class => ['encryptPassword'],
  19.             BeforeEntityUpdatedEvent::class => ['updatePassword'],
  20.             AfterEntityBuiltEvent::class => ['uncryptPassword'],
  21.         ];
  22.     }
  23.     public function encryptPassword(BeforeEntityPersistedEvent $event)
  24.     {
  25.         $entity $event->getEntityInstance();
  26.         if (!($entity instanceof Instance)) {
  27.             return;
  28.         }
  29.         $entity->setPassword($this->appHelper->encryptPassword($entity->getPassword()));
  30.     }
  31.     public function updatePassword(BeforeEntityUpdatedEvent $event)
  32.     {
  33.         $entity $event->getEntityInstance();
  34.         if (!($entity instanceof Instance)) {
  35.             return;
  36.         }
  37.         $entity->setPassword($this->appHelper->encryptPassword($entity->getPassword()));
  38.     }
  39.     public function uncryptPassword(AfterEntityBuiltEvent $event)
  40.     {
  41.         $entity $event->getEntity()->getInstance();
  42.         if (!($entity instanceof Instance)) {
  43.             return;
  44.         }
  45.         $entity->setPassword($this->appHelper->uncryptPassword($entity->getPassword()));
  46.     }
  47. }