<?php
namespace App\EventSubscriber;
use App\Entity\Admin\Property;
use App\Entity\Project;
use App\Entity\User;
use DateTime;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class EasyAdminEventSubscriber implements EventSubscriberInterface
{
private $passwordHasher;
public function __construct(UserPasswordHasherInterface $passwordHasher)
{
return $this->passwordHasher = $passwordHasher;
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['setCreatedAtAndisSoldeAndFileUpload'],
BeforeEntityPersistedEvent::class => ['setUserPassWord'],
BeforeEntityPersistedEvent::class =>['setProjectCreatedAt']
];
}
public function setCreatedAtAndisSoldeAndFileUpload(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Property)) {
return;
}
$date = new DateTime('now');
$entity->setCreatedAt($date);
$entity->setIsSolde(false);
}
public function setUserPassWord(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
$password = $entity->getPassword();
$entity->setPassword($this->passwordHasher->hashPassword($entity, $password));
}
public function setProjectCreatedAt(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity) instanceof Project){
return;
}
$date = new DateTime('now');
$entity->setProjectCreatedAt($date);
$entity->setIsFinish(false);
}
}