/** * Add message to conversation * * @param MAILBOX_BOL_Conversation $conversation * @param int $senderId * @param string $text * @throws InvalidArgumentException */ public function addMessage(MAILBOX_BOL_Conversation $conversation, $senderId, $text) { if (empty($senderId)) { throw new InvalidArgumentException("Not numeric params were provided! Numbers are expected!"); } if ($conversation === null) { throw new InvalidArgumentException("Conversation doesn't exist!"); } if (empty($conversation->id)) { throw new InvalidArgumentException("Conversation with id = " . $conversation->id . " is not exist"); } if (!in_array($senderId, array($conversation->initiatorId, $conversation->interlocutorId))) { throw new InvalidArgumentException("Wrong senderId!"); } $senderId = (int) $senderId; $recipientId = $senderId == $conversation->initiatorId ? $conversation->interlocutorId : $conversation->initiatorId; $text = trim($text); if (empty($text)) { throw new InvalidArgumentException("Empty string params were provided!"); } // create message $message = new MAILBOX_BOL_Message(); $message->conversationId = $conversation->id; $message->senderId = $senderId; $message->recipientId = $recipientId; $message->text = $text; $message->timeStamp = time(); $this->messageDao->save($message); // insert record into LastMessage table $lastMessage = $this->lastMessageDao->findByConversationId($conversation->id); if ($lastMessage === null) { $lastMessage = new MAILBOX_BOL_LastMessage(); $lastMessage->conversationId = $conversation->id; $lastMessage->initiatorMessageId = $message->id; } else { switch ($senderId) { case $conversation->initiatorId: $unReadBy = MAILBOX_BOL_ConversationDao::READ_INTERLOCUTOR; $readBy = MAILBOX_BOL_ConversationDao::READ_INITIATOR; $unDeletedBy = MAILBOX_BOL_ConversationDao::DELETED_INTERLOCUTOR; $consoleViewed = MAILBOX_BOL_ConversationDao::VIEW_INITIATOR; $lastMessage->initiatorMessageId = $message->id; break; case $conversation->interlocutorId: $unReadBy = MAILBOX_BOL_ConversationDao::READ_INITIATOR; $readBy = MAILBOX_BOL_ConversationDao::READ_INTERLOCUTOR; $unDeletedBy = MAILBOX_BOL_ConversationDao::DELETED_INITIATOR; $lastMessage->interlocutorMessageId = $message->id; $consoleViewed = MAILBOX_BOL_ConversationDao::VIEW_INTERLOCUTOR; break; } $conversation->deleted = (int) $conversation->deleted & ~$unDeletedBy; $conversation->read = (int) $conversation->read & ~$unReadBy | $readBy; $conversation->viewed = $consoleViewed; $conversation->notificationSent = 0; $this->conversationDao->save($conversation); } $this->lastMessageDao->save($lastMessage); return $message; }