aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Feedback/FeedbackEntriesIndex.php
blob: 4c570bf38efc1d193ee7fcbadfe671395c6d0313 (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
44
45
46
<?php

namespace JsonApi\Routes\Feedback;

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;

/**
 * Displays the entries of a certain feedback element.
 */
class FeedbackEntriesIndex extends JsonApiController
{
    protected $allowedIncludePaths = ['author', 'feedback-element'];
    protected $allowedPagingParameters = ['offset', 'limit'];

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

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

        /** @var \SimpleOrMapCollection */
        $entries = $resource->entries;

        if (!Authority::canSeeResultsOfFeedbackElement($user, $resource)) {
            $entries = $entries->filter(function ($entry) use ($user) {
                return $entry->user_id === $user->id;
            });
        }

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

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