src/OpenAI/EventSubscriber/OpenAIApiSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\OpenAI\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\Security\Core\Security;
  8. class OpenAIApiSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private readonly Security $security)
  11.     {
  12.     }
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             KernelEvents::REQUEST => 'onKernelRequest',
  17.         ];
  18.     }
  19.     public function onKernelRequest(RequestEvent $requestEvent): void
  20.     {
  21.         $request $requestEvent->getRequest();
  22.         $path $request->getPathInfo();
  23.         /** @var \App\Entity\Usuario $user */
  24.         $user $this->security->getUser();
  25.         if (\str_starts_with($path'/api/openai')) {
  26.            if (!$user){
  27.                 throw new \LogicException('You must be logged in to use the OpenAI API');
  28.            }
  29.            if (!$user->isVirtualAssistantActive()){
  30.                throw new \LogicException('You do not have the virtual assistant active');
  31.            }
  32.         }
  33.     }
  34. }