src/EventSubscriber/LabelCreateEventSubscriber.php line 30
<?phpdeclare(strict_types=1);namespace App\EventSubscriber;use ApiPlatform\Core\EventListener\EventPriorities;use App\Entity\Label;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;final class LabelCreateEventSubscriber implements EventSubscriberInterface{public function __construct(private readonly TokenStorageInterface $tokenStorage){}public static function getSubscribedEvents(): array{return [KernelEvents::VIEW => ['createItemSequence', EventPriorities::PRE_WRITE]];}public function createItemSequence(ViewEvent $event): void{$entity = $event->getControllerResult();if (!$entity instanceof Label||$event->getRequest()->getMethod() !== Request::METHOD_POST) {return;}$user = $this->tokenStorage->getToken()->getUser();if (null === $user) {throw new NotFoundHttpException();}$entity->setInstitution($user->getCurrentInstitution());}}