Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ClassMetadataSearchRequestValidator
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 validate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 validateRequired
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 validateMaxListSize
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2020 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22declare(strict_types=1);
23
24namespace oat\tao\model\Lists\Presentation\Web\RequestValidator;
25
26use common_exception_BadRequest as BadRequestException;
27use oat\tao\model\Lists\Presentation\Web\RequestHandler\ClassMetadataSearchRequestHandler;
28use oat\tao\model\service\InjectionAwareService;
29use Psr\Http\Message\ServerRequestInterface;
30
31class ClassMetadataSearchRequestValidator extends InjectionAwareService
32{
33    public const SERVICE_ID = 'tao/ClassMetadataSearchRequestValidator';
34
35    private const REQUIRED_QUERY_PARAMETERS = [
36        ClassMetadataSearchRequestHandler::QUERY_CLASS_ID,
37    ];
38
39    /**
40     * @param ServerRequestInterface $request
41     *
42     * @throws BadRequestException
43     */
44    public function validate(ServerRequestInterface $request): void
45    {
46        $this->validateRequired($request);
47        $this->validateMaxListSize($request);
48    }
49
50    /**
51     * @param ServerRequestInterface $request
52     *
53     * @throws BadRequestException
54     */
55    private function validateRequired(ServerRequestInterface $request): void
56    {
57        $missingQueryParameters = array_diff_key(
58            array_flip(self::REQUIRED_QUERY_PARAMETERS),
59            $request->getQueryParams()
60        );
61
62        if ($missingQueryParameters) {
63            throw new BadRequestException(
64                sprintf(
65                    'The following query parameters must be provided: "%s".',
66                    implode('", "', array_keys($missingQueryParameters))
67                )
68            );
69        }
70    }
71
72    /**
73     * @param ServerRequestInterface $request
74     *
75     * @throws BadRequestException
76     */
77    private function validateMaxListSize(ServerRequestInterface $request): void
78    {
79        $queryParameters = $request->getQueryParams();
80
81        if (!isset($queryParameters[ClassMetadataSearchRequestHandler::QUERY_MAX_LIST_SIZE])) {
82            return;
83        }
84
85        $maxListSize = $queryParameters[ClassMetadataSearchRequestHandler::QUERY_MAX_LIST_SIZE];
86
87        if ((int)$maxListSize <= 0) {
88            throw new BadRequestException(
89                sprintf(
90                    'The parameter %s should be a positive integer, got: "%s".',
91                    ClassMetadataSearchRequestHandler::QUERY_MAX_LIST_SIZE,
92                    $maxListSize
93                )
94            );
95        }
96    }
97}