<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Client;
use App\Entity\Colis;
use App\Exception\EntiteNotFoundException;
use App\Service\FileUploader;
use App\Service\Utils;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Vich\UploaderBundle\Storage\StorageInterface;
class ClientSubscriber extends BaseEventSubscriber
{
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
['processClientDelete', EventPriorities::PRE_WRITE],
],
];
}
public function processClientDelete(ViewEvent $event){
$client = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$client instanceof Client || Request::METHOD_DELETE !== $method) {
return ;
}
$fs = new Filesystem();
try {
if($client->getPhotoRecto()){
$fs->remove($this->container->get('media_directory').'/'.$client->getPhotoRecto());
}
if($client->getPhotoVerso()){
$fs->remove($this->container->get('media_directory').'/'.$client->getPhotoVerso());
}
} catch (\Exception $e){
throw new EntiteNotFoundException($e->getMessage());
}
}
}