aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Blubber/CommentsCreate.php
blob: 3548645bf35748a6cce6454cb87818057c998a60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

namespace JsonApi\Routes\Blubber;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\BadRequestException;
use JsonApi\Errors\InternalServerError;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;

use JsonApi\Routes\ValidationTrait;

/**
 * Create a new blubber comment.
 */
class CommentsCreate extends JsonApiController
{
    use ValidationTrait;

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        $json = $this->validate($request);

        if (!($thread = \BlubberThread::find($args['id']))) {
            throw new RecordNotFoundException();
        }

        if (!Authority::canCreateComment($user = $this->getUser($request), $thread)) {
            throw new AuthorizationFailedException();
        }

        $content = self::arrayGet($json, 'data.attributes.content');

        $comment = \BlubberComment::create([
            'thread_id' => $thread->id,
            'content' => $content,
            'user_id' => $user->id,
            'external_contact' => 0
        ]);

        return $this->getCreatedResponse($comment);
    }

    protected function validateResourceDocument($json, $data)
    {
        if (empty(self::arrayGet($json, 'data.attributes.content'))) {
            return 'Comment should not be empty.';
        }
    }
}