/**
  * Constructor the initialize the log function, the chat service handler and the chat service pipe
  */
 public function __construct()
 {
     $this->rooms = new RoomCollection();
     $this->clients = new ClientCollection();
     $this->logger = new Logger([Logger::CONSOLE]);
     $this->services['chatService'] = new ChatService();
     $this->services['roomService'] = new RoomService();
     $this->services['clientService'] = new ClientService();
     // Get all the rooms on server start
     $roomManager = new RoomManager();
     $this->rooms = $roomManager->getAllRooms();
 }
Example #2
0
 /**
  * Insert room data in database
  */
 private function insertRoomData()
 {
     $rooms = new RoomCollection();
     $roomManager = new RoomManager(null, $rooms);
     static::out('Create room data' . PHP_EOL);
     // Create a default chat room
     $default = new Room(['id' => 1, 'name' => 'Default', 'creator' => 1, 'creationDate' => new \DateTime(), 'maxUsers' => 50]);
     $rooms->add($default);
     // Create some rooms some public and some with password 123
     for ($i = 1; $i < 11; $i++) {
         $room = new Room(['id' => $i + 1, 'name' => 'Room ' . $i, 'creator' => 1, 'password' => mt_rand(0, 1) ? null : '123', 'creationDate' => new \DateTime(), 'maxUsers' => 20]);
         $rooms->add($room);
     }
     if ($roomManager->saveRoomCollection()) {
         static::ok(sprintf('The followings chat rooms are inserted %s' . PHP_EOL, $rooms));
     } else {
         static::fail('An error occurred on chat rooms collection save' . PHP_EOL);
     }
 }
 /**
  * Update a user right in a room
  *
  * @param      array           $data    JSON decoded client data
  * @param      Client          $client  The client calling the request
  * @param      RoomCollection  $rooms   The actives rooms
  *
  * @return     \Generator
  *
  * @todo       to test
  */
 private function updateUserRight(array $data, Client $client, RoomCollection $rooms)
 {
     $success = false;
     $message = _('User right updated');
     $roomManager = new RoomManager(null, $rooms);
     $room = null;
     if (!is_numeric($data['roomId'] ?? null) && !$roomManager->isRoomExist((int) $data['roomId'])) {
         $message = _('This room does not exist');
     } else {
         $roomManager->loadRoomFromCollection((int) $data['roomId']);
         $room = $roomManager->getRoom();
         if (!$client->isRegistered()) {
             $message = _('You are not registered so you cannot update the room information');
         } elseif (!$roomManager->isClientInTheRoom($client)) {
             $message = _('You are not logged in this room');
         } elseif (!$roomManager->hasGrantRight($client)) {
             $message = _('You do not have the right to grant a user right in this room');
         } elseif ($room->getClients()->getObjectById($data['clientId'] ?? -1) === null) {
             $message = _('This client does not exists in this room');
         } else {
             try {
                 $success = $roomManager->updateRoomRight($room->getClients()->getObjectById($data['clientId']), $data['rightName'] ?? '', (bool) $data['rightValue'] ?? false);
                 if ($success) {
                     (yield $this->changeUserRight($room, $room->getClients()->getObjectById($data['clientId'])));
                 }
             } catch (Exception $e) {
                 $message = $e->getMessage();
             }
         }
     }
     (yield $client->getConnection()->send(json_encode(['service' => $this->serviceName, 'action' => 'updateUserRight', 'success' => $success, 'text' => $message])));
 }
 /**
  * Get the next chat conversation historic part of a room
  *
  * @param      array             $data     JSON decoded client data
  * @param      Client            $client   The client object
  *
  * @return     \Generator
  *
  * @todo To test
  */
 private function getHistoric(array $data, Client $client)
 {
     $success = false;
     $message = _('Historic successfully loaded !');
     $roomManager = new RoomManager();
     if (!is_numeric($data['roomId'] ?? null) && !$roomManager->isRoomExist((int) $data['roomId'])) {
         $message = _('This room does not exist');
     } else {
         if (!$roomManager->isClientInTheRoom($client)) {
             $message = _('You are not logged in this room');
         } else {
             $success = true;
             $historic = $this->getRoomHistoric($roomManager->getRoom()->id, $client, $data['lastMessageDate']);
         }
     }
     (yield $client->getConnection()->send(json_encode(['service' => $this->serviceName, 'action' => 'getHistoric', 'success' => $success, 'text' => $message, 'historic' => $historic ?? [], 'roomId' => $data['roomId']])));
 }