aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/ConfigValues/ByUserIndex.php
blob: 84bfed553eaed027bcd9e7c01efbb061293c3fbe (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
<?php

namespace JsonApi\Routes\ConfigValues;

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;

class ByUserIndex extends JsonApiController
{
    use HelperTrait;

    protected $allowedFilteringParameters = ['field'];

    protected $allowedIncludePaths = [];

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        if (!($range = \User::find($args['id']))) {
            throw new RecordNotFoundException();
        }
        if (!Authority::canShowConfigValue($this->getUser($request), $range)) {
            throw new AuthorizationFailedException();
        }

        $configuration = $range->getConfiguration();
        $fields = $configuration->getFields($range->getRangeType());

        $filtering = $this->getQueryParameters()->getFilteringParameters() ?? [];
        if (array_key_exists('field', $filtering)) {
            $fields = array_filter($fields, function ($field) use ($filtering) {
                return $field === $filtering['field'];
            });
        }

        return $this->getContentResponse(array_map(function ($field) use ($range) {
            return $this->findOrFakeConfigValue($range, $field);
        }, $fields));
    }
}