<?php
namespace App\EventSubscriber;
use App\Entity\Article;
use App\Entity\ArticleContenu;
use App\Entity\Page;
use App\Entity\PageContenu;
use App\Entity\Projet;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $entityManager;
private $passwordEncoder;
private ManagerRegistry $manager;
public function __construct(
ManagerRegistry $manager,
EntityManagerInterface $entityManager,
UserPasswordEncoderInterface $passwordEncoder,
LoggerInterface $logger
) {
$this->manager = $manager;
$this->entityManager = $entityManager;
$this->passwordEncoder = $passwordEncoder;
$this->logger = $logger;
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['beforeAdd'],
BeforeEntityUpdatedEvent::class => ['beforeUpdate'], //surtout utile lors d'un reset de mot passe plutôt qu'un réel update, car l'update va de nouveau encrypter le mot de passe DEJA encrypté ...
];
}
public function beforeAdd(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof User) {
$this->setPassword($entity);
} else if ($entity instanceof Page) {
$entity->setDescription(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getDescription()));
$entity->setDescriptionCourte(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getDescriptionCourte()));
$entity->setDescriptionFin(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getDescriptionFin()));
} else if ($entity instanceof PageContenu) {
$entity->setParagraph(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getParagraph()));
} else if ($entity instanceof Article) {
$entity->setDescription(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getDescription()));
} else if ($entity instanceof ArticleContenu) {
$entity->setParagraphe(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getParagraphe()));
} else if ($entity instanceof Projet) {
$entity->setAppSecret(bin2hex(random_bytes(16)));
} else {
return;
}
}
public function beforeUpdate(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
/* if ($entity instanceof User) {
$this->setPassword($entity);
} else */
if ($entity instanceof Page) {
$entity->setDescription(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getDescription()));
$entity->setDescriptionCourte(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getDescriptionCourte()));
$entity->setDescriptionFin(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getDescriptionFin()));
} else if ($entity instanceof PageContenu) {
$entity->setParagraph(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getParagraph()));
} else if ($entity instanceof Article) {
$entity->setDescription(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getDescription()));
} else if ($entity instanceof ArticleContenu) {
$entity->setParagraphe(str_replace([
'<div>', '</div>',
'<h1>', '</h1>',
'<h2>', '</h2>',
'<h3>', '</h3>',
'<h4>', '</h4>',
'<h5>', '</h5>',
'<h6>', '</h6>',
'<p>', '</p>',
'<pre>', '</pre>',
'<blockquote>', '</blockquote>'
], "", $entity->getParagraphe()));
} else if ($entity instanceof Projet) {
if ($entity->getAppSecret() == null || $entity->getAppSecret() == '') {
$entity->setAppSecret(bin2hex(random_bytes(16)));
}
} else {
return;
}
}
/**
* @param User $entity
*/
public function setPassword(User $entity): void
{
$pass = $entity->getPassword();
$entity->setPassword(
$this->passwordEncoder->encodePassword(
$entity,
$pass
)
);
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
}