src/EventSubscriber/EasyAdminEventSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Admin\Property;
  4. use App\Entity\Project;
  5. use App\Entity\User;
  6. use DateTime;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. class EasyAdminEventSubscriber implements EventSubscriberInterface
  11. {
  12.     private $passwordHasher;
  13.     public function __construct(UserPasswordHasherInterface $passwordHasher)
  14.     {
  15.         return $this->passwordHasher $passwordHasher;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             BeforeEntityPersistedEvent::class => ['setCreatedAtAndisSoldeAndFileUpload'],
  21.             BeforeEntityPersistedEvent::class => ['setUserPassWord'],
  22.             BeforeEntityPersistedEvent::class =>['setProjectCreatedAt']
  23.         ];
  24.     }
  25.     public function setCreatedAtAndisSoldeAndFileUpload(BeforeEntityPersistedEvent $event)
  26.     {
  27.         $entity $event->getEntityInstance();
  28.         if (!($entity instanceof Property)) {
  29.             return;
  30.         }
  31.         $date = new DateTime('now');
  32.         $entity->setCreatedAt($date);
  33.         $entity->setIsSolde(false);
  34.     }
  35.     public function setUserPassWord(BeforeEntityPersistedEvent $event)
  36.     {
  37.         $entity $event->getEntityInstance();
  38.         if (!($entity instanceof User)) {
  39.             return;
  40.         }
  41.         $password $entity->getPassword();
  42.         $entity->setPassword($this->passwordHasher->hashPassword($entity$password));
  43.     }
  44.     public function setProjectCreatedAt(BeforeEntityPersistedEvent $event)
  45.     {
  46.         $entity $event->getEntityInstance();
  47.         if (!($entity) instanceof Project){
  48.             return;
  49.         }
  50.         $date = new DateTime('now');
  51.         $entity->setProjectCreatedAt($date);
  52.         $entity->setIsFinish(false);
  53.     }
  54. }