src/EventSubscriber/EasyAdminSubscriber.php line 30

Open in your IDE?
  1. <?PHP
  2. # src/EventSubscriber/EasyAdminSubscriber.php
  3. namespace App\EventSubscriber;
  4. use App\Entity\Realisation;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class EasyAdminSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var ParameterBagInterface
  12.      */
  13.     private $parameterBag;
  14.     public function __construct(ParameterBagInterface   $parameterBag)
  15.     {
  16.         $this->parameterBag $parameterBag;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             AfterEntityDeletedEvent::class => ['deletePhysicalImage'],
  22.         ];
  23.     }
  24.     public function deletePhysicalImage(AfterEntityDeletedEvent $event)
  25.     {
  26.         $entity $event->getEntityInstance();
  27.         if (!($entity instanceof Realisation)) return;
  28.         $cover $this->parameterBag->get('kernel.project_dir') . '/public/uploads/realisations/' $entity->getCover();
  29.         if (!is_null($entity->getCover()) && !empty($entity->getCover()) && file_exists($cover)) unlink($cover);
  30.         
  31.         $avant $this->parameterBag->get('kernel.project_dir') . '/public/uploads/realisations/' $entity->getAvant();
  32.         if (!is_null($entity->getAvant()) && !empty($entity->getAvant()) && file_exists($avant)) unlink($avant);
  33.         
  34.         $apres $this->parameterBag->get('kernel.project_dir') . '/public/uploads/realisations/' $entity->getApres();
  35.         if (!is_null($entity->getApres()) && !empty($entity->getApres()) && file_exists($apres)) unlink($apres);
  36.         foreach ($entity->getPhotos() as $photo) {
  37.             $url $this->parameterBag->get('kernel.project_dir') . '/public/uploads/realisations/' $photo->getImage();
  38.             if (!is_null($photo->getImage()) && !empty($photo->getImage()) && file_exists($url)) unlink($url);
  39.         }
  40.         
  41.     }
  42. }