src/EventSubscriber/ServiceSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\Api\IriConverterInterface;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Constant\ApiConstants;
  6. use App\Entity\Client;
  7. use App\Entity\Colis;
  8. use App\Entity\Service;
  9. use App\Exception\EntiteNotFoundException;
  10. use App\Service\FileUploader;
  11. use App\Service\Utils;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Filesystem\Filesystem;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Event\ViewEvent;
  19. use Symfony\Component\HttpKernel\Event\RequestEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Vich\UploaderBundle\Storage\StorageInterface;
  22. class ServiceSubscriber extends BaseEventSubscriber
  23. {
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             KernelEvents::VIEW => [
  28.                 ['processServiceDelete'EventPriorities::PRE_WRITE]
  29.             ],
  30.         ];
  31.     }
  32.     public function processServiceDelete(ViewEvent $event){
  33.         $service $event->getControllerResult();
  34.         $method $event->getRequest()->getMethod();
  35.         if (!$service instanceof Service || Request::METHOD_DELETE !== $method) {
  36.             return ;
  37.         }
  38.         $fs = new Filesystem();
  39.         try {
  40.             if($service->getLogo()){
  41.                 $fs->remove($this->container->get('service_directory').'/'.$service->getLogo());
  42.             }
  43.         } catch (\Exception $e){
  44.             throw new EntiteNotFoundException($e->getMessage());
  45.         }
  46.     }
  47. }