Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
30 / 35 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ValueCollectionSearchRequestHandler | |
85.71% |
30 / 35 |
|
66.67% |
2 / 3 |
8.19 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
5 | |||
| getPropertyListUri | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 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-2021 (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\RequestHandler; |
| 26 | |
| 27 | use common_exception_BadRequest as BadRequestException; |
| 28 | use core_kernel_classes_Property as RdfProperty; |
| 29 | use oat\tao\model\Lists\Business\Domain\ValueCollectionSearchRequest; |
| 30 | use oat\tao\model\Lists\Business\Input\ValueCollectionSearchInput; |
| 31 | use oat\tao\model\Lists\Presentation\Web\RequestValidator\ValueCollectionSearchRequestValidator; |
| 32 | use oat\tao\model\service\InjectionAwareService; |
| 33 | use Psr\Http\Message\ServerRequestInterface; |
| 34 | use tao_helpers_Uri as Id; |
| 35 | |
| 36 | class ValueCollectionSearchRequestHandler extends InjectionAwareService |
| 37 | { |
| 38 | public const SERVICE_ID = 'tao/ValueCollectionSearchRequestHandler'; |
| 39 | |
| 40 | public const QUERY_PARAMETER_ID = 'propertyUri'; |
| 41 | public const QUERY_PARAMETER_PARENT_LIST_VALUES = 'parentListValues'; |
| 42 | public const QUERY_PARAMETER_SUBJECT = 'subject'; |
| 43 | public const QUERY_PARAMETER_EXCLUDE = 'exclude'; |
| 44 | |
| 45 | private const SEARCH_LIMIT = 20; |
| 46 | |
| 47 | /** @var ValueCollectionSearchRequestValidator */ |
| 48 | private $requestValidator; |
| 49 | |
| 50 | public function __construct(ValueCollectionSearchRequestValidator $requestValidator) |
| 51 | { |
| 52 | parent::__construct(); |
| 53 | |
| 54 | $this->requestValidator = $requestValidator; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param ServerRequestInterface $request |
| 59 | * |
| 60 | * @return ValueCollectionSearchInput |
| 61 | * |
| 62 | * @throws BadRequestException |
| 63 | */ |
| 64 | public function handle(ServerRequestInterface $request): ValueCollectionSearchInput |
| 65 | { |
| 66 | $this->requestValidator->validate($request); |
| 67 | |
| 68 | $queryParameters = $request->getQueryParams(); |
| 69 | |
| 70 | $propertyUri = Id::decode( |
| 71 | $queryParameters[self::QUERY_PARAMETER_ID] |
| 72 | ); |
| 73 | |
| 74 | $searchRequest = (new ValueCollectionSearchRequest()) |
| 75 | ->setLimit(self::SEARCH_LIMIT) |
| 76 | ->setPropertyUri($propertyUri); |
| 77 | |
| 78 | if (!empty($queryParameters[self::QUERY_PARAMETER_PARENT_LIST_VALUES])) { |
| 79 | $parentListValues = $queryParameters[self::QUERY_PARAMETER_PARENT_LIST_VALUES]; |
| 80 | |
| 81 | array_walk( |
| 82 | $parentListValues, |
| 83 | function (&$value) { |
| 84 | $value = Id::decode($value); |
| 85 | } |
| 86 | ); |
| 87 | |
| 88 | $searchRequest->setParentListValues(...$parentListValues); |
| 89 | } |
| 90 | |
| 91 | $listUri = $this->getPropertyListUri($propertyUri); |
| 92 | |
| 93 | if ($listUri !== null) { |
| 94 | $searchRequest->setValueCollectionUri($listUri); |
| 95 | } |
| 96 | |
| 97 | $subject = trim($queryParameters[self::QUERY_PARAMETER_SUBJECT] ?? ''); |
| 98 | |
| 99 | if (!empty($subject)) { |
| 100 | $searchRequest->setSubject($subject); |
| 101 | } |
| 102 | |
| 103 | foreach ($queryParameters[self::QUERY_PARAMETER_EXCLUDE] ?? [] as $excluded) { |
| 104 | $searchRequest->addExcluded( |
| 105 | Id::decode($excluded) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | return new ValueCollectionSearchInput($searchRequest); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param string $propertyUri |
| 114 | * |
| 115 | * @return string|null |
| 116 | */ |
| 117 | protected function getPropertyListUri(string $propertyUri): ?string |
| 118 | { |
| 119 | $property = new RdfProperty($propertyUri); |
| 120 | $list = $property->getRange(); |
| 121 | |
| 122 | if ($list !== null) { |
| 123 | return $list->getUri(); |
| 124 | } |
| 125 | |
| 126 | return null; |
| 127 | } |
| 128 | } |