aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/News/ByUserIndex.php
blob: 3d1574d66438844b96066a064de2b76956b5221b (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
<?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\RecordNotFoundException;
use JsonApi\JsonApiController;

/*
* Get all news of a user
*/
class ByUserIndex extends JsonApiController
{
    protected $allowedPagingParameters = ['offset', 'limit'];
    protected $allowedIncludePaths = ['author', 'ranges'];

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        if (!$observedUser = \User::find($args['id'])) {
            throw new RecordNotFoundException();
        }

        if (!Authority::canIndexNewsOfUser($this->getUser($request), $observedUser)) {
            throw new AuthorizationFailedException();
        }

        $news = \StudipNews::GetNewsByAuthor($args['id'], true);

        list($offset, $limit) = $this->getOffsetAndLimit();

        return $this->getPaginatedContentResponse(
            array_slice($news, $offset, $limit),
            count($news)
        );
    }
}