src/EventSubscriber/MuestraInSesionSubscriber.php line 24
<?phpnamespace App\EventSubscriber;use ApiPlatform\Symfony\EventListener\EventPriorities;use App\Entity\Muestra;use App\Exception\ObjectInUseException;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\KernelEvents;final class MuestraInSesionSubscriber implements EventSubscriberInterface{public function __construct(){}public static function getSubscribedEvents(){return [KernelEvents::VIEW => ['checkSesion', EventPriorities::PRE_WRITE],];}public function checkSesion(ViewEvent $event): void{$muestra = $event->getControllerResult();$method = $event->getRequest()->getMethod();if (!$muestra instanceof Muestra || Request::METHOD_DELETE !== $method) {return;}if ($muestra->getSesion() !== null && $muestra->isOriginal()) {throw new ObjectInUseException(sprintf('La muestra "%s" no se puede eliminar porque pertenence a la sesion "%s"', $muestra->getNombre(), $muestra->getSesion()->getNombre()));}}}