src/EventSubscriber/NotificationSubscriber.php line 27
<?phpnamespace App\EventSubscriber;use ApiPlatform\Symfony\EventListener\EventPriorities;use ApiPlatform\Util\RequestAttributesExtractor;use App\Entity\Notification;use App\Event\NotificationCreatedEvent;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\KernelEvents;class NotificationSubscriber implements EventSubscriberInterface{public function __construct(private EventDispatcherInterface $dispatcher){}public static function getSubscribedEvents(): array{return [KernelEvents::VIEW => ['dispatchNotificationCreation', EventPriorities::PRE_WRITE],];}public function dispatchNotificationCreation(ViewEvent $event): void{$controllerResult = $event->getControllerResult();$request = $event->getRequest();if (!$controllerResult instanceof Notification|| !($attributes = RequestAttributesExtractor::extractAttributes($request))|| !$attributes['persist']|| 'POST' !== $request->getMethod()) {return;}$this->dispatcher->dispatch(new NotificationCreatedEvent($controllerResult));}}