From 6097eb20aa8e593f64ecf5494c3c43069337321f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 24 May 2022 08:17:26 +0200 Subject: Move setDescription to room service Signed-off-by: Joas Schilling --- lib/Command/Room/TRoomCommand.php | 2 +- lib/Controller/RoomController.php | 2 +- lib/Room.php | 36 ++++-------------------------------- lib/Service/RoomService.php | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 34 deletions(-) (limited to 'lib') diff --git a/lib/Command/Room/TRoomCommand.php b/lib/Command/Room/TRoomCommand.php index 07b31bc5e..280f96b8d 100644 --- a/lib/Command/Room/TRoomCommand.php +++ b/lib/Command/Room/TRoomCommand.php @@ -114,7 +114,7 @@ trait TRoomCommand { */ protected function setRoomDescription(Room $room, string $description): void { try { - $room->setDescription($description); + $this->roomService->setDescription($room, $description); } catch (\LengthException $e) { throw new InvalidArgumentException('Invalid room description.'); } diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index 7eba275b1..3d5e542ed 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -876,7 +876,7 @@ class RoomController extends AEnvironmentAwareController { } try { - $this->room->setDescription($description); + $this->roomService->setDescription($this->room, $description); } catch (\LengthException $exception) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } diff --git a/lib/Room.php b/lib/Room.php index 49f6ca324..a965dda8c 100644 --- a/lib/Room.php +++ b/lib/Room.php @@ -359,6 +359,10 @@ class Room { return $this->description; } + public function setDescription(string $description): void { + $this->description = $description; + } + /** * @deprecated Use ParticipantService::getGuestCount() instead * @return int @@ -688,38 +692,6 @@ class Room { return true; } - /** - * @param string $description - * @return bool True when the change was valid, false otherwise - * @throws \LengthException when the given description is too long - */ - public function setDescription(string $description): bool { - $description = trim($description); - - if (mb_strlen($description) > self::DESCRIPTION_MAXIMUM_LENGTH) { - throw new \LengthException('Conversation description is limited to ' . self::DESCRIPTION_MAXIMUM_LENGTH . ' characters'); - } - - $oldDescription = $this->getDescription(); - if ($description === $oldDescription) { - return false; - } - - $event = new ModifyRoomEvent($this, 'description', $description, $oldDescription); - $this->dispatcher->dispatch(self::EVENT_BEFORE_DESCRIPTION_SET, $event); - - $update = $this->db->getQueryBuilder(); - $update->update('talk_rooms') - ->set('description', $update->createNamedParameter($description)) - ->where($update->expr()->eq('id', $update->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))); - $update->executeStatement(); - $this->description = $description; - - $this->dispatcher->dispatch(self::EVENT_AFTER_DESCRIPTION_SET, $event); - - return true; - } - /** * @param string $password Currently it is only allowed to have a password for Room::TYPE_PUBLIC * @return bool True when the change was valid, false otherwise diff --git a/lib/Service/RoomService.php b/lib/Service/RoomService.php index 0c9805d44..ca5f8cb8a 100644 --- a/lib/Service/RoomService.php +++ b/lib/Service/RoomService.php @@ -440,6 +440,39 @@ class RoomService { return $updated; } + /** + * @param string $description + * @return bool True when the change was valid, false otherwise + * @throws \LengthException when the given description is too long + */ + public function setDescription(Room $room, string $description): bool { + $description = trim($description); + + if (mb_strlen($description) > Room::DESCRIPTION_MAXIMUM_LENGTH) { + throw new \LengthException('Conversation description is limited to ' . Room::DESCRIPTION_MAXIMUM_LENGTH . ' characters'); + } + + $oldDescription = $room->getDescription(); + if ($description === $oldDescription) { + return false; + } + + $event = new ModifyRoomEvent($room, 'description', $description, $oldDescription); + $this->dispatcher->dispatch(Room::EVENT_BEFORE_DESCRIPTION_SET, $event); + + $update = $this->db->getQueryBuilder(); + $update->update('talk_rooms') + ->set('description', $update->createNamedParameter($description)) + ->where($update->expr()->eq('id', $update->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT))); + $update->executeStatement(); + + $room->setDescription($description); + + $this->dispatcher->dispatch(Room::EVENT_AFTER_DESCRIPTION_SET, $event); + + return true; + } + public function verifyPassword(Room $room, string $password): array { $event = new VerifyRoomPasswordEvent($room, $password); $this->dispatcher->dispatch(Room::EVENT_PASSWORD_VERIFY, $event); -- cgit v1.2.3