Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       0.00%  | 
       0 / 52  | 
               | 
       0.00%  | 
       0 / 8  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| MetadataResolver |         | 
       0.00%  | 
       0 / 52  | 
               | 
       0.00%  | 
       0 / 8  | 
       342 |         | 
       0.00%  | 
       0 / 1  | 
      
| resolve |         | 
       0.00%  | 
       0 / 35  | 
               | 
       0.00%  | 
       0 / 1  | 
       72 | |||
| validate |         | 
       0.00%  | 
       0 / 5  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| isEmptyAlias |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| getMetadataWithAlias |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| add |         | 
       0.00%  | 
       0 / 3  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| getCached |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| getPropertyUri |         | 
       0.00%  | 
       0 / 5  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| getElementMapFactory |         | 
       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) 2021 (original work) Open Assessment Technologies SA; | 
| 19 | */ | 
| 20 | |
| 21 | declare(strict_types=1); | 
| 22 | |
| 23 | namespace oat\taoQtiItem\model\import\Metadata; | 
| 24 | |
| 25 | use core_kernel_classes_Class; | 
| 26 | use core_kernel_classes_Property; | 
| 27 | use oat\generis\model\GenerisRdf; | 
| 28 | use oat\generis\model\OntologyAwareTrait; | 
| 29 | use oat\oatbox\service\ConfigurableService; | 
| 30 | use oat\tao\helpers\form\ElementMapFactory; | 
| 31 | use oat\taoQtiItem\model\import\ParsedMetadatum; | 
| 32 | use oat\taoQtiItem\model\import\Validator\AggregatedValidationException; | 
| 33 | use oat\taoQtiItem\model\import\Validator\ErrorValidationException; | 
| 34 | |
| 35 | class MetadataResolver extends ConfigurableService | 
| 36 | { | 
| 37 | use OntologyAwareTrait; | 
| 38 | |
| 39 | private const MAX_CACHE_SIZE = 1000; | 
| 40 | |
| 41 | /** @var array */ | 
| 42 | private $cache = []; | 
| 43 | |
| 44 | /** | 
| 45 | * @param ParsedMetadatum[] $metadata | 
| 46 | * | 
| 47 | * @return ParsedMetadatum[] | 
| 48 | * | 
| 49 | * @throws AggregatedValidationException | 
| 50 | */ | 
| 51 | public function resolve(core_kernel_classes_Class $class, array $metadata): array | 
| 52 | { | 
| 53 | $result = []; | 
| 54 | $aliasProperty = $this->getProperty(GenerisRdf::PROPERTY_ALIAS); | 
| 55 | $classProperties = $class->getProperties(true); | 
| 56 | $classUri = $class->getUri(); | 
| 57 | $metaDataWithAlias = $this->getMetadataWithAlias($metadata); | 
| 58 | $errors = []; | 
| 59 | |
| 60 | if (empty($metaDataWithAlias)) { | 
| 61 | return $metadata; | 
| 62 | } | 
| 63 | |
| 64 | foreach ($classProperties as $property) { | 
| 65 | $aliasName = (string)$property->getOnePropertyValue($aliasProperty); | 
| 66 | |
| 67 | if (empty($aliasName)) { | 
| 68 | continue; | 
| 69 | } | 
| 70 | |
| 71 | foreach ($metaDataWithAlias as $metaDataItem) { | 
| 72 | if ($metaDataItem->getAlias() === $aliasName) { | 
| 73 | $propertyUri = $this->getPropertyUri($property, $classUri, $aliasName); | 
| 74 | $metaDataItem->setPropertyUri($propertyUri); | 
| 75 | $errors = $this->validate($property, $aliasName, $metaDataItem->getValue(), $errors); | 
| 76 | $result[$propertyUri] = $metaDataItem->getValue(); | 
| 77 | break; | 
| 78 | } | 
| 79 | } | 
| 80 | } | 
| 81 | |
| 82 | if (empty($errors)) { | 
| 83 | return $result; | 
| 84 | } | 
| 85 | |
| 86 | $messages = []; | 
| 87 | |
| 88 | foreach ($errors as $alias => $message) { | 
| 89 | $messages[] = '"' . $alias . '" ' . $message; | 
| 90 | } | 
| 91 | |
| 92 | //@TODO In the future, create one separate error for each metadata. Requires new translations | 
| 93 | throw new AggregatedValidationException( | 
| 94 | [ | 
| 95 | new ErrorValidationException( | 
| 96 | 'Metadata are not correct: %s', | 
| 97 | [ | 
| 98 | implode('. ', $messages) | 
| 99 | ] | 
| 100 | ) | 
| 101 | ], | 
| 102 | [] | 
| 103 | ); | 
| 104 | } | 
| 105 | |
| 106 | private function validate( | 
| 107 | core_kernel_classes_Property $property, | 
| 108 | string $alias, | 
| 109 | string $value, | 
| 110 | array $errors | 
| 111 | ): array { | 
| 112 | $element = $this->getElementMapFactory()->create($property); | 
| 113 | $element->setValue($value); | 
| 114 | |
| 115 | if (!$element->validate()) { | 
| 116 | $errors[$alias] = $element->getError(); | 
| 117 | } | 
| 118 | |
| 119 | return $errors; | 
| 120 | } | 
| 121 | |
| 122 | private function isEmptyAlias(ParsedMetadatum $metadata): bool | 
| 123 | { | 
| 124 | return empty($metadata->getPropertyUri()); | 
| 125 | } | 
| 126 | |
| 127 | /** | 
| 128 | * @param ParsedMetadatum[] $metadata | 
| 129 | * | 
| 130 | * @return ParsedMetadatum[] | 
| 131 | */ | 
| 132 | private function getMetadataWithAlias(array $metadata): array | 
| 133 | { | 
| 134 | return array_filter($metadata, [$this, 'isEmptyAlias']); | 
| 135 | } | 
| 136 | |
| 137 | private function add(string $classUri, string $aliasName, string $aliasUri): void | 
| 138 | { | 
| 139 | $this->cache[$classUri . $aliasName] = $aliasUri; | 
| 140 | |
| 141 | if (count($this->cache) > self::MAX_CACHE_SIZE) { | 
| 142 | array_shift($this->cache); | 
| 143 | } | 
| 144 | } | 
| 145 | |
| 146 | private function getCached(string $classUri, string $aliasName): ?string | 
| 147 | { | 
| 148 | return $this->cache[$classUri . $aliasName] ?? null; | 
| 149 | } | 
| 150 | |
| 151 | private function getPropertyUri( | 
| 152 | core_kernel_classes_Property $property, | 
| 153 | string $classUri, | 
| 154 | string $aliasName | 
| 155 | ): ?string { | 
| 156 | $cachedPropertyUri = $this->getCached($classUri, $aliasName); | 
| 157 | |
| 158 | if (null !== $cachedPropertyUri) { | 
| 159 | return $cachedPropertyUri; | 
| 160 | } | 
| 161 | |
| 162 | $this->add($classUri, $aliasName, $property->getUri()); | 
| 163 | |
| 164 | return $property->getUri(); | 
| 165 | } | 
| 166 | |
| 167 | private function getElementMapFactory(): ElementMapFactory | 
| 168 | { | 
| 169 | return $this->getServiceManager()->get(ElementMapFactory::class); | 
| 170 | } | 
| 171 | } |