src/EventSubscriber/AdministrationSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Client;
  4. use App\Entity\Parameter;
  5. use App\Entity\Photo;
  6. use App\Entity\User;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Bundle\FrameworkBundle\Console\Application;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use Symfony\Component\Console\Input\ArrayInput;
  15. use Symfony\Component\Console\Output\NullOutput;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. class AdministrationSubscriber implements EventSubscriberInterface
  18. {
  19.     public function __construct(UserPasswordHasherInterface $passwordEncoder,  KernelInterface $kernel)
  20.     {
  21.         $this->passwordEncoder $passwordEncoder;
  22.         $this->kernel $kernel;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             BeforeEntityPersistedEvent::class => ['add'],
  28.             BeforeEntityUpdatedEvent::class => ['upd'],
  29.             AfterEntityUpdatedEvent::class => ['clearCacheUpd'],
  30.             AfterEntityPersistedEvent::class => ['clearCachePersist'],
  31.             // AfterEntityPersistedEvent::class => ['newUpload']
  32.         ];
  33.     }
  34.     public function encodePassword(User $entity){
  35.        
  36.         {
  37.             $entity->setPassword($this->passwordEncoder->hashPassword($entity$entity->getPlainPassword()));
  38.         }
  39.     }
  40.     public function encodePasswordClient(Client $entity){
  41.        
  42.         {
  43.             $entity->setPassword($this->passwordEncoder->hashPassword($entity$entity->getPlainPassword()));
  44.         }
  45.     }
  46.     public function add(BeforeEntityPersistedEvent $event){
  47.         $entity $event->getEntityInstance();
  48.          if($entity instanceof User){
  49.             $this->encodePassword($entity);
  50.          }
  51.          if($entity instanceof Client){
  52.             $this->encodePasswordClient($entity);
  53.          }
  54.         
  55.     }
  56.     public function upd(BeforeEntityUpdatedEvent $event){
  57.         $entity $event->getEntityInstance();
  58.          if($entity instanceof User){
  59.             $this->encodePassword($entity);
  60.          }
  61.          if($entity instanceof Client){
  62.             $this->encodePasswordClient($entity);
  63.          }
  64.     }
  65.     public function clearCacheUpd(AfterEntityUpdatedEvent $event)
  66.     {
  67.         $entity $event->getEntityInstance();
  68.         if($entity instanceof Parameter){
  69.             set_time_limit(0);
  70.             $application = new Application($this->kernel);
  71.             $application->setAutoExit(false);
  72.             $input = new ArrayInput([
  73.                 'command' => 'c:c'
  74.             ]);
  75.             $output = new NullOutput();
  76.             $application->run($input$output);
  77.         }
  78.     }
  79.     public function clearCachePersist(AfterEntityPersistedEvent $event)
  80.     {
  81.         $entity $event->getEntityInstance();
  82.         if($entity instanceof Parameter){
  83.             set_time_limit(0);
  84.             $application = new Application($this->kernel);
  85.             $application->setAutoExit(false);
  86.             $input = new ArrayInput([
  87.                 'command' => 'c:c'
  88.             ]);
  89.             $output = new NullOutput();
  90.             $application->run($input$output);
  91.         }
  92.     }
  93. }