<?PHP
# src/EventSubscriber/EasyAdminSubscriber.php
namespace App\EventSubscriber;
use App\Entity\Realisation;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
/**
* @var ParameterBagInterface
*/
private $parameterBag;
public function __construct(ParameterBagInterface $parameterBag)
{
$this->parameterBag = $parameterBag;
}
public static function getSubscribedEvents()
{
return [
AfterEntityDeletedEvent::class => ['deletePhysicalImage'],
];
}
public function deletePhysicalImage(AfterEntityDeletedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Realisation)) return;
$cover = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/realisations/' . $entity->getCover();
if (!is_null($entity->getCover()) && !empty($entity->getCover()) && file_exists($cover)) unlink($cover);
$avant = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/realisations/' . $entity->getAvant();
if (!is_null($entity->getAvant()) && !empty($entity->getAvant()) && file_exists($avant)) unlink($avant);
$apres = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/realisations/' . $entity->getApres();
if (!is_null($entity->getApres()) && !empty($entity->getApres()) && file_exists($apres)) unlink($apres);
foreach ($entity->getPhotos() as $photo) {
$url = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/realisations/' . $photo->getImage();
if (!is_null($photo->getImage()) && !empty($photo->getImage()) && file_exists($url)) unlink($url);
}
}
}