src/EventSubscriber/ClientSubscriber.php line 36

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\Entity\Client;
  6. use App\Entity\Colis;
  7. use App\Exception\EntiteNotFoundException;
  8. use App\Service\FileUploader;
  9. use App\Service\Utils;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Filesystem\Filesystem;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\ViewEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Vich\UploaderBundle\Storage\StorageInterface;
  19. class ClientSubscriber extends BaseEventSubscriber
  20. {
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             KernelEvents::VIEW => [
  25.                 ['processClientDelete'EventPriorities::PRE_WRITE],
  26.             ],
  27.         ];
  28.     }
  29.     public function processClientDelete(ViewEvent $event){
  30.         $client $event->getControllerResult();
  31.         $method $event->getRequest()->getMethod();
  32.         if (!$client instanceof Client || Request::METHOD_DELETE !== $method) {
  33.             return ;
  34.         }
  35.         $fs = new Filesystem();
  36.         try {
  37.             if($client->getPhotoRecto()){
  38.                 $fs->remove($this->container->get('media_directory').'/'.$client->getPhotoRecto());
  39.             }
  40.             if($client->getPhotoVerso()){
  41.                 $fs->remove($this->container->get('media_directory').'/'.$client->getPhotoVerso());
  42.             }
  43.         } catch (\Exception $e){
  44.             throw new EntiteNotFoundException($e->getMessage());
  45.         }
  46.     }
  47. }