Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
9 / 15 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ImporterFactory | |
60.00% |
9 / 15 |
|
0.00% |
0 / 3 |
6.60 | |
0.00% |
0 / 1 |
| create | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
3.05 | |||
| throwException | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDefaultMapper | |
0.00% |
0 / 3 |
|
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) 2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\import\service; |
| 22 | |
| 23 | use oat\oatbox\service\ConfigurableService; |
| 24 | use oat\oatbox\service\exception\InvalidService; |
| 25 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 26 | |
| 27 | class ImporterFactory extends ConfigurableService implements ImporterFactoryInterface |
| 28 | { |
| 29 | /** |
| 30 | * Create an importer |
| 31 | * |
| 32 | * User type is defined in a config mapper and is associated to a role |
| 33 | * |
| 34 | * @param $type |
| 35 | * @return ImportServiceInterface |
| 36 | * @throws \common_exception_NotFound |
| 37 | * @throws InvalidService |
| 38 | * @throws InvalidServiceManagerException |
| 39 | */ |
| 40 | public function create($type) |
| 41 | { |
| 42 | $typeOptions = $this->getOption(self::OPTION_MAPPERS); |
| 43 | $typeOption = isset($typeOptions[$type]) ? $typeOptions[$type] : $this->throwException(); |
| 44 | $importerString = $typeOption[self::OPTION_MAPPERS_IMPORTER] ?? $this->throwException(); |
| 45 | $importer = $this->buildService($importerString, ImportServiceInterface::class); |
| 46 | |
| 47 | if (isset($typeOption[self::OPTION_MAPPERS_MAPPER])) { |
| 48 | $mapperString = $typeOption[self::OPTION_MAPPERS_MAPPER]; |
| 49 | $mapper = $this->buildService($mapperString); |
| 50 | } else { |
| 51 | $mapper = $this->getDefaultMapper(); |
| 52 | } |
| 53 | |
| 54 | $this->propagate($mapper); |
| 55 | $importer->setMapper($mapper); |
| 56 | |
| 57 | return $importer; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @throws \common_exception_NotFound |
| 62 | */ |
| 63 | protected function throwException() |
| 64 | { |
| 65 | throw new \common_exception_NotFound('Unable to load importer for type.'); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return ImportMapperInterface |
| 70 | */ |
| 71 | protected function getDefaultMapper() |
| 72 | { |
| 73 | return new OntologyMapper([ |
| 74 | ImportMapperInterface::OPTION_SCHEMA => $this->getOption(self::OPTION_DEFAULT_SCHEMA) |
| 75 | ]); |
| 76 | } |
| 77 | } |