aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Forum/PostingDelete.php
blob: 64225b3aa9636434b6a2a4f169af62888dfacf1c (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
<?php
namespace JsonApi\Routes\Forum;

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

class PostingDelete extends JsonApiController
{
    public function __invoke(Request $request, Response $response, $args)
    {
        $posting = Posting::find($args['posting_id']);
        if (!$posting) {
            throw new RecordNotFoundException();
        }

        if (
            !Authority::canDeletePost($this->getUser($request), $posting, (bool) $posting->discussion->closed_at)
        ) {
            throw new AuthorizationFailedException();
        }

        $posting->delete();

        return $this->getCodeResponse(204);
    }
}