Exemplo n.º 1
0
 /**
  * Creates a new comment
  *
  * @param string $objectType e.g. "files"
  * @param string $objectId e.g. the file id
  * @param string $data JSON encoded string containing the properties of the tag to create
  * @param string $contentType content type of the data
  * @return IComment newly created comment
  *
  * @throws BadRequest if a field was missing
  * @throws UnsupportedMediaType if the content type is not supported
  */
 private function createComment($objectType, $objectId, $data, $contentType = 'application/json')
 {
     if (explode(';', $contentType)[0] === 'application/json') {
         $data = json_decode($data, true);
     } else {
         throw new UnsupportedMediaType();
     }
     $actorType = $data['actorType'];
     $actorId = null;
     if ($actorType === 'users') {
         $user = $this->userSession->getUser();
         if (!is_null($user)) {
             $actorId = $user->getUID();
         }
     }
     if (is_null($actorId)) {
         throw new BadRequest('Invalid actor "' . $actorType . '"');
     }
     try {
         $comment = $this->commentsManager->create($actorType, $actorId, $objectType, $objectId);
         $comment->setMessage($data['message']);
         $comment->setVerb($data['verb']);
         $this->commentsManager->save($comment);
         return $comment;
     } catch (\InvalidArgumentException $e) {
         throw new BadRequest('Invalid input values', 0, $e);
     }
 }