Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.49% |
43 / 61 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CsvParser | |
70.49% |
43 / 61 |
|
85.71% |
6 / 7 |
19.04 | |
0.00% |
0 / 1 |
| parseFile | |
65.38% |
34 / 52 |
|
0.00% |
0 / 1 |
9.03 | |||
| trimLine | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| convertCsvLineToArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeBOM | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHeaderValidator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLineValidator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCsvLineConverter | |
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) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\import\Parser; |
| 24 | |
| 25 | use oat\oatbox\filesystem\File; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\taoQtiItem\model\import\ItemImportResult; |
| 28 | use oat\taoQtiItem\model\import\Validator\AggregatedValidationException; |
| 29 | use oat\taoQtiItem\model\import\Validator\HeaderValidator; |
| 30 | use oat\taoQtiItem\model\import\Validator\LineValidator; |
| 31 | use oat\taoQtiItem\model\import\Validator\ValidatorInterface; |
| 32 | use oat\taoQtiItem\model\import\TemplateInterface; |
| 33 | use oat\taoQtiItem\model\import\Validator\WarningValidationException; |
| 34 | use Throwable; |
| 35 | |
| 36 | class CsvParser extends ConfigurableService implements ParserInterface |
| 37 | { |
| 38 | use CsvSeparatorTrait; |
| 39 | |
| 40 | /** |
| 41 | * @inheritDoc |
| 42 | */ |
| 43 | public function parseFile(File $file, TemplateInterface $template): ItemImportResult |
| 44 | { |
| 45 | $results = new ItemImportResult(); |
| 46 | $logger = $this->getLogger(); |
| 47 | $lineValidator = $this->getLineValidator(); |
| 48 | $csvLineConverter = $this->getCsvLineConverter(); |
| 49 | $currentLineNumber = 0; |
| 50 | |
| 51 | $lines = explode(PHP_EOL, $file->readPsrStream()->getContents()); |
| 52 | $header = $this->convertCsvLineToArray($lines[0]); |
| 53 | $header = $this->trimLine($header); |
| 54 | |
| 55 | try { |
| 56 | $this->getHeaderValidator()->validate($header, $template); |
| 57 | } catch (Throwable $exception) { |
| 58 | $logger->warning( |
| 59 | sprintf( |
| 60 | 'Tabular import: Unexpected error on line %s: %s', |
| 61 | 1, |
| 62 | $exception->getMessage() |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | return $results->addException(1, $exception); |
| 67 | } |
| 68 | |
| 69 | array_shift($lines); |
| 70 | |
| 71 | foreach (array_filter($lines) as $lineNumber => $line) { |
| 72 | $currentLineNumber = $lineNumber + 2; |
| 73 | $aggregatedException = null; |
| 74 | |
| 75 | $parsedLine = $this->trimLine($this->convertCsvLineToArray($line)); |
| 76 | $headedLine = array_combine($header, $parsedLine); |
| 77 | |
| 78 | try { |
| 79 | $lineValidator->validate($headedLine, $template); |
| 80 | } catch (AggregatedValidationException $aggregatedException) { |
| 81 | $results->addException($currentLineNumber, $aggregatedException); |
| 82 | |
| 83 | if ($aggregatedException->hasErrors()) { |
| 84 | continue; |
| 85 | } |
| 86 | } catch (Throwable $exception) { |
| 87 | $results->addException($currentLineNumber, $exception); |
| 88 | |
| 89 | $logger->error( |
| 90 | sprintf( |
| 91 | 'Tabular import: Unexpected error on line %s: %s', |
| 92 | $currentLineNumber, |
| 93 | $exception->getMessage() |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | $item = $csvLineConverter->convert($headedLine, $template, $aggregatedException); |
| 99 | |
| 100 | if ($item->isNoneResponse()) { |
| 101 | $results->addException( |
| 102 | $currentLineNumber, |
| 103 | new WarningValidationException( |
| 104 | 'A value should be provided for at least one of the columns: %s', |
| 105 | [ |
| 106 | 'choice_[1-99]_score, correct_answer', |
| 107 | ] |
| 108 | ) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | $results->addItem($currentLineNumber, $item); |
| 113 | } |
| 114 | |
| 115 | $results->setTotalScannedItems(max($currentLineNumber - 1, 0)); |
| 116 | |
| 117 | return $results; |
| 118 | } |
| 119 | |
| 120 | private function trimLine(array $line): array |
| 121 | { |
| 122 | $newLine = []; |
| 123 | |
| 124 | foreach ($line as $value) { |
| 125 | $newLine[] = trim((string)$value); |
| 126 | } |
| 127 | |
| 128 | return $newLine; |
| 129 | } |
| 130 | |
| 131 | private function convertCsvLineToArray(string $line): array |
| 132 | { |
| 133 | return str_getcsv($this->removeBOM($line), $this->getCsvSeparator()); |
| 134 | } |
| 135 | |
| 136 | private function removeBOM(string $line): string |
| 137 | { |
| 138 | return str_replace("\xEF\xBB\xBF", '', $line); |
| 139 | } |
| 140 | |
| 141 | private function getHeaderValidator(): ValidatorInterface |
| 142 | { |
| 143 | return $this->getServiceLocator()->get(HeaderValidator::class); |
| 144 | } |
| 145 | |
| 146 | private function getLineValidator(): ValidatorInterface |
| 147 | { |
| 148 | return $this->getServiceLocator()->get(LineValidator::class); |
| 149 | } |
| 150 | |
| 151 | private function getCsvLineConverter(): CsvLineConverter |
| 152 | { |
| 153 | return $this->getServiceLocator()->get(CsvLineConverter::class); |
| 154 | } |
| 155 | } |