Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| OntologyMetadataImporter | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
380 | |
0.00% |
0 / 1 |
| import | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
90 | |||
| getInjectors | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| addInjector | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| __toPhpCode | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 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\import; |
| 23 | |
| 24 | use oat\generis\model\OntologyAwareTrait; |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\oatbox\service\exception\InvalidService; |
| 27 | use oat\oatbox\service\ServiceNotFoundException; |
| 28 | use oat\tao\model\metadata\exception\InconsistencyConfigException; |
| 29 | use oat\tao\model\metadata\exception\MetadataImportException; |
| 30 | use oat\tao\model\metadata\exception\injector\MetadataInjectorReadException; |
| 31 | use oat\tao\model\metadata\exception\injector\MetadataInjectorWriteException; |
| 32 | use oat\tao\model\metadata\injector\Injector; |
| 33 | |
| 34 | /** |
| 35 | * Class OntologyMetadataImport |
| 36 | * |
| 37 | * @author Camille Moyon |
| 38 | * @package oat\tao\model\metadata\import |
| 39 | * |
| 40 | * - promote $this OntologyMetadataImport to abstract |
| 41 | * - create itemMetadataImportImplementation to taoItem |
| 42 | */ |
| 43 | abstract class OntologyMetadataImporter extends ConfigurableService implements MetadataImporter |
| 44 | { |
| 45 | use OntologyAwareTrait; |
| 46 | |
| 47 | protected $injectors = []; |
| 48 | |
| 49 | /** |
| 50 | * Main method to import Iterator data to Ontology object |
| 51 | * |
| 52 | * @param array $data |
| 53 | * @param boolean $dryrun If set to true no data will be written |
| 54 | * @return \common_report_Report |
| 55 | */ |
| 56 | public function import(array $data, $dryrun = false) |
| 57 | { |
| 58 | try { |
| 59 | /** @var Injector[] $injectors */ |
| 60 | $injectors = $this->getInjectors(); |
| 61 | } catch (InconsistencyConfigException $e) { |
| 62 | return \common_report_Report::createFailure('Config problem: ' . $e->getMessage()); |
| 63 | } |
| 64 | |
| 65 | // Global report |
| 66 | $report = \common_report_Report::createInfo('Report of metadata import.'); |
| 67 | |
| 68 | // Foreach line of dateSource |
| 69 | foreach ($data as $uri => $dataSource) { |
| 70 | try { |
| 71 | // Check if resource exists |
| 72 | $resource = $this->getResource($uri); |
| 73 | if (! $resource->exists()) { |
| 74 | throw new MetadataImportException('Unable to find resource associated to uri : "' . $uri . '"'); |
| 75 | } |
| 76 | |
| 77 | $lineReport = \common_report_Report::createInfo('Report by line.'); |
| 78 | $dataSource = array_change_key_case($dataSource); |
| 79 | |
| 80 | // Foreach injector to map a target source |
| 81 | /** @var Injector $injector */ |
| 82 | foreach ($injectors as $name => $injector) { |
| 83 | $injectorReport = null; |
| 84 | |
| 85 | try { |
| 86 | $dataRead = $injector->read($dataSource); |
| 87 | $injector->write($resource, $dataRead, $dryrun); |
| 88 | $injectorReport = \common_report_Report::createSuccess( |
| 89 | 'Injector "' . $name . '" successfully ran.' |
| 90 | ); |
| 91 | } catch (MetadataInjectorReadException $e) { |
| 92 | $injectorReport = \common_report_Report::createFailure( |
| 93 | 'Injector "' . $name . '" failed to run at read: ' . $e->getMessage() |
| 94 | ); |
| 95 | } catch (MetadataInjectorWriteException $e) { |
| 96 | $injectorReport = \common_report_Report::createFailure( |
| 97 | 'Injector "' . $name . '" failed to run at write: ' . $e->getMessage() |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | // Skip if there are no report (no data to read for this injector) |
| 102 | if (! is_null($injectorReport)) { |
| 103 | $lineReport->add($injectorReport); |
| 104 | } |
| 105 | } |
| 106 | } catch (MetadataImportException $e) { |
| 107 | $lineReport = \common_report_Report::createFailure($e->getMessage()); |
| 108 | } |
| 109 | $report->add($lineReport); |
| 110 | } |
| 111 | |
| 112 | return $report; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get metadata injectors from config |
| 117 | * |
| 118 | * @return Injector[] |
| 119 | * @throws InconsistencyConfigException |
| 120 | */ |
| 121 | protected function getInjectors() |
| 122 | { |
| 123 | if (empty($this->injectors)) { |
| 124 | try { |
| 125 | foreach (array_keys($this->getOptions()) as $injectorName) { |
| 126 | /** @var Injector $injector */ |
| 127 | $injector = $this->getSubService($injectorName, Injector::class); |
| 128 | $injector->createInjectorHelpers(); |
| 129 | $this->injectors[$injectorName] = $injector; |
| 130 | } |
| 131 | } catch (ServiceNotFoundException $e) { |
| 132 | throw new InconsistencyConfigException($e->getMessage()); |
| 133 | } catch (InvalidService $e) { |
| 134 | throw new InconsistencyConfigException($e->getMessage()); |
| 135 | } |
| 136 | |
| 137 | if (empty($this->injectors)) { |
| 138 | throw new InconsistencyConfigException('No injector found into config.'); |
| 139 | } |
| 140 | } |
| 141 | return $this->injectors; |
| 142 | } |
| 143 | |
| 144 | public function addInjector($name, Injector $injector) |
| 145 | { |
| 146 | if (isset($this->injectors[$name])) { |
| 147 | throw new InconsistencyConfigException('An injector with name "' . $name . '" already exists.'); |
| 148 | } |
| 149 | |
| 150 | $this->injectors[$name] = $injector; |
| 151 | } |
| 152 | |
| 153 | public function __toPhpCode() |
| 154 | { |
| 155 | $injectorString = ''; |
| 156 | foreach ($this->injectors as $name => $injector) { |
| 157 | $injectorString .= ' \'' . $name . '\' => ' . $injector->__toPhpCode() . PHP_EOL; |
| 158 | } |
| 159 | |
| 160 | return 'new ' . get_class($this) . '(array(' . PHP_EOL . $injectorString . '))'; |
| 161 | } |
| 162 | } |