src/EventSubscriber/AdminSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Image;
  4. use App\Entity\Menu;
  5. use App\Entity\Restaurante;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. class AdminSubscriber implements EventSubscriberInterface
  11. {
  12.     private Security $security;
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             BeforeEntityUpdatedEvent::class => ['onUpdatedsetImageUser'],
  21.             BeforeEntityPersistedEvent::class => ['onPersistedsetImageUser'],
  22.         ];
  23.     }
  24.     public function onUpdatedsetImageUser(BeforeEntityUpdatedEvent $event)
  25.     {        
  26.         $entity $event->getEntityInstance();
  27.         if ($entity instanceof Menu) {
  28.             $user $entity->getRestaurante()->getUser();
  29.             $images $entity->getImages();
  30.             $this->updateImages($images,$user);
  31.         } elseif ($entity instanceof Restaurante) {            
  32.             $user $entity->getUser();
  33.             $images $entity->getImages();
  34.             $this->updateImages($images,$user);
  35.         }
  36.         return;
  37.     }
  38.     public function onPersistedsetImageUser(BeforeEntityPersistedEvent $event)
  39.     {
  40.         $entity $event->getEntityInstance();
  41.         
  42.         if ($entity instanceof Menu) {
  43.             $user $entity->getRestaurante()->getUser();
  44.             $images $entity->getImages();
  45.             $this->updateImages($images,$user);
  46.         } elseif ($entity instanceof Restaurante) {            
  47.             $user $entity->getUser();
  48.             $images $entity->getImages();
  49.             $this->updateImages($images,$user);
  50.         }
  51.         return;
  52.     }
  53.     public function updateImages($images,$user){
  54.         foreach ($images as $image) {
  55.             if($image->getFile()){
  56.                 $image->setFilepath('');
  57.             }
  58.             $image->setUser($user);
  59.         }
  60.     }
  61. }