Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
8.00% |
2 / 25 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CsvItemImporter | |
8.00% |
2 / 25 |
|
28.57% |
2 / 7 |
87.87 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getForm | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| import | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| getTemplateRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCsvImporter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getReportBuilder | |
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) 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; |
| 24 | |
| 25 | use oat\generis\model\OntologyAwareTrait; |
| 26 | use oat\oatbox\log\LoggerAwareTrait; |
| 27 | use oat\taoQtiItem\model\import\Report\ReportBuilder; |
| 28 | use Throwable; |
| 29 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 30 | use oat\oatbox\PhpSerializeStateless; |
| 31 | use oat\oatbox\event\EventManagerAwareTrait; |
| 32 | use oat\taoQtiItem\model\import\Repository\CsvTemplateRepository; |
| 33 | use oat\taoQtiItem\model\import\Repository\TemplateRepositoryInterface; |
| 34 | use oat\tao\model\import\ImportHandlerHelperTrait; |
| 35 | use oat\tao\model\import\TaskParameterProviderInterface; |
| 36 | use tao_models_classes_import_ImportHandler; |
| 37 | use Psr\Http\Message\ServerRequestInterface; |
| 38 | |
| 39 | class CsvItemImporter implements |
| 40 | tao_models_classes_import_ImportHandler, |
| 41 | ServiceLocatorAwareInterface, |
| 42 | TaskParameterProviderInterface |
| 43 | { |
| 44 | use LoggerAwareTrait; |
| 45 | use OntologyAwareTrait; |
| 46 | use PhpSerializeStateless; |
| 47 | use EventManagerAwareTrait; |
| 48 | use ImportHandlerHelperTrait { |
| 49 | getTaskParameters as getDefaultTaskParameters; |
| 50 | } |
| 51 | |
| 52 | private const DEFAULT_CSV_SEPARATOR = ';'; |
| 53 | |
| 54 | /** @var ServerRequestInterface|null */ |
| 55 | private $request; |
| 56 | |
| 57 | public function __construct(ServerRequestInterface $request = null) |
| 58 | { |
| 59 | $this->request = $request; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @inheritdoc |
| 64 | */ |
| 65 | public function getLabel() |
| 66 | { |
| 67 | return __('CSV content + metadata'); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @inheritdoc |
| 72 | */ |
| 73 | public function getForm() |
| 74 | { |
| 75 | $form = new CsvImportForm( |
| 76 | [], |
| 77 | [ |
| 78 | 'classUri' => $this->request ? $this->request->getParsedBody()['classUri'] : null |
| 79 | ] |
| 80 | ); |
| 81 | |
| 82 | return $form->getForm(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @inheritdoc |
| 87 | */ |
| 88 | public function import($class, $form, $userId = null) |
| 89 | { |
| 90 | $reportBuilder = $this->getReportBuilder(); |
| 91 | |
| 92 | try { |
| 93 | $uploadedFile = $this->fetchUploadedFile($form); |
| 94 | $template = $this->getTemplateRepository()->findById(CsvTemplateRepository::DEFAULT); |
| 95 | |
| 96 | $importer = $this->getCsvImporter(); |
| 97 | $importer->setCsvSeparator(self::DEFAULT_CSV_SEPARATOR); //@TODO Get it from UI/Task |
| 98 | |
| 99 | $importResults = $importer->import($uploadedFile, $template, $class); |
| 100 | |
| 101 | $report = $reportBuilder->buildByResults($importResults, $importResults->getFirstItem()); |
| 102 | } catch (Throwable $e) { |
| 103 | $report = $reportBuilder->buildByException($e); |
| 104 | |
| 105 | $this->getLogger()->warning($report->getMessage()); |
| 106 | } finally { |
| 107 | if (isset($uploadedFile)) { |
| 108 | $this->getUploadService()->remove($uploadedFile); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $report; |
| 113 | } |
| 114 | |
| 115 | private function getTemplateRepository(): TemplateRepositoryInterface |
| 116 | { |
| 117 | return $this->getServiceLocator()->get(CsvTemplateRepository::class); |
| 118 | } |
| 119 | |
| 120 | private function getCsvImporter(): CsvItemImportHandler |
| 121 | { |
| 122 | return $this->getServiceLocator()->get(CsvItemImportHandler::class); |
| 123 | } |
| 124 | |
| 125 | private function getReportBuilder(): ReportBuilder |
| 126 | { |
| 127 | return $this->getServiceLocator()->get(ReportBuilder::class); |
| 128 | } |
| 129 | } |