Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       58 / 58  | 
               | 
       100.00%  | 
       7 / 7  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| OntologyMetadataInjector |         | 
       100.00%  | 
       58 / 58  | 
               | 
       100.00%  | 
       7 / 7  | 
       28 |         | 
       100.00%  | 
       1 / 1  | 
      
| __construct |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| addInjectionRule |         | 
       100.00%  | 
       9 / 9  | 
               | 
       100.00%  | 
       1 / 1  | 
       3 | |||
| setInjectionRules |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| getInjectionRules |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| inject |         | 
       100.00%  | 
       34 / 34  | 
               | 
       100.00%  | 
       1 / 1  | 
       16 | |||
| getRuleByValue |         | 
       100.00%  | 
       7 / 7  | 
               | 
       100.00%  | 
       1 / 1  | 
       4 | |||
| getRuleByPath |         | 
       100.00%  | 
       5 / 5  | 
               | 
       100.00%  | 
       1 / 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) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); | 
| 19 | * | 
| 20 | */ | 
| 21 | |
| 22 | namespace oat\taoQtiItem\model\qti\metadata\ontology; | 
| 23 | |
| 24 | use oat\oatbox\event\EventManager; | 
| 25 | use oat\oatbox\service\ServiceManager; | 
| 26 | use oat\tao\model\event\MetadataModified; | 
| 27 | use oat\taoQtiItem\model\qti\metadata\MetadataInjector; | 
| 28 | use oat\taoQtiItem\model\qti\metadata\MetadataInjectionException; | 
| 29 | use core_kernel_classes_Resource; | 
| 30 | use core_kernel_classes_Property; | 
| 31 | use InvalidArgumentException; | 
| 32 | |
| 33 | class OntologyMetadataInjector implements MetadataInjector | 
| 34 | { | 
| 35 | private $injectionRules; | 
| 36 | |
| 37 | public function __construct() | 
| 38 | { | 
| 39 | $this->setInjectionRules([]); | 
| 40 | } | 
| 41 | |
| 42 | public function addInjectionRule(array $path, $propertyUri, $value = null, $ontologyValue = null) | 
| 43 | { | 
| 44 | if (count($path) === 0) { | 
| 45 | $msg = "The path argument must be a non-empty array."; | 
| 46 | throw new InvalidArgumentException($msg); | 
| 47 | } | 
| 48 | |
| 49 | $injectionRules = $this->getInjectionRules(); | 
| 50 | |
| 51 | $pathKey = implode('->', $path); | 
| 52 | if (isset($injectionRules[$pathKey]) === false) { | 
| 53 | $injectionRules[$pathKey] = []; | 
| 54 | } | 
| 55 | |
| 56 | $injectionRules[$pathKey][] = [$propertyUri, $value, $ontologyValue]; | 
| 57 | $this->setInjectionRules($injectionRules); | 
| 58 | } | 
| 59 | |
| 60 | protected function setInjectionRules(array $injectionRules) | 
| 61 | { | 
| 62 | $this->injectionRules = $injectionRules; | 
| 63 | } | 
| 64 | |
| 65 | protected function getInjectionRules() | 
| 66 | { | 
| 67 | return $this->injectionRules; | 
| 68 | } | 
| 69 | |
| 70 | public function inject($target, array $values) | 
| 71 | { | 
| 72 | if (!$target instanceof core_kernel_classes_Resource) { | 
| 73 | $msg = "The given target is not an instance of core_kernel_classes_Resource."; | 
| 74 | throw new MetadataInjectionException($msg); | 
| 75 | } | 
| 76 | |
| 77 | $data = []; | 
| 78 | |
| 79 | foreach ($values as $metadataValues) { | 
| 80 | foreach ($metadataValues as $metadataValue) { | 
| 81 | $lang = $metadataValue->getLanguage() ?: DEFAULT_LANG; | 
| 82 | |
| 83 | if (($rule = $this->getRuleByValue($metadataValue->getPath(), $metadataValue->getValue())) !== false) { | 
| 84 | // Direct Mapping. | 
| 85 | if (!isset($data[$rule[0]])) { | 
| 86 | $data[$rule[0]] = []; | 
| 87 | } | 
| 88 | if (!isset($data[$rule[0]][$lang])) { | 
| 89 | $data[$rule[0]][$lang] = []; | 
| 90 | } | 
| 91 | |
| 92 | $data[$rule[0]][$lang][] = [$rule[2], $metadataValue]; | 
| 93 | } elseif (($rule = $this->getRuleByPath($metadataValue->getPath())) !== false) { | 
| 94 | if (!isset($data[$rule[0]])) { | 
| 95 | $data[$rule[0]] = []; | 
| 96 | } | 
| 97 | if (!isset($data[$rule[0]][$lang])) { | 
| 98 | $data[$rule[0]][$lang] = []; | 
| 99 | } | 
| 100 | |
| 101 | $data[$rule[0]][$lang][] = [$metadataValue->getValue(), $metadataValue]; | 
| 102 | } | 
| 103 | } | 
| 104 | } | 
| 105 | |
| 106 | // Cleanup impacted metadata, in case the $target is being overwritten. | 
| 107 | foreach ($data as $propertyUri => $perLangData) { | 
| 108 | foreach (array_keys($perLangData) as $lang) { | 
| 109 | $target->removePropertyValueByLg(new core_kernel_classes_Property($propertyUri), $lang); | 
| 110 | } | 
| 111 | } | 
| 112 | |
| 113 | // Inject new data in Ontology for target. | 
| 114 | foreach ($data as $propertyUri => $perLangData) { | 
| 115 | foreach ($perLangData as $lang => $d) { | 
| 116 | foreach ($d as $actualData) { | 
| 117 | $target->setPropertyValueByLg( | 
| 118 | new core_kernel_classes_Property($propertyUri), | 
| 119 | $actualData[0], | 
| 120 | $lang | 
| 121 | ); | 
| 122 | |
| 123 | // Send events. | 
| 124 | $eventManager = ServiceManager::getServiceManager()->get(EventManager::SERVICE_ID); | 
| 125 | $metadata = $actualData[1]->getPath(); | 
| 126 | $metadataUri = array_pop($metadata); | 
| 127 | $eventManager->trigger(new MetadataModified($target, $metadataUri, $actualData[1]->getValue())); | 
| 128 | } | 
| 129 | } | 
| 130 | } | 
| 131 | } | 
| 132 | |
| 133 | protected function getRuleByValue($path, $value) | 
| 134 | { | 
| 135 | $pathKey = implode('->', $path); | 
| 136 | $rules = $this->getInjectionRules(); | 
| 137 | |
| 138 | if (isset($rules[$pathKey]) === true) { | 
| 139 | foreach ($rules[$pathKey] as $rule) { | 
| 140 | if ($rule[1] === $value) { | 
| 141 | return $rule; | 
| 142 | } | 
| 143 | } | 
| 144 | } | 
| 145 | |
| 146 | return false; | 
| 147 | } | 
| 148 | |
| 149 | protected function getRuleByPath($path) | 
| 150 | { | 
| 151 | $pathKey = implode('->', $path); | 
| 152 | $rules = $this->getInjectionRules(); | 
| 153 | if (isset($rules[$pathKey]) === true) { | 
| 154 | return $rules[$pathKey][0]; | 
| 155 | } | 
| 156 | |
| 157 | return false; | 
| 158 | } | 
| 159 | } |