<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Agence;
use App\Exception\EntiteNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class AgenceSubscriber extends BaseEventSubscriber
{
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
['processAgenceDelete', EventPriorities::PRE_WRITE],
],
];
}
public function processAgenceDelete(ViewEvent $event){
$agence = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$agence instanceof Agence || Request::METHOD_DELETE !== $method) {
return ;
}
$fs = new Filesystem();
try {
if($agence->getLogo()){
$fs->remove($this->container->get('agence_directory').'/'.$agence->getLogofilename());
}
} catch (\Exception $e){
throw new EntiteNotFoundException($e->getMessage());
}
}
}