Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
47 / 47 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| ValueCollectionSearchRequestValidator | |
100.00% |
47 / 47 |
|
100.00% |
6 / 6 |
13 | |
100.00% |
1 / 1 |
| validate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| validateRequired | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| validateSubject | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| validateExclude | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| validateExcludeElements | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| createBadTypeException | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\Lists\Presentation\Web\RequestValidator; |
| 26 | |
| 27 | use common_exception_BadRequest as BadRequestException; |
| 28 | use oat\tao\model\Lists\Presentation\Web\RequestHandler\ValueCollectionSearchRequestHandler; |
| 29 | use oat\tao\model\service\InjectionAwareService; |
| 30 | use Psr\Http\Message\ServerRequestInterface; |
| 31 | |
| 32 | class ValueCollectionSearchRequestValidator extends InjectionAwareService |
| 33 | { |
| 34 | public const SERVICE_ID = 'tao/ValueCollectionSearchRequestValidator'; |
| 35 | |
| 36 | private const REQUIRED_QUERY_PARAMETERS = [ |
| 37 | ValueCollectionSearchRequestHandler::QUERY_PARAMETER_ID, |
| 38 | ]; |
| 39 | |
| 40 | /** |
| 41 | * @param ServerRequestInterface $request |
| 42 | * |
| 43 | * @throws BadRequestException |
| 44 | */ |
| 45 | public function validate(ServerRequestInterface $request): void |
| 46 | { |
| 47 | $this->validateRequired($request); |
| 48 | $this->validateSubject($request); |
| 49 | $this->validateExclude($request); |
| 50 | $this->validateExcludeElements($request); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param ServerRequestInterface $request |
| 55 | * |
| 56 | * @throws BadRequestException |
| 57 | */ |
| 58 | private function validateRequired(ServerRequestInterface $request): void |
| 59 | { |
| 60 | $missingQueryParameters = array_diff_key( |
| 61 | array_flip(self::REQUIRED_QUERY_PARAMETERS), |
| 62 | $request->getQueryParams() |
| 63 | ); |
| 64 | |
| 65 | if ($missingQueryParameters) { |
| 66 | throw new BadRequestException( |
| 67 | sprintf( |
| 68 | 'The following query parameters must be provided: "%s".', |
| 69 | implode('", "', array_keys($missingQueryParameters)) |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param ServerRequestInterface $request |
| 77 | * |
| 78 | * @throws BadRequestException |
| 79 | */ |
| 80 | private function validateSubject(ServerRequestInterface $request): void |
| 81 | { |
| 82 | $queryParameters = $request->getQueryParams(); |
| 83 | |
| 84 | if ( |
| 85 | isset($queryParameters[ValueCollectionSearchRequestHandler::QUERY_PARAMETER_SUBJECT]) |
| 86 | && !is_string($queryParameters[ValueCollectionSearchRequestHandler::QUERY_PARAMETER_SUBJECT]) |
| 87 | ) { |
| 88 | throw $this->createBadTypeException( |
| 89 | ValueCollectionSearchRequestHandler::QUERY_PARAMETER_SUBJECT, |
| 90 | $queryParameters[ValueCollectionSearchRequestHandler::QUERY_PARAMETER_SUBJECT], |
| 91 | 'string' |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param ServerRequestInterface $request |
| 98 | * |
| 99 | * @throws BadRequestException |
| 100 | */ |
| 101 | private function validateExclude(ServerRequestInterface $request): void |
| 102 | { |
| 103 | $queryParameters = $request->getQueryParams(); |
| 104 | |
| 105 | if ( |
| 106 | isset($queryParameters[ValueCollectionSearchRequestHandler::QUERY_PARAMETER_EXCLUDE]) |
| 107 | && !is_array($queryParameters[ValueCollectionSearchRequestHandler::QUERY_PARAMETER_EXCLUDE]) |
| 108 | ) { |
| 109 | throw $this->createBadTypeException( |
| 110 | ValueCollectionSearchRequestHandler::QUERY_PARAMETER_EXCLUDE, |
| 111 | $queryParameters[ValueCollectionSearchRequestHandler::QUERY_PARAMETER_EXCLUDE], |
| 112 | 'array' |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param ServerRequestInterface $request |
| 119 | * |
| 120 | * @throws BadRequestException |
| 121 | */ |
| 122 | private function validateExcludeElements(ServerRequestInterface $request): void |
| 123 | { |
| 124 | $queryParameters = $request->getQueryParams(); |
| 125 | |
| 126 | foreach ( |
| 127 | $queryParameters[ValueCollectionSearchRequestHandler::QUERY_PARAMETER_EXCLUDE] ?? [] as $key => $excluded |
| 128 | ) { |
| 129 | if (!is_string($excluded)) { |
| 130 | throw $this->createBadTypeException( |
| 131 | ValueCollectionSearchRequestHandler::QUERY_PARAMETER_EXCLUDE . "[$key]", |
| 132 | $excluded, |
| 133 | 'string' |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private function createBadTypeException(string $parameter, $value, string $expectedType): BadRequestException |
| 140 | { |
| 141 | return new BadRequestException( |
| 142 | sprintf( |
| 143 | '"%s" query parameter is expected to be of %s type, %s given.', |
| 144 | $parameter, |
| 145 | $expectedType, |
| 146 | gettype($value) |
| 147 | ) |
| 148 | ); |
| 149 | } |
| 150 | } |