src/Core/Application/EventSubscriber/Chat/ChatChannelCreatedSubscriber.php line 36
<?phpdeclare(strict_types=1);namespace App\Core\Application\EventSubscriber\Chat;use App\Admin\Domain\Repository\Course\CourseRepositoryInterface;use App\Core\Application\Event\Chat\ChatChannelCreatedEvent;use App\Core\Application\Service\RocketChat\RocketChatClientServiceInterface;use App\Core\Domain\Entity\ChatSetting\ChatChannel;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Uid\Uuid;class ChatChannelCreatedSubscriber implements EventSubscriberInterface{private readonly RocketChatClientServiceInterface $rocketChatClientService;private readonly EntityManagerInterface $em;public function __construct(EntityManagerInterface $em,RocketChatClientServiceInterface $rocketChatClientService,private readonly CourseRepositoryInterface $courseRepository) {$this->rocketChatClientService = $rocketChatClientService;$this->em = $em;}public static function getSubscribedEvents(): array{return [ChatChannelCreatedEvent::class => 'createChannel',];}public function createChannel(ChatChannelCreatedEvent $event): void{$courseId = $event->courseId;$course = $this->courseRepository->find(['id' => $courseId]);$channelId = $this->rocketChatClientService->createChannel($this->parseName($course->getName()));if (!$channelId) {throw new \RuntimeException('Nie udało się utworzyć kanału czatu w RocketChat.');}$chatChannel = new ChatChannel(Uuid::v4());$chatChannel->setCourse($course);$chatChannel->setChatChannelId($channelId);$this->em->persist($chatChannel);$this->em->flush();}private function parseName(string $input): string{$lowercase = mb_strtolower($input, 'UTF-8');$transliterated = iconv('UTF-8', 'ASCII//TRANSLIT', $lowercase);$cleaned = preg_replace('/[^a-z0-9]+/', '_', $transliterated);return trim($cleaned, '_');}}