aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/InstituteMemberships/ByUserIndex.php
blob: 2fd98332c8b37c229a1693d68fa75d348318b58b (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
<?php

namespace JsonApi\Routes\InstituteMemberships;

use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use JsonApi\Routes\Institutes\Authority;

class ByUserIndex extends JsonApiController
{
    protected $allowedIncludePaths = ['user', 'institute'];

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

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

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

        $institutes = $user->institute_memberships;
        $total = count($institutes);
        list($offset, $limit) = $this->getOffsetAndLimit();

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