<?php
namespace App\EventSubscriber;
use App\Entity\Instance;
use App\Service\AppHelper;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityBuiltEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
public function __construct(private AppHelper $appHelper)
{}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['encryptPassword'],
BeforeEntityUpdatedEvent::class => ['updatePassword'],
AfterEntityBuiltEvent::class => ['uncryptPassword'],
];
}
public function encryptPassword(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Instance)) {
return;
}
$entity->setPassword($this->appHelper->encryptPassword($entity->getPassword()));
}
public function updatePassword(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Instance)) {
return;
}
$entity->setPassword($this->appHelper->encryptPassword($entity->getPassword()));
}
public function uncryptPassword(AfterEntityBuiltEvent $event)
{
$entity = $event->getEntity()->getInstance();
if (!($entity instanceof Instance)) {
return;
}
$entity->setPassword($this->appHelper->uncryptPassword($entity->getPassword()));
}
}