Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ArrayImportValueMapper | |
0.00% |
0 / 19 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| map | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| getReport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| mapValueThroughMapper | |
0.00% |
0 / 6 |
|
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) 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\oatbox\log\LoggerAwareTrait; |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | |
| 27 | class ArrayImportValueMapper extends ConfigurableService implements ImportValueMapperInterface |
| 28 | { |
| 29 | use LoggerAwareTrait; |
| 30 | |
| 31 | public const OPTION_DELIMITER = 'delimiter'; |
| 32 | |
| 33 | public const OPTION_VALUE_MAPPER = 'valueMapper'; |
| 34 | |
| 35 | /** @var common_report_Report */ |
| 36 | protected $report; |
| 37 | |
| 38 | /** |
| 39 | * @inheritdoc |
| 40 | */ |
| 41 | public function map($value) |
| 42 | { |
| 43 | $mapValues = []; |
| 44 | $delimiter = $this->getOption(static::OPTION_DELIMITER); |
| 45 | $valueMapper = $this->getOption(static::OPTION_VALUE_MAPPER); |
| 46 | $values = explode($delimiter, $value); |
| 47 | |
| 48 | $this->report = common_report_Report::createInfo(); |
| 49 | |
| 50 | foreach ($values as $value) { |
| 51 | if ($valueMapper instanceof ImportValueMapperInterface) { |
| 52 | $valueToBeMapped = $this->mapValueThroughMapper($valueMapper, $value); |
| 53 | if (!is_null($valueToBeMapped)) { |
| 54 | $mapValues[] = $valueToBeMapped; |
| 55 | } |
| 56 | } else { |
| 57 | $mapValues[] = $value; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return $mapValues; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @inheritdoc |
| 66 | */ |
| 67 | public function getReport() |
| 68 | { |
| 69 | return $this->report; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param ImportValueMapperInterface $valueMapper |
| 74 | * @param string $value |
| 75 | * @return mixed |
| 76 | * @throws \common_exception_Error |
| 77 | */ |
| 78 | protected function mapValueThroughMapper($valueMapper, $value) |
| 79 | { |
| 80 | try { |
| 81 | $returnValue = $valueMapper->map($value); |
| 82 | $this->report->add($valueMapper->getReport()); |
| 83 | |
| 84 | return $returnValue; |
| 85 | } catch (RdsResourceNotFoundException $e) { |
| 86 | $this->report->add(common_report_Report::createFailure($e->getMessage())); |
| 87 | |
| 88 | $this->logWarning($e->getMessage()); |
| 89 | } |
| 90 | } |
| 91 | } |