<?php
namespace App\EventSubscriber;
use App\Entity\Client;
use App\Entity\Parameter;
use App\Entity\Photo;
use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class AdministrationSubscriber implements EventSubscriberInterface
{
public function __construct(UserPasswordHasherInterface $passwordEncoder, KernelInterface $kernel)
{
$this->passwordEncoder = $passwordEncoder;
$this->kernel = $kernel;
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['add'],
BeforeEntityUpdatedEvent::class => ['upd'],
AfterEntityUpdatedEvent::class => ['clearCacheUpd'],
AfterEntityPersistedEvent::class => ['clearCachePersist'],
// AfterEntityPersistedEvent::class => ['newUpload']
];
}
public function encodePassword(User $entity){
{
$entity->setPassword($this->passwordEncoder->hashPassword($entity, $entity->getPlainPassword()));
}
}
public function encodePasswordClient(Client $entity){
{
$entity->setPassword($this->passwordEncoder->hashPassword($entity, $entity->getPlainPassword()));
}
}
public function add(BeforeEntityPersistedEvent $event){
$entity = $event->getEntityInstance();
if($entity instanceof User){
$this->encodePassword($entity);
}
if($entity instanceof Client){
$this->encodePasswordClient($entity);
}
}
public function upd(BeforeEntityUpdatedEvent $event){
$entity = $event->getEntityInstance();
if($entity instanceof User){
$this->encodePassword($entity);
}
if($entity instanceof Client){
$this->encodePasswordClient($entity);
}
}
public function clearCacheUpd(AfterEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if($entity instanceof Parameter){
set_time_limit(0);
$application = new Application($this->kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'c:c'
]);
$output = new NullOutput();
$application->run($input, $output);
}
}
public function clearCachePersist(AfterEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if($entity instanceof Parameter){
set_time_limit(0);
$application = new Application($this->kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'c:c'
]);
$output = new NullOutput();
$application->run($input, $output);
}
}
}