Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.00% |
24 / 25 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RdsValidatorValueMapper | |
96.00% |
24 / 25 |
|
50.00% |
1 / 2 |
6 | |
0.00% |
0 / 1 |
| map | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
5 | |||
| getReport | |
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) 2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\import\service; |
| 22 | |
| 23 | use common_report_Report; |
| 24 | use oat\generis\model\OntologyAwareTrait; |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | |
| 27 | class RdsValidatorValueMapper extends ConfigurableService implements ImportValueMapperInterface |
| 28 | { |
| 29 | use OntologyAwareTrait; |
| 30 | |
| 31 | public const OPTION_CLASS = 'class'; |
| 32 | public const OPTION_PROPERTY = 'property'; |
| 33 | |
| 34 | /** @var common_report_Report */ |
| 35 | protected $report; |
| 36 | |
| 37 | /** |
| 38 | * @param string $value |
| 39 | * @return mixed |
| 40 | * @throws RdsResourceNotFoundException |
| 41 | */ |
| 42 | public function map($value) |
| 43 | { |
| 44 | $class = $this->getClass($this->getOption(static::OPTION_CLASS)); |
| 45 | |
| 46 | if (is_null($this->getOption(static::OPTION_PROPERTY))) { |
| 47 | $results = [$class->getResource($value)]; |
| 48 | } else { |
| 49 | $results = $class->searchInstances( |
| 50 | [ $this->getOption(static::OPTION_PROPERTY) => $value ], |
| 51 | [ 'like' => false, 'recursive' => true ] |
| 52 | ); |
| 53 | } |
| 54 | if (count($results) === 0) { |
| 55 | throw new RdsResourceNotFoundException( |
| 56 | 'No resource found for class: ' . $this->getOption(static::OPTION_CLASS) |
| 57 | . ' value: ' . $value |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | if (count($results) > 1) { |
| 62 | throw new RdsResourceNotFoundException( |
| 63 | 'Multiple values has been found for class: ' . $this->getOption(static::OPTION_CLASS) |
| 64 | . ' value :' . $value |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | $resource = reset($results); |
| 69 | if (!$resource->isInstanceOf($class)) { |
| 70 | throw new RdsResourceNotFoundException( |
| 71 | 'Resource is not a class: ' . $this->getOption(static::OPTION_CLASS) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | $this->report = common_report_Report::createSuccess('Resource mapped with success: ' . $class . ':' . $value); |
| 76 | |
| 77 | return $resource; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return common_report_Report |
| 82 | */ |
| 83 | public function getReport() |
| 84 | { |
| 85 | return $this->report; |
| 86 | } |
| 87 | } |