Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ClassMetadataSearchRequestHandler | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| 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-2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Lists\Presentation\Web\RequestHandler; |
| 24 | |
| 25 | use common_exception_BadRequest as BadRequestException; |
| 26 | use oat\tao\model\Lists\Business\Domain\ClassMetadataSearchRequest; |
| 27 | use oat\tao\model\Lists\Business\Input\ClassMetadataSearchInput; |
| 28 | use oat\tao\model\Lists\Presentation\Web\RequestValidator\ClassMetadataSearchRequestValidator; |
| 29 | use oat\tao\model\service\InjectionAwareService; |
| 30 | use Psr\Http\Message\ServerRequestInterface; |
| 31 | use tao_helpers_form_elements_Calendar; |
| 32 | use tao_helpers_Uri as Id; |
| 33 | |
| 34 | class ClassMetadataSearchRequestHandler extends InjectionAwareService |
| 35 | { |
| 36 | public const SERVICE_ID = 'tao/ClassMetadataSearchRequestHandler'; |
| 37 | public const QUERY_CLASS_ID = 'classUri'; |
| 38 | public const QUERY_MAX_LIST_SIZE = 'maxListSize'; |
| 39 | |
| 40 | /** @var ClassMetadataSearchRequestValidator */ |
| 41 | private $requestValidator; |
| 42 | |
| 43 | public function __construct(ClassMetadataSearchRequestValidator $requestValidator) |
| 44 | { |
| 45 | parent::__construct(); |
| 46 | |
| 47 | $this->requestValidator = $requestValidator; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param ServerRequestInterface $request |
| 52 | * |
| 53 | * @return ClassMetadataSearchInput |
| 54 | * |
| 55 | * @throws BadRequestException |
| 56 | */ |
| 57 | public function handle(ServerRequestInterface $request): ClassMetadataSearchInput |
| 58 | { |
| 59 | $this->requestValidator->validate($request); |
| 60 | |
| 61 | $queryParameters = $request->getQueryParams(); |
| 62 | |
| 63 | $classUri = Id::decode( |
| 64 | $queryParameters[self::QUERY_CLASS_ID] |
| 65 | ); |
| 66 | |
| 67 | $searchRequest = (new ClassMetadataSearchRequest()) |
| 68 | ->setClassUri($classUri) |
| 69 | ->ignoreWidgets( |
| 70 | [ |
| 71 | tao_helpers_form_elements_Calendar::WIDGET_ID, |
| 72 | ] |
| 73 | ); |
| 74 | |
| 75 | if (!empty($queryParameters[self::QUERY_MAX_LIST_SIZE])) { |
| 76 | $searchRequest->setMaxListSize((int)$queryParameters[self::QUERY_MAX_LIST_SIZE]); |
| 77 | } |
| 78 | |
| 79 | return new ClassMetadataSearchInput($searchRequest); |
| 80 | } |
| 81 | } |