src/JarvisAI/EventSubscriber/UserAILogSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\JarvisAI\EventSubscriber;
  3. use App\JarvisAI\Event\UserAILogEvent;
  4. use App\JarvisAI\Repository\UserAILogRepository;
  5. use AppBundle\Entity\User;
  6. use Doctrine\ORM\OptimisticLockException;
  7. use Doctrine\ORM\Exception\ORMException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. class UserAILogSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var UserAiLogRepository
  14.      */
  15.     private $repository;
  16.     /**
  17.      * @var TokenStorageInterface
  18.      */
  19.     private $tokenStorage;
  20.     /**
  21.      * @param UserAiLogRepository $repository
  22.      * @param TokenStorageInterface $tokenStorage
  23.      */
  24.     public function __construct(UserAiLogRepository $repositoryTokenStorageInterface $tokenStorage)
  25.     {
  26.         $this->repository $repository;
  27.         $this->tokenStorage $tokenStorage;
  28.     }
  29.     /**
  30.      * @return string[]
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             UserAILogEvent::NAME => 'onUserAILog',
  36.         ];
  37.     }
  38.     /**
  39.      * @param UserAILogEvent $event
  40.      * @return void
  41.      * @throws ORMException
  42.      * @throws OptimisticLockException
  43.      */
  44.     public function onUserAILog(UserAILogEvent $event): void
  45.     {
  46.         /** @var User $user */
  47.         $user $this->tokenStorage->getToken()->getUser();
  48.         $this->repository->create($user$event->getModel());
  49.     }
  50. }