src/EventSubscriber/EasyAdminSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Tenant;
  4. use App\Entity\TenantConfig;
  5. use App\Entity\User;
  6. use App\Service\TenantService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
  14. use Symfony\Component\Filesystem\Filesystem;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. class EasyAdminSubscriber implements EventSubscriberInterface {
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private EntityManagerInterface $entityManager;
  21.     /**
  22.      * @var Filesystem
  23.      */
  24.     private Filesystem $filesystem;
  25.     /**
  26.      * @var ContainerBagInterface
  27.      */
  28.     private ContainerBagInterface $params;
  29.     /**
  30.      * @var UserPasswordHasherInterface
  31.      */
  32.     private UserPasswordHasherInterface $passwordHasher;
  33.     /**
  34.      * @var TenantService
  35.      */
  36.     private TenantService $tenantService;
  37.     /**
  38.      * @param ContainerBagInterface       $params
  39.      * @param EntityManagerInterface      $entityManager
  40.      * @param Filesystem                  $filesystem
  41.      * @param TenantService               $tenantService
  42.      * @param UserPasswordHasherInterface $passwordHasher
  43.      *
  44.      * @SuppressWarnings(PHPMD.LongVariable)
  45.      */
  46.     public function __construct(
  47.         ContainerBagInterface $params,
  48.         EntityManagerInterface $entityManager,
  49.         Filesystem $filesystem,
  50.         TenantService $tenantService,
  51.         UserPasswordHasherInterface $passwordHasher
  52.     ) {
  53.         $this->params $params;
  54.         $this->entityManager $entityManager;
  55.         $this->filesystem $filesystem;
  56.         $this->tenantService $tenantService;
  57.         $this->passwordHasher $passwordHasher;
  58.     }
  59.     public static function getSubscribedEvents() {
  60.         return [
  61.             AfterEntityPersistedEvent::class => ['afterEntityPersistedCallback'],
  62.             BeforeEntityPersistedEvent::class => ['createEntityCallback'],
  63.             BeforeEntityUpdatedEvent::class => ['updateEntityCallback'],
  64.         ];
  65.     }
  66.     /**
  67.      * BeforeEntityPersistedEvent callback function.
  68.      *
  69.      * @param BeforeEntityPersistedEvent $event
  70.      *
  71.      * @return void
  72.      */
  73.     public function createEntityCallback(BeforeEntityPersistedEvent $event): void {
  74.         $entity $event->getEntityInstance();
  75.         if (!($entity instanceof User)) {
  76.             return;
  77.         }
  78.         if (empty($entity->getPlainTextPassword())) {
  79.             return;
  80.         }
  81.         $entity->setPassword(
  82.             $this->passwordHasher->hashPassword(
  83.                 $entity,
  84.                 $entity->getPlainTextPassword()
  85.             )
  86.         );
  87.     }
  88.     /**
  89.      * AfterEntityPersistedEvent callback function.
  90.      *
  91.      * @param AfterEntityPersistedEvent $event
  92.      *
  93.      * @return void
  94.      */
  95.     public function afterEntityPersistedCallback(AfterEntityPersistedEvent $event): void {
  96.         $entity $event->getEntityInstance();
  97.         $projectDir $this->params->get('kernel.project_dir');
  98.         if ($entity instanceof Tenant) {
  99.             $directoryToCreate \sprintf('%1$s/public/images/%2$s'$projectDir$entity->getUploadDirectory());
  100.             if (!$this->filesystem->exists($directoryToCreate)) {
  101.                 try {
  102.                     $this->filesystem->mkdir($directoryToCreate);
  103.                 } catch (IOExceptionInterface $exception) {
  104.                     echo 'An error occurred while creating your directory at ' $exception->getPath();
  105.                 }
  106.             }
  107.             if (!$entity->hasTenangConfig()) {
  108.                 $entity->setTenantConfig(
  109.                     new TenantConfig()
  110.                 );
  111.             }
  112.             $this->entityManager->flush();
  113.             $this->tenantService->addDefaultTextTemplates($entity);
  114.         }
  115.     }
  116.     /**
  117.      * BeforeEntityUpdatedEvent callback function.
  118.      *
  119.      * @param BeforeEntityUpdatedEvent $event
  120.      *
  121.      * @return void
  122.      */
  123.     public function updateEntityCallback(BeforeEntityUpdatedEvent $event): void {
  124.         $entity $event->getEntityInstance();
  125.         if (!($entity instanceof User)) {
  126.             return;
  127.         }
  128.         if (empty($entity->getPlainTextPassword())) {
  129.             return;
  130.         }
  131.         $entity->setPassword(
  132.             $this->passwordHasher->hashPassword(
  133.                 $entity,
  134.                 $entity->getPlainTextPassword()
  135.             )
  136.         );
  137.     }
  138. }