/** * Create a new room * * @param int $idUser The user creator id * @param string $roomName The room name * @param int $maxUsers The max room users * @param string $password The room password DEFAULT '' * * @throws Exception If the room name is empty * @throws Exception If the room name already exists * @throws Exception If the max number of users is lower than 2 * * @return bool True if the room was successfully created, false otherwise */ public function createRoom(int $idUser, string $roomName, int $maxUsers, string $password = '') : bool { $roomEntityManager = new RoomEntityManager(); $success = $roomEntityManager->createRoom($idUser, $roomName, $maxUsers, $password); if ($success && $this->roomCollection !== null) { $this->roomCollection->add($roomEntityManager->getEntity()); } return $success; }
/** * Get all the rooms from the database in the $entityCollection attribute * * @return RoomCollection All the rooms from the database */ public function getAllRooms() { $rooms = new RoomCollection(); $sqlMarks = 'SELECT * FROM %s'; $sql = static::sqlFormat($sqlMarks, (new Room())->getTableName()); foreach (DB::query($sql)->fetchAll() as $roomAttributes) { $rooms->add(new Room($roomAttributes)); } return $rooms; }
/** * 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); } }