Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
74.42% |
32 / 43 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ImportProcessor | |
74.42% |
32 / 43 |
|
80.00% |
4 / 5 |
13.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| withDataBinder | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
63.33% |
19 / 30 |
|
0.00% |
0 / 1 |
9.42 | |||
| bindProperties | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| clearRecord | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2022 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\StatisticalMetadata\Import\Processor; |
| 24 | |
| 25 | use oat\tao\model\StatisticalMetadata\Import\Extractor\MetadataAliasesExtractor; |
| 26 | use Throwable; |
| 27 | use oat\oatbox\filesystem\File; |
| 28 | use oat\oatbox\reporting\Report; |
| 29 | use core_kernel_classes_Resource; |
| 30 | use oat\tao\model\Csv\Factory\ReaderFactory; |
| 31 | use tao_models_classes_dataBinding_GenerisInstanceDataBinder; |
| 32 | use oat\tao\model\import\Processor\ImportFileProcessorInterface; |
| 33 | use oat\tao\model\StatisticalMetadata\Import\Result\ImportResult; |
| 34 | use oat\tao\model\StatisticalMetadata\Import\Builder\ReportBuilder; |
| 35 | use oat\tao\model\StatisticalMetadata\Import\Validator\HeaderValidator; |
| 36 | use oat\tao\model\StatisticalMetadata\Import\Extractor\ResourceExtractor; |
| 37 | use oat\tao\model\StatisticalMetadata\Import\Extractor\MetadataValuesExtractor; |
| 38 | use oat\tao\model\StatisticalMetadata\Import\Exception\AbstractValidationException; |
| 39 | use oat\tao\model\StatisticalMetadata\Import\Extractor\MetadataPropertiesExtractor; |
| 40 | use oat\tao\model\StatisticalMetadata\Import\Exception\AggregatedValidationException; |
| 41 | use tao_models_classes_dataBinding_GenerisInstanceDataBindingException as DataBindingException; |
| 42 | |
| 43 | // @TODO Improve tests - add invalid cases |
| 44 | class ImportProcessor implements ImportFileProcessorInterface |
| 45 | { |
| 46 | /** @var ReaderFactory */ |
| 47 | private $readerFactory; |
| 48 | |
| 49 | /** @var HeaderValidator */ |
| 50 | private $headerValidator; |
| 51 | |
| 52 | /** @var MetadataPropertiesExtractor */ |
| 53 | private $metadataPropertiesExtractor; |
| 54 | |
| 55 | /** @var ResourceExtractor */ |
| 56 | private $resourceExtractor; |
| 57 | |
| 58 | /** @var MetadataValuesExtractor */ |
| 59 | private $metadataValuesExtractor; |
| 60 | |
| 61 | /** @var ReportBuilder */ |
| 62 | private $reportBuilder; |
| 63 | |
| 64 | /** @var tao_models_classes_dataBinding_GenerisInstanceDataBinder */ |
| 65 | private $dataBinder; |
| 66 | |
| 67 | /** @var NotifyImportService */ |
| 68 | private $notifyImportService; |
| 69 | |
| 70 | /** @var MetadataAliasesExtractor */ |
| 71 | private $metadataAliasesExtractor; |
| 72 | |
| 73 | public function __construct( |
| 74 | ReaderFactory $readerFactory, |
| 75 | HeaderValidator $headerValidator, |
| 76 | MetadataPropertiesExtractor $metadataPropertiesExtractor, |
| 77 | MetadataAliasesExtractor $metadataAliasesExtractor, |
| 78 | ResourceExtractor $resourceExtractor, |
| 79 | MetadataValuesExtractor $metadataValuesExtractor, |
| 80 | ReportBuilder $reportBuilder, |
| 81 | NotifyImportService $notifyImportService |
| 82 | ) { |
| 83 | $this->readerFactory = $readerFactory; |
| 84 | $this->headerValidator = $headerValidator; |
| 85 | $this->metadataPropertiesExtractor = $metadataPropertiesExtractor; |
| 86 | $this->metadataAliasesExtractor = $metadataAliasesExtractor; |
| 87 | $this->resourceExtractor = $resourceExtractor; |
| 88 | $this->metadataValuesExtractor = $metadataValuesExtractor; |
| 89 | $this->reportBuilder = $reportBuilder; |
| 90 | $this->notifyImportService = $notifyImportService; |
| 91 | } |
| 92 | |
| 93 | public function withDataBinder(tao_models_classes_dataBinding_GenerisInstanceDataBinder $dataBinder): void |
| 94 | { |
| 95 | $this->dataBinder = $dataBinder; |
| 96 | } |
| 97 | |
| 98 | public function process(File $file): Report |
| 99 | { |
| 100 | $result = new ImportResult(); |
| 101 | |
| 102 | try { |
| 103 | $csv = $this->readerFactory->createFromStream( |
| 104 | $file->readStream(), |
| 105 | [ReaderFactory::DELIMITER => ';'] |
| 106 | ); |
| 107 | |
| 108 | $header = $csv->getHeader(); |
| 109 | $this->headerValidator->validateRequiredHeaders($header); |
| 110 | |
| 111 | $metadataProperties = $this->metadataPropertiesExtractor->extract($header); |
| 112 | |
| 113 | $this->notifyImportService->withAliases($this->metadataAliasesExtractor->extract($header)); |
| 114 | } catch (AbstractValidationException | AggregatedValidationException $exception) { |
| 115 | $result->addException(0, $exception); |
| 116 | |
| 117 | return $this->reportBuilder->buildByResult($result); |
| 118 | } catch (Throwable $exception) { |
| 119 | return $this->reportBuilder->buildByException($exception); |
| 120 | } |
| 121 | |
| 122 | foreach ($csv->getRecords($header) as $line => $record) { |
| 123 | try { |
| 124 | $record = $this->clearRecord($record); |
| 125 | |
| 126 | $result->increaseTotalScannedRecords(); |
| 127 | $resource = $this->resourceExtractor->extract($record); |
| 128 | $metadataValues = $this->metadataValuesExtractor->extract($record, $resource, $metadataProperties); |
| 129 | |
| 130 | $this->bindProperties($resource, $metadataValues); |
| 131 | |
| 132 | $this->notifyImportService->addResource($resource); |
| 133 | |
| 134 | $result->increaseTotalImportedRecords(); |
| 135 | } catch (AbstractValidationException | AggregatedValidationException $exception) { |
| 136 | $result->addException($line, $exception); |
| 137 | } catch (Throwable $exception) { |
| 138 | return $this->reportBuilder->buildByException($exception); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | try { |
| 143 | $this->notifyImportService->notify(); |
| 144 | } catch (Throwable $exception) { |
| 145 | return $this->reportBuilder->buildByException($exception); |
| 146 | } |
| 147 | |
| 148 | return $this->reportBuilder->buildByResult($result); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @TODO Improve the repository to allow persistence of resource properties and use it instead of a data binder. |
| 153 | * |
| 154 | * @throws DataBindingException |
| 155 | */ |
| 156 | private function bindProperties(core_kernel_classes_Resource $resource, array $values): void |
| 157 | { |
| 158 | $binder = $this->dataBinder ?? new tao_models_classes_dataBinding_GenerisInstanceDataBinder($resource); |
| 159 | $binder->forceModification(); |
| 160 | $binder->bind($values); |
| 161 | } |
| 162 | |
| 163 | private function clearRecord(array $record): array |
| 164 | { |
| 165 | return array_map('trim', $record); |
| 166 | } |
| 167 | } |