aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Files/SubfoldersIndex.php
blob: e8f4d133f49396731edff63c4a61e4491014f95a (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
<?php

namespace JsonApi\Routes\Files;

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;

class SubfoldersIndex extends JsonApiController
{
    protected $allowedIncludePaths = ['owner', 'parent', 'range', 'folders', 'file-refs'];

    protected $allowedPagingParameters = ['offset', 'limit'];

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

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

        $subfolders = array_map(
            function ($subfolder) {
                return $subfolder->getTypedFolder();
            },
            $folder->subfolders->getArrayCopy()
        );
        list($offset, $limit) = $this->getOffsetAndLimit();

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