/** * @param Message $message * @param User $from * * @return MessageValueObject */ private function populateMessageValueObject(Message $message, User $from = null) { if (null === $from) { $from = $message->getUser(); } if (null === $from) { throw new InvalidParameterException('You must provide the user who wrote the message'); } $from = $this->populateUserValueObject($from); $conversation = $message->getConversation()->getId(); return new MessageValueObject($message->getId(), $from, $conversation, $message->getText()); }
/** * @ParamConverter("conversation", class="MessengerBundle:Conversation") * * @param Conversation $conversation * * @return Conversation */ public function newMessageAction(Conversation $conversation, $jsonMessage) { $em = $this->getManager(); $stdMessage = json_decode($jsonMessage); $user = $em->getRepository(User::class)->find($stdMessage->user); if (null === $user) { throw new NotFoundHttpException('No user found'); } $message = new Message($stdMessage->text); $message->setType($stdMessage->type)->setUser($user); $conversation->addMessage($message); $em->persist($conversation); $em->flush(); return $conversation; }
/** * @param $message * @param $type * * @return Message * * @throws \HttpInvalidParamException */ private function handleMessageType(Message $message, $type) { if (Message::TYPE_TEXT_STRING === $type) { $message->setType(Message::TYPE_TEXT); } elseif (Message::TYPE_WIZZ_STRING === $type) { $message->setType(Message::TYPE_WIZZ); } elseif (null === $type) { $message->setType(Message::TYPE_TEXT); } else { throw new \HttpInvalidParamException('The type of message provided does not exist', 400); } return $message; }