src/Core/Application/EventSubscriber/Chat/SetUserChatSubscriber.php line 41

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Core\Application\EventSubscriber\Chat;
  4. use App\Core\Application\Event\Chat\ChatUserPersonalTokenGeneratedEvent;
  5. use App\Core\Application\Event\Chat\SetUserChatEvent;
  6. use App\Core\Application\Service\RocketChat\RocketChatClientServiceInterface;
  7. use App\Core\Domain\Entity\ChatSetting\ChatSetting;
  8. use App\Core\Domain\Entity\ChatSetting\ChatUserInterface;
  9. use App\Core\Domain\Entity\Student\Student;
  10. use App\Core\Domain\Entity\User\User;
  11. use App\Core\Domain\Repository\ChatSetting\ChatChannelRepositoryInterface;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Uid\Uuid;
  15. class SetUserChatSubscriber implements EventSubscriberInterface
  16. {
  17.     private readonly EntityManagerInterface $em;
  18.     private readonly RocketChatClientServiceInterface $rocketChatClientService;
  19.     public function __construct(
  20.         EntityManagerInterface $em,
  21.         RocketChatClientServiceInterface $rocketChatClientService,
  22.         private readonly  ChatChannelRepositoryInterface $chatChannelRepository
  23.     )
  24.     {
  25.         $this->em $em;
  26.         $this->rocketChatClientService $rocketChatClientService;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             SetUserChatEvent::class => 'setUserToChat',
  32.         ];
  33.     }
  34.     public function setUserToChat(SetUserChatEvent $event): void
  35.     {
  36.         $user $event->user;
  37.         $dto $event->dto;
  38.         $channelId $event->chatChannelId;
  39.         $userId $this->rocketChatClientService->createUser($dto);
  40.         $chatSetting = new ChatSetting(Uuid::v4());
  41.         $chatSetting->setChatUserId($userId);
  42.         if ($user instanceof Student) {
  43.             $user->setChatSetting($chatSetting);
  44.             $this->addUserToChannel($channelId$user);
  45.             $this->generateToken($user);
  46.         }
  47.         if ($user instanceof User) {
  48.             $user->setChatSetting($chatSetting);
  49.             $this->addUserToChannel($channelId$user);
  50.             $this->generateToken($user);
  51.         }
  52.         $this->em->flush();
  53.     }
  54.     private function addUserToChannel(string $channelIdChatUserInterface $user): void
  55.     {
  56.         $chatChannel $this->chatChannelRepository->findOneBy(['chatChannelId' => $channelId]);
  57.         if ($user instanceof Student) {
  58.             $this->rocketChatClientService->inviteUserToChannel($user->getChatSetting()->getChatUserId(), $channelId);
  59.         }
  60.         if ($user instanceof User) {
  61.             $this->rocketChatClientService->inviteUserToChannel($user->getChatSetting()->getChatUserId(), $channelId);
  62.         }
  63.         $user->getChatSetting()->addChatChannel($chatChannel);
  64.         $this->em->persist($user);
  65.     }
  66.     private function generateToken(ChatUserInterface $user): void
  67.     {
  68.         $token '';
  69.         if ($user instanceof Student) {
  70.             $token $this->rocketChatClientService->createToken($user->getChatSetting()->getChatUserId());
  71.         }
  72.         if ($user instanceof User) {
  73.             $token $this->rocketChatClientService->createToken($user->getChatSetting()->getChatUserId());
  74.         }
  75.         $user->getChatSetting()->setToken($token);
  76.     }
  77. }