src/EventSubscriber/AgenceSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Agence;
  5. use App\Exception\EntiteNotFoundException;
  6. use Symfony\Component\Filesystem\Filesystem;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\ViewEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. class AgenceSubscriber extends BaseEventSubscriber
  11. {
  12.     public static function getSubscribedEvents()
  13.     {
  14.         return [
  15.             KernelEvents::VIEW => [
  16.                 ['processAgenceDelete'EventPriorities::PRE_WRITE],
  17.             ],
  18.         ];
  19.     }
  20.     public function processAgenceDelete(ViewEvent $event){
  21.         $agence $event->getControllerResult();
  22.         $method $event->getRequest()->getMethod();
  23.         if (!$agence instanceof Agence || Request::METHOD_DELETE !== $method) {
  24.             return ;
  25.         }
  26.         $fs = new Filesystem();
  27.         try {
  28.             if($agence->getLogo()){
  29.                 $fs->remove($this->container->get('agence_directory').'/'.$agence->getLogofilename());
  30.             }
  31.         } catch (\Exception $e){
  32.             throw new EntiteNotFoundException($e->getMessage());
  33.         }
  34.     }
  35. }