aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Wiki/DescendantsIndex.php
blob: 2049c79fe9847ce1ef68f532217ed61d6be35045 (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\Wiki;

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

/*
 * Get paginated list of all descendants of a page
 */
class DescendantsIndex extends JsonApiController
{
    use HelperTrait;

    protected $allowedPagingParameters = ['offset', 'limit'];
    protected $allowedIncludePaths = ['author', 'children', 'descendants', 'parent', 'range'];

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        $wikiPage = self::findWikiPage($args['id']);

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

        $descendants = $wikiPage->getDescendants();
        $total = count($descendants);

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

        return $this->getPaginatedContentResponse(
            array_slice($descendants, $offset, $limit),
            $total
        );
    }
}