aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/News/UserNewsCreate.php
blob: 7580202504a59dd065cb8cc0770dc65d5c9bec02 (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
<?php

namespace JsonApi\Routes\News;

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

/**
 * Create a news where the range is the user himself.
 */
class UserNewsCreate extends AbstractNewsCreate
{
    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        $json = $this->validate($request);
        $observer = $this->getUser($request);
        if (!$user = \User::find($args['id'])) {
            throw new RecordNotFoundException();
        }
        if (!Authority::canCreateUserNews($observer, $user)) {
            throw new AuthorizationFailedException();
        }
        if (!$news = $this->createNewsFromJSON($observer, $user, $json)) {
            throw new InternalServerError('Could not create news.');
        }

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