getUser($request), $news)) { throw new AuthorizationFailedException(); } if (!$comment = $this->createCommentFromJSON($user, $news, $this->validate($request))) { throw new InternalServerError('Could not create comment.'); } return $this->getCreatedResponse($comment); } protected function validateResourceDocument($json, $data) { if (empty(self::arrayGet($json, 'data.attributes.content'))) { return 'Comment should not be empty.'; } } protected function createCommentFromJSON($user, \StudipNews $news, array $json) { $content = self::arrayGet($json, 'data.attributes.content'); return $this->createComment($user, $news, $content); } protected function createComment($user, \StudipNews $news, $content) { $commentContent = \Studip\Markup::purifyHtml($content); $comment = new \StudipComment(); $comment->user_id = $user->id; $comment->content = $commentContent; $comment->object_id = $news->id; $comment->store(); return \StudipComment::find($comment->comment_id); } }