Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
48 / 60 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| OntologyMetadataInjector | |
80.00% |
48 / 60 |
|
71.43% |
5 / 7 |
38.69 | |
0.00% |
0 / 1 |
| setOptions | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
7 | |||
| createInjectorHelpers | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| read | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
| write | |
90.91% |
20 / 22 |
|
0.00% |
0 / 1 |
9.06 | |||
| setReaders | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| setWriters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| __toPhpCode | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| 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) 2016 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\metadata\injector; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\tao\model\metadata\exception\InconsistencyConfigException; |
| 26 | use oat\tao\model\metadata\exception\reader\MetadataReaderNotFoundException; |
| 27 | use oat\tao\model\metadata\exception\writer\MetadataWriterException; |
| 28 | use oat\tao\model\metadata\exception\injector\MetadataInjectorReadException; |
| 29 | use oat\tao\model\metadata\exception\injector\MetadataInjectorWriteException; |
| 30 | use oat\tao\model\metadata\reader\KeyReader; |
| 31 | use oat\tao\model\metadata\reader\Reader; |
| 32 | use oat\tao\model\metadata\writer\ontologyWriter\OntologyWriter; |
| 33 | |
| 34 | /** |
| 35 | * Class OntologyMetadataInjector |
| 36 | * |
| 37 | * @author Camille Moyon |
| 38 | * @package oat\tao\model\metadata\injector |
| 39 | */ |
| 40 | class OntologyMetadataInjector extends ConfigurableService implements Injector |
| 41 | { |
| 42 | public const CONFIG_SOURCE = 'source'; |
| 43 | |
| 44 | public const CONFIG_DESTINATION = 'destination'; |
| 45 | |
| 46 | /** |
| 47 | * Components to read value from $dataSource |
| 48 | * |
| 49 | * @var Reader[] |
| 50 | */ |
| 51 | protected $readers; |
| 52 | |
| 53 | /** |
| 54 | * Components to write value to $resource |
| 55 | * |
| 56 | * @var OntologyWriter[] |
| 57 | */ |
| 58 | protected $writers; |
| 59 | |
| 60 | /** |
| 61 | * Override Configurable parent to check required field (source & destination) |
| 62 | * |
| 63 | * @param array $options |
| 64 | * @throws InconsistencyConfigException |
| 65 | */ |
| 66 | public function setOptions(array $options) |
| 67 | { |
| 68 | if ( |
| 69 | ! array_key_exists(self::CONFIG_SOURCE, $options) |
| 70 | || ! is_array($options[self::CONFIG_SOURCE]) |
| 71 | || empty($options[self::CONFIG_SOURCE]) |
| 72 | ) { |
| 73 | throw new InconsistencyConfigException(__('Injector has to contains a valid "source" field.')); |
| 74 | } |
| 75 | |
| 76 | if ( |
| 77 | ! array_key_exists(self::CONFIG_DESTINATION, $options) |
| 78 | || ! is_array($options[self::CONFIG_DESTINATION]) |
| 79 | || empty($options[self::CONFIG_DESTINATION]) |
| 80 | ) { |
| 81 | throw new InconsistencyConfigException(__('Injector has to contains a valid "destination" field.')); |
| 82 | } |
| 83 | |
| 84 | parent::setOptions($options); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Create injector helpers (readers & writers) from options |
| 89 | */ |
| 90 | public function createInjectorHelpers() |
| 91 | { |
| 92 | $this->setReaders($this->getOption(self::CONFIG_SOURCE)); |
| 93 | $this->setWriters($this->getOption(self::CONFIG_DESTINATION)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Read all values from readers and store it into array of $name => reader $value |
| 98 | * Throw exception if at least one reader cannot read value |
| 99 | * |
| 100 | * @param array $dataSource |
| 101 | * @return array All collected data from $this->readers |
| 102 | * @throws MetadataInjectorReadException |
| 103 | */ |
| 104 | public function read(array $dataSource) |
| 105 | { |
| 106 | $data = $errors = []; |
| 107 | |
| 108 | foreach ($this->readers as $name => $reader) { |
| 109 | try { |
| 110 | $data[$name] = $reader->getValue($dataSource); |
| 111 | } catch (MetadataReaderNotFoundException $e) { |
| 112 | $errors[$name] = $e->getMessage(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (! empty($errors)) { |
| 117 | foreach ($errors as $name => $error) { |
| 118 | \common_Logger::d('Error on injector "' . __CLASS__ . '" with reader "' . $name . '" : ' . $error); |
| 119 | } |
| 120 | throw new MetadataInjectorReadException( |
| 121 | 'Injector "' . __CLASS__ . '" cannot read all required values from readers: ' |
| 122 | . implode(', ', array_keys($errors)) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | return $data; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Write $data values using $this->writers |
| 131 | * |
| 132 | * @param \core_kernel_classes_Resource $resource |
| 133 | * @param array $data |
| 134 | * @param bool $dryrun |
| 135 | * @return bool |
| 136 | * @throws MetadataInjectorWriteException |
| 137 | */ |
| 138 | public function write(\core_kernel_classes_Resource $resource, array $data, $dryrun = false) |
| 139 | { |
| 140 | $writers = $errors = []; |
| 141 | |
| 142 | foreach ($this->writers as $name => $writer) { |
| 143 | try { |
| 144 | $value = $writer->format($data); |
| 145 | if ($writer->validate($value)) { |
| 146 | $writers[$name] = $writer; |
| 147 | } else { |
| 148 | $errors[$name] = 'Writer "' . $name . '" cannot validate value.'; |
| 149 | } |
| 150 | } catch (MetadataReaderNotFoundException $e) { |
| 151 | $errors[$name] = 'Writer "' . $name . '" cannot format value: ' . $e->getMessage(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | foreach ($writers as $name => $writer) { |
| 156 | if (! $writer instanceof OntologyWriter) { |
| 157 | $errors[$name] = __CLASS__ . ' must implements ' . OntologyWriter::class; |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | try { |
| 162 | $writer->write($resource, $data, $dryrun); |
| 163 | } catch (MetadataWriterException $e) { |
| 164 | $errors[$name] = $e->getMessage(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (! empty($errors)) { |
| 169 | foreach ($errors as $name => $error) { |
| 170 | \common_Logger::d('Error on injector "' . __CLASS__ . '" with writer "' . $name . '" : ' . $error); |
| 171 | } |
| 172 | throw new MetadataInjectorWriteException( |
| 173 | 'Injector "' . __CLASS__ . '" cannot write values from writers: ' . implode(', ', array_keys($errors)) |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Set $this->readers with Reader instance |
| 182 | * |
| 183 | * @param array $readers |
| 184 | * @throws InconsistencyConfigException |
| 185 | */ |
| 186 | protected function setReaders(array $readers) |
| 187 | { |
| 188 | foreach ($readers as $name => $options) { |
| 189 | $this->readers[$name] = new KeyReader($options); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Set $this->writers with OntologyWriter instance |
| 195 | * |
| 196 | * @param array $writers |
| 197 | */ |
| 198 | protected function setWriters(array $writers) |
| 199 | { |
| 200 | foreach ($writers as $name => $destination) { |
| 201 | $this->writers[$name] = $this->buildService($destination); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * To configuration serialization |
| 207 | * |
| 208 | * @return string |
| 209 | */ |
| 210 | public function __toPhpCode() |
| 211 | { |
| 212 | $source = ''; |
| 213 | if (! empty($this->readers)) { |
| 214 | foreach ($this->readers as $reader) { |
| 215 | $source .= \common_Utils::toHumanReadablePhpString($reader, 2) . PHP_EOL; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | $destination = ''; |
| 220 | if (! empty($this->writers)) { |
| 221 | foreach ($this->writers as $writer) { |
| 222 | $destination .= \common_Utils::toHumanReadablePhpString($writer, 2) . PHP_EOL; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | $params = [self::CONFIG_SOURCE => $this->readers, self::CONFIG_DESTINATION => $this->writers]; |
| 227 | |
| 228 | return 'new ' . get_class($this) . '(' . \common_Utils::toHumanReadablePhpString($params, 1) . '),'; |
| 229 | } |
| 230 | } |