Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
45 / 45 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
UpdateMetadataRequestHandler | |
100.00% |
45 / 45 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
handle | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
3 | |||
validateRequestBody | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
5 |
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) 2022 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoQtiItem\model\presentation\web; |
24 | |
25 | use common_exception_Error; |
26 | use core_kernel_classes_Property; |
27 | use core_kernel_classes_Resource; |
28 | use InvalidArgumentException; |
29 | use JsonException; |
30 | use oat\taoQtiItem\model\input\UpdateMetadataInput; |
31 | use oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue; |
32 | use Psr\Http\Message\ServerRequestInterface; |
33 | use RuntimeException; |
34 | |
35 | class UpdateMetadataRequestHandler |
36 | { |
37 | /** |
38 | * @throws JsonException |
39 | * @throws InvalidArgumentException |
40 | * @throws common_exception_Error |
41 | */ |
42 | public function handle(ServerRequestInterface $request): UpdateMetadataInput |
43 | { |
44 | $requestBody = (array)json_decode( |
45 | (string)$request->getBody(), |
46 | true, |
47 | 512, |
48 | JSON_THROW_ON_ERROR |
49 | ); |
50 | |
51 | $this->validateRequestBody($requestBody); |
52 | |
53 | $resource = new core_kernel_classes_Resource($requestBody[UpdateMetadataInput::RESOURCE_URI]); |
54 | |
55 | if (!$resource->exists()) { |
56 | throw new RuntimeException(sprintf('Resource with id %s does not exist', $resource->getUri())); |
57 | } |
58 | |
59 | $property = new core_kernel_classes_Property($requestBody[UpdateMetadataInput::PROPERTY_URI]); |
60 | |
61 | if (!$property->exists()) { |
62 | throw new RuntimeException(sprintf('Property with id %s does not exist', $property->getUri())); |
63 | } |
64 | |
65 | return new UpdateMetadataInput( |
66 | $resource, |
67 | new SimpleMetadataValue( |
68 | $requestBody[UpdateMetadataInput::RESOURCE_URI], |
69 | [$requestBody[UpdateMetadataInput::PROPERTY_URI]], |
70 | $requestBody[UpdateMetadataInput::VALUE] |
71 | ) |
72 | ); |
73 | } |
74 | |
75 | private function validateRequestBody(array $requestBody): void |
76 | { |
77 | if ( |
78 | !isset( |
79 | $requestBody[UpdateMetadataInput::PROPERTY_URI], |
80 | $requestBody[UpdateMetadataInput::RESOURCE_URI], |
81 | $requestBody[UpdateMetadataInput::VALUE] |
82 | ) |
83 | ) { |
84 | throw new InvalidArgumentException( |
85 | sprintf( |
86 | "The properties %s, %s and %s are mandatory", |
87 | ...UpdateMetadataInput::VALID_PROPERTIES |
88 | ) |
89 | ); |
90 | } |
91 | |
92 | foreach ($requestBody as $parameterKey => $parameterValue) { |
93 | if (!in_array($parameterKey, UpdateMetadataInput::VALID_PROPERTIES)) { |
94 | throw new InvalidArgumentException( |
95 | sprintf( |
96 | "Valid properties are %s", |
97 | implode(', ', UpdateMetadataInput::VALID_PROPERTIES) |
98 | ) |
99 | ); |
100 | } |
101 | |
102 | if (!is_string($parameterValue)) { |
103 | throw new InvalidArgumentException( |
104 | sprintf( |
105 | "The property %s must be string", |
106 | $parameterKey |
107 | ) |
108 | ); |
109 | } |
110 | } |
111 | } |
112 | } |