Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
74.29% |
26 / 35 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| MetaMetadataImportMapper | |
74.29% |
26 / 35 |
|
40.00% |
2 / 5 |
30.23 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| mapMetaMetadataToProperties | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 | |||
| mapMetadataToProperties | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| matchProperty | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
6.07 | |||
| isSynced | |
36.36% |
4 / 11 |
|
0.00% |
0 / 1 |
19.63 | |||
| 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) 2024 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\qti\metadata\importer; |
| 24 | |
| 25 | use core_kernel_classes_Class as kernelClass; |
| 26 | use core_kernel_classes_Property as Property; |
| 27 | use core_kernel_classes_Resource; |
| 28 | use InvalidArgumentException; |
| 29 | use oat\generis\model\GenerisRdf; |
| 30 | use oat\taoQtiItem\model\import\ChecksumGenerator; |
| 31 | |
| 32 | class MetaMetadataImportMapper |
| 33 | { |
| 34 | private ChecksumGenerator $checksumGenerator; |
| 35 | |
| 36 | public function __construct(ChecksumGenerator $checksumGenerator) |
| 37 | { |
| 38 | $this->checksumGenerator = $checksumGenerator; |
| 39 | } |
| 40 | |
| 41 | public function mapMetaMetadataToProperties( |
| 42 | array $metaMetadataProperties, |
| 43 | kernelClass $itemClass, |
| 44 | kernelClass $testClass = null |
| 45 | ): array { |
| 46 | $matchedProperties = []; |
| 47 | foreach ($metaMetadataProperties as $metaMetadataProperty) { |
| 48 | if ($match = $this->matchProperty($metaMetadataProperty, $itemClass->getProperties(true))) { |
| 49 | $matchedProperties['itemProperties'][$metaMetadataProperty['uri']] = $match; |
| 50 | } |
| 51 | |
| 52 | if ( |
| 53 | $testClass && |
| 54 | $match = $this->matchProperty($metaMetadataProperty, $testClass->getProperties(true)) |
| 55 | ) { |
| 56 | $matchedProperties['testProperties'][$metaMetadataProperty['uri']] = $match; |
| 57 | } |
| 58 | } |
| 59 | return $matchedProperties; |
| 60 | } |
| 61 | |
| 62 | public function mapMetadataToProperties( |
| 63 | array $metadataProperties, |
| 64 | kernelClass $itemClass, |
| 65 | kernelClass $testClass = null |
| 66 | ): array { |
| 67 | $parsedMetadataProperties = []; |
| 68 | foreach ($metadataProperties as $metadataProperty) { |
| 69 | foreach ($metadataProperty as $property) { |
| 70 | $parsedMetadataProperties[] = [ |
| 71 | 'uri' => $property->getPath()[1], |
| 72 | ]; |
| 73 | } |
| 74 | } |
| 75 | return $this->mapMetaMetadataToProperties($parsedMetadataProperties, $itemClass, $testClass); |
| 76 | } |
| 77 | |
| 78 | private function matchProperty(array &$metaMetadataProperty, array $classProperties): ?Property |
| 79 | { |
| 80 | /** @var Property $itemClassProperty */ |
| 81 | foreach ($classProperties as $classProperty) { |
| 82 | if ( |
| 83 | $classProperty->getUri() === $metaMetadataProperty['uri'] |
| 84 | ) { |
| 85 | return $classProperty; |
| 86 | } |
| 87 | if ( |
| 88 | $classProperty->getLabel() === $metaMetadataProperty['label'] |
| 89 | || $classProperty->getAlias() === $metaMetadataProperty['alias'] |
| 90 | ) { |
| 91 | if ($this->isSynced($classProperty, $metaMetadataProperty)) { |
| 92 | return $classProperty; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | private function isSynced(Property $classProperty, array &$metaMetadataProperty): bool |
| 100 | { |
| 101 | $multiple = $classProperty->getOnePropertyValue(new Property(GenerisRdf::PROPERTY_MULTIPLE)); |
| 102 | try { |
| 103 | $checksum = $this->checksumGenerator->getRangeChecksum($classProperty); |
| 104 | } catch (InvalidArgumentException $e) { |
| 105 | return false; |
| 106 | } |
| 107 | $metaMetadataProperty['checksum_result'] = $checksum === $metaMetadataProperty['checksum']; |
| 108 | $metaMetadataProperty['widget_result'] = |
| 109 | $classProperty->getWidget() && $classProperty->getWidget()->getUri() === $metaMetadataProperty['widget']; |
| 110 | |
| 111 | return $multiple instanceof core_kernel_classes_Resource |
| 112 | && $multiple->getUri() === $metaMetadataProperty['multiple'] |
| 113 | && $checksum === $metaMetadataProperty['checksum'] |
| 114 | && $classProperty->getWidget() && $classProperty->getWidget()->getUri() === $metaMetadataProperty['widget']; |
| 115 | } |
| 116 | } |