Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 84 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
MetadataImporter | |
0.00% |
0 / 84 |
|
0.00% |
0 / 12 |
1722 | |
0.00% |
0 / 1 |
extract | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
inject | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
guard | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
classLookUp | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
validate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
register | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
56 | |||
unregister | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
metadataValueUris | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
registerMetadataService | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getGuardians | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getClassLookUp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getValidators | |
0.00% |
0 / 1 |
|
0.00% |
0 / 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) 2017 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\taoQtiItem\model\qti\metadata\importer; |
23 | |
24 | use oat\tao\model\metadata\exception\MetadataImportException; |
25 | use oat\taoQtiItem\model\qti\metadata\AbstractMetadataService; |
26 | use oat\taoQtiItem\model\qti\metadata\MetadataClassLookup; |
27 | use oat\taoQtiItem\model\qti\metadata\MetadataClassLookupClassCreator; |
28 | use oat\taoQtiItem\model\qti\metadata\MetadataGuardian; |
29 | use oat\taoQtiItem\model\qti\metadata\ContextualMetadataGuardian; |
30 | use oat\taoQtiItem\model\qti\metadata\MetadataService; |
31 | use oat\taoQtiItem\model\qti\metadata\MetadataValidator; |
32 | use oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue; |
33 | |
34 | class MetadataImporter extends AbstractMetadataService |
35 | { |
36 | /** |
37 | * Config key to store guardians classes |
38 | */ |
39 | public const GUARDIAN_KEY = 'guardians'; |
40 | |
41 | /** |
42 | * Config key to store classLookup classes |
43 | */ |
44 | public const CLASS_LOOKUP_KEY = 'classLookups'; |
45 | |
46 | /** |
47 | * Config key to store validator classes |
48 | */ |
49 | public const VALIDATOR_KEY = 'validators'; |
50 | |
51 | /** |
52 | * Extract metadata value from a DomManifest |
53 | * |
54 | * {@inheritdoc} |
55 | */ |
56 | public function extract($domManifest) |
57 | { |
58 | if (! $domManifest instanceof \DOMDocument) { |
59 | throw new MetadataImportException( |
60 | __('Metadata import requires an instance of DomManifest to extract metadata') |
61 | ); |
62 | } |
63 | return parent::extract($domManifest); |
64 | } |
65 | |
66 | /** |
67 | * Inject an identified metadata value to a resource |
68 | * |
69 | * {@inheritdoc} |
70 | */ |
71 | public function inject($identifier, $resource) |
72 | { |
73 | if (! $resource instanceof \core_kernel_classes_Resource) { |
74 | throw new MetadataImportException( |
75 | __('Metadata import requires an instance of core_kernel_classes_Resource to inject metadata') |
76 | ); |
77 | } |
78 | parent::inject($identifier, $resource); |
79 | } |
80 | |
81 | /** |
82 | * Guard a metadata identifier at import |
83 | * |
84 | * Guard a metadata identifier by calling guard method of each guardians |
85 | * If guardians have no reason to stop process, true is returned |
86 | * If a guardian does not allow the import, the target guardian is returned |
87 | * |
88 | * @param $identifier |
89 | * @return bool |
90 | */ |
91 | public function guard($identifier, $context = '') |
92 | { |
93 | foreach ($this->getGuardians() as $guardian) { |
94 | if ($guardian instanceof ContextualMetadataGuardian && $guardian->getContext() !== $context) { |
95 | continue; |
96 | } |
97 | |
98 | if ($this->hasMetadataValue($identifier)) { |
99 | \common_Logger::i(__('Guard for resource "%s"...', $identifier)); |
100 | if (($guard = $guardian->guard($this->getMetadataValue($identifier))) !== false) { |
101 | return $guard; |
102 | } |
103 | } |
104 | } |
105 | return false; |
106 | } |
107 | |
108 | /** |
109 | * Lookup classes for a metadata identifier at import |
110 | * |
111 | * Lookup classes for a metadata identifier by calling lookup method of each classLookup |
112 | * If no lookup has been triggered, false is returned |
113 | * If a lookup has been triggered, classLookup could apply his own process |
114 | * Specific should be applied here, like get created classes |
115 | * CreatedClasses params could be updated |
116 | * |
117 | * @param $identifier |
118 | * @param $createdClasses |
119 | * @return bool |
120 | */ |
121 | public function classLookUp($identifier, &$createdClasses) |
122 | { |
123 | $targetClass = false; |
124 | foreach ($this->getClassLookUp() as $classLookup) { |
125 | if ($this->hasMetadataValue($identifier)) { |
126 | \common_Logger::i(__('Target Class Lookup for resource "%s"...', $identifier)); |
127 | if (($targetClass = $classLookup->lookup($this->getMetadataValue($identifier))) !== false) { |
128 | \common_Logger::i( |
129 | // phpcs:disable Generic.Files.LineLength |
130 | __('Class Lookup Successful. Resource "%s" will be stored in RDFS Class "%s".', $identifier, $targetClass->getUri()) |
131 | // phpcs:enable Generic.Files.LineLength |
132 | ); |
133 | |
134 | if ($classLookup instanceof MetadataClassLookupClassCreator) { |
135 | $createdClasses = $classLookup->createdClasses(); |
136 | } |
137 | |
138 | break; |
139 | } |
140 | } |
141 | } |
142 | return $targetClass; |
143 | } |
144 | |
145 | /** |
146 | * @param $identifier |
147 | * @return \common_report_Report |
148 | */ |
149 | public function validate($identifier) |
150 | { |
151 | $report = new \common_report_Report(\common_report_Report::TYPE_SUCCESS); |
152 | foreach ($this->getValidators() as $validator) { |
153 | $report = $validator->validate($this->getMetadataValue($identifier)); |
154 | if ($report->getType() === \common_report_Report::TYPE_ERROR) { |
155 | break; |
156 | } |
157 | } |
158 | return $report; |
159 | } |
160 | |
161 | |
162 | |
163 | /** |
164 | * Register an importer instance |
165 | * |
166 | * Register an instance e.q. Injectors, Extractors, Guardians or LooUpClass |
167 | * Respective interface is checked |
168 | * Throw exception if call if not correctly formed |
169 | * |
170 | * @param $key |
171 | * @param $name |
172 | * @return bool |
173 | * @throws \InvalidArgumentException |
174 | */ |
175 | public function register($key, $name) |
176 | { |
177 | if (empty($key) || empty($name)) { |
178 | throw new \InvalidArgumentException(__('Register method expects $key and $name parameters.')); |
179 | } |
180 | |
181 | if (is_object($name)) { |
182 | $name = get_class($name); |
183 | } |
184 | |
185 | switch ($key) { |
186 | case self::GUARDIAN_KEY: |
187 | $this->registerInstance(self::GUARDIAN_KEY, $name, MetadataGuardian::class); |
188 | return true; |
189 | break; |
190 | case self::CLASS_LOOKUP_KEY: |
191 | $this->registerInstance(self::CLASS_LOOKUP_KEY, $name, MetadataClassLookup::class); |
192 | return true; |
193 | break; |
194 | case self::VALIDATOR_KEY: |
195 | $this->registerInstance(self::VALIDATOR_KEY, $name, MetadataValidator::class); |
196 | return true; |
197 | break; |
198 | } |
199 | return parent::register($key, $name); |
200 | } |
201 | |
202 | /** |
203 | * Unregister an instances |
204 | * |
205 | * Look for GUARDIAN OR CLASS_LOOKUP key, otherwise fallback to parent |
206 | * |
207 | * @param string $key |
208 | * @param string $name |
209 | * @return bool |
210 | * @throws \common_Exception |
211 | */ |
212 | public function unregister($key, $name) |
213 | { |
214 | if (empty($key) || empty($name)) { |
215 | throw new \common_Exception(__('Unregister method expects $key and $name parameters.')); |
216 | } |
217 | |
218 | if (is_object($name)) { |
219 | $name = get_class($name); |
220 | } |
221 | |
222 | switch ($key) { |
223 | case self::GUARDIAN_KEY: |
224 | $this->unregisterInstance(self::GUARDIAN_KEY, $name); |
225 | return true; |
226 | break; |
227 | case self::CLASS_LOOKUP_KEY: |
228 | $this->unregisterInstance(self::CLASS_LOOKUP_KEY, $name); |
229 | return true; |
230 | break; |
231 | } |
232 | return parent::unregister($key, $name); |
233 | } |
234 | |
235 | public function metadataValueUris($metadata): array |
236 | { |
237 | $metadataUriList = []; |
238 | foreach ($metadata as $resourceIdentifier => $metadataValueCollection) { |
239 | foreach ($metadataValueCollection as $metadataValue) { |
240 | if (!$metadataValue instanceof SimpleMetadataValue) { |
241 | continue; |
242 | } |
243 | $uri = $metadataValue->getPath()[1]; |
244 | if (!empty($uri)) { |
245 | $metadataUriList[] = $uri; |
246 | } |
247 | } |
248 | } |
249 | |
250 | return array_unique($metadataUriList); |
251 | } |
252 | |
253 | /** |
254 | * Allow to register, into the config, the current importer service |
255 | */ |
256 | protected function registerMetadataService() |
257 | { |
258 | if ($this->getServiceLocator()->has(MetadataService::SERVICE_ID)) { |
259 | $metadataService = $this->getServiceLocator()->get(MetadataService::SERVICE_ID); |
260 | } else { |
261 | $metadataService = $this->getServiceManager()->build(MetadataService::class); |
262 | } |
263 | $metadataService->setOption(MetadataService::IMPORTER_KEY, $this); |
264 | $this->getServiceManager()->register(MetadataService::SERVICE_ID, $metadataService); |
265 | } |
266 | |
267 | /** |
268 | * Return all guardians stored into config |
269 | * |
270 | * @return MetadataGuardian[] |
271 | */ |
272 | protected function getGuardians() |
273 | { |
274 | return $this->getInstances(self::GUARDIAN_KEY, MetadataGuardian::class); |
275 | } |
276 | |
277 | /** |
278 | * Return all classLookup stored into config |
279 | * |
280 | * @return MetadataClassLookup[] |
281 | */ |
282 | protected function getClassLookUp() |
283 | { |
284 | return $this->getInstances(self::CLASS_LOOKUP_KEY, MetadataClassLookup::class); |
285 | } |
286 | |
287 | /** |
288 | * Return all validators stored into config |
289 | * |
290 | * @return MetadataValidator[] |
291 | */ |
292 | protected function getValidators() |
293 | { |
294 | return $this->getInstances(self::VALIDATOR_KEY, MetadataValidator::class); |
295 | } |
296 | } |