src/Core/Application/EventSubscriber/Chat/ChatUserTokenGeneratedSubscriber.php line 30

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Core\Application\EventSubscriber\Chat;
  4. use App\Core\Application\Event\Chat\ChatUserTokenGeneratedEvent;
  5. use App\Core\Application\Service\RocketChat\RocketChatClientServiceInterface;
  6. use App\Core\Domain\Entity\ChatSetting\ChatUserInterface;
  7. use App\Core\Domain\Entity\Student\Student;
  8. use App\Core\Domain\Entity\User\User;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ChatUserTokenGeneratedSubscriber implements EventSubscriberInterface
  11. {
  12.     private readonly RocketChatClientServiceInterface $rocketChatClientService;
  13.     public function __construct(RocketChatClientServiceInterface $rocketChatClientService)
  14.     {
  15.         $this->rocketChatClientService $rocketChatClientService;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             ChatUserTokenGeneratedEvent::class => 'createToken',
  21.         ];
  22.     }
  23.     public function createToken(ChatUserTokenGeneratedEvent $event): void
  24.     {
  25.         $user $event->user;
  26.         $token '';
  27.         if ($user instanceof Student) {
  28.             $token $this->rocketChatClientService->createToken($user->getChatSetting()->getChatUserId());
  29.         }
  30.         if ($user instanceof User) {
  31.             $token $this->rocketChatClientService->createToken($user->getChatSetting()->getChatUserId());
  32.         }
  33.         $user->getChatSetting()->setToken($token);
  34.     }
  35. }