src/EventSubscriber/EasyAdminSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use App\Service\DocumentBackfillService;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. class EasyAdminSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var \Doctrine\ORM\EntityManagerInterface
  14.      */
  15.     private $manager;
  16.     /**
  17.      * @var DocumentBackfillService
  18.      */
  19.     private $backfill;
  20.     public function __construct(EntityManagerInterface $managerDocumentBackfillService $backfill)
  21.     {
  22.         $this->manager $manager;
  23.         $this->backfill $backfill;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             BeforeEntityPersistedEvent::class => ['checkUser'],
  29.             BeforeCrudActionEvent::class => ['backfillScansOnEdit'],
  30.         ];
  31.     }
  32.     /**
  33.      * When an entity's EDIT page is opened in the admin, retroactively scan
  34.      * any of its attached documents that don't yet have an optimized version.
  35.      * Generic across every parent type the backfill registry knows about.
  36.      */
  37.     public function backfillScansOnEdit(BeforeCrudActionEvent $event): void
  38.     {
  39.         try {
  40.             $context $event->getAdminContext();
  41.             if ($context === null || $context->getCrud() === null) {
  42.                 return;
  43.             }
  44.             if (Crud::PAGE_EDIT !== $context->getCrud()->getCurrentPage()) {
  45.                 return;
  46.             }
  47.             $entityDto $context->getEntity();
  48.             if ($entityDto === null || !$entityDto->isAccessible()) {
  49.                 return;
  50.             }
  51.             $instance $entityDto->getInstance();
  52.             if (is_object($instance) && $this->backfill->supports($instance)) {
  53.                 $this->backfill->backfillForParent($instance);
  54.             }
  55.         } catch (\Throwable $e) {
  56.             // Never break the admin because of a scan; degradation is handled deeper too.
  57.         }
  58.     }
  59.     public function checkUser(BeforeEntityPersistedEvent $event)
  60.     {
  61.         /*$entity = $event->getEntityInstance();
  62.         if (!($entity instanceof User)) {
  63.             return;
  64.         }
  65.         $user = $this->manager->getRepository( User::class )->findBy( [ 'email' => $entity->getEmail() ] );
  66.         if ($user) {
  67.             //die;
  68.         }*/
  69.     }
  70. }