src/Core/Application/EventSubscriber/Chat/ChatInvitedUserToChannelSubscriber.php line 36

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Core\Application\EventSubscriber\Chat;
  4. use App\Core\Application\Event\Chat\ChatInvitedUserToChannelEvent;
  5. use App\Core\Application\Service\RocketChat\RocketChatClientServiceInterface;
  6. use App\Core\Domain\Entity\Student\Student;
  7. use App\Core\Domain\Entity\User\User;
  8. use App\Core\Domain\Repository\ChatSetting\ChatChannelRepositoryInterface;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ChatInvitedUserToChannelSubscriber implements EventSubscriberInterface
  12. {
  13.     private readonly RocketChatClientServiceInterface $rocketChatClientService;
  14.     private readonly EntityManagerInterface $em;
  15.     public function __construct(
  16.         EntityManagerInterface $em,
  17.         RocketChatClientServiceInterface $rocketChatClientService,
  18.         private readonly ChatChannelRepositoryInterface $chatChannelRepository
  19.     ) {
  20.         $this->rocketChatClientService $rocketChatClientService;
  21.         $this->em $em;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ChatInvitedUserToChannelEvent::class => 'inviteUserToChannel',
  27.         ];
  28.     }
  29.     public function inviteUserToChannel(ChatInvitedUserToChannelEvent $event): void
  30.     {
  31.         $channelId $event->channelId;
  32.         $user $event->user;
  33.         if ($user instanceof Student) {
  34.             $this->rocketChatClientService->inviteUserToChannel($user->getChatSetting()->getChatUserId(), $channelId);
  35.         }
  36.         if ($user instanceof User) {
  37.             $this->rocketChatClientService->inviteUserToChannel($user->getChatSetting()->getChatUserId(), $channelId);
  38.         }
  39.         $chatChannel $this->chatChannelRepository->findOneBy(['chat_channel_id' => $channelId]);
  40.         $user->getChatSetting()->addChatChannel($chatChannel);
  41.         $this->em->persist($user);
  42.     }
  43. }