Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
51.22% |
21 / 41 |
|
58.33% |
7 / 12 |
CRAP | |
0.00% |
0 / 1 |
| AgnosticImportHandler | |
51.22% |
21 / 41 |
|
58.33% |
7 / 12 |
50.55 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withFileProcessor | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withErrorHandler | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getForm | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| withLabel | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withForm | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| import | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getTaskParameters | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| processFile | |
16.67% |
2 / 12 |
|
0.00% |
0 / 1 |
4.31 | |||
| handleException | |
33.33% |
2 / 6 |
|
0.00% |
0 / 1 |
3.19 | |||
| getUploadedFile | |
0.00% |
0 / 1 |
|
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) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\import\service; |
| 24 | |
| 25 | use oat\oatbox\filesystem\File; |
| 26 | use oat\oatbox\reporting\Report; |
| 27 | use oat\tao\model\import\Processor\ImportFileErrorHandlerInterface; |
| 28 | use oat\tao\model\import\Processor\ImportFileProcessorInterface; |
| 29 | use oat\tao\model\upload\UploadService; |
| 30 | use tao_helpers_form_Form; |
| 31 | use oat\tao\model\import\TaskParameterProviderInterface; |
| 32 | use tao_models_classes_import_CsvUploadForm; |
| 33 | use tao_models_classes_import_ImportHandler; |
| 34 | use Throwable; |
| 35 | |
| 36 | class AgnosticImportHandler implements tao_models_classes_import_ImportHandler, TaskParameterProviderInterface |
| 37 | { |
| 38 | public const STATISTICAL_METADATA_SERVICE_ID = self::class . '::STATISTICAL_METADATA'; |
| 39 | |
| 40 | /** @var UploadService */ |
| 41 | private $uploadService; |
| 42 | |
| 43 | /** @var string|null */ |
| 44 | private $label; |
| 45 | |
| 46 | /** @var tao_helpers_form_Form|null */ |
| 47 | private $form; |
| 48 | |
| 49 | /** @var ImportFileProcessorInterface|null */ |
| 50 | private $fileProcessor; |
| 51 | |
| 52 | /** @var ImportFileErrorHandlerInterface|null */ |
| 53 | private $errorHandler; |
| 54 | |
| 55 | public function __construct(UploadService $uploadService) |
| 56 | { |
| 57 | $this->uploadService = $uploadService; |
| 58 | } |
| 59 | |
| 60 | public function withFileProcessor(ImportFileProcessorInterface $fileProcessor): self |
| 61 | { |
| 62 | $this->fileProcessor = $fileProcessor; |
| 63 | |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | public function withErrorHandler(ImportFileErrorHandlerInterface $errorHandler): self |
| 68 | { |
| 69 | $this->errorHandler = $errorHandler; |
| 70 | |
| 71 | return $this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @inheritdoc |
| 76 | */ |
| 77 | public function getLabel() |
| 78 | { |
| 79 | return $this->label ?? 'Import'; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @inheritdoc |
| 84 | */ |
| 85 | public function getForm() |
| 86 | { |
| 87 | if ($this->form) { |
| 88 | return $this->form; |
| 89 | } |
| 90 | |
| 91 | return (new tao_models_classes_import_CsvUploadForm())->getForm(); |
| 92 | } |
| 93 | |
| 94 | public function withLabel(string $label): self |
| 95 | { |
| 96 | $this->label = $label; |
| 97 | |
| 98 | return $this; |
| 99 | } |
| 100 | |
| 101 | public function withForm(tao_helpers_form_Form $form): self |
| 102 | { |
| 103 | $this->form = $form; |
| 104 | |
| 105 | return $this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @inheritdoc |
| 110 | */ |
| 111 | public function import($class, $form, $userId = null) |
| 112 | { |
| 113 | try { |
| 114 | $uploadedFile = $this->uploadService->fetchUploadedFile($form); |
| 115 | |
| 116 | return $this->processFile($uploadedFile); |
| 117 | } catch (Throwable $exception) { |
| 118 | return $this->handleException($exception); |
| 119 | } finally { |
| 120 | $this->uploadService->remove($uploadedFile); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @inheritDoc |
| 126 | */ |
| 127 | public function getTaskParameters(tao_helpers_form_Form $importForm): array |
| 128 | { |
| 129 | $file = $this->uploadService->getUploadedFlyFile($this->getUploadedFile($importForm)); |
| 130 | |
| 131 | return [ |
| 132 | 'uploaded_file' => $file->getPrefix(), |
| 133 | ]; |
| 134 | } |
| 135 | |
| 136 | private function processFile(File $file): Report |
| 137 | { |
| 138 | if ($this->fileProcessor) { |
| 139 | return $this->fileProcessor->process($file); |
| 140 | } |
| 141 | |
| 142 | $subReport = Report::create( |
| 143 | Report::TYPE_SUCCESS, |
| 144 | 'Imported %s with success', |
| 145 | [ |
| 146 | $file->getBasename() |
| 147 | ] |
| 148 | )->setData([]); |
| 149 | |
| 150 | return Report::createSuccess(__('Imported successfully finished!')) |
| 151 | ->setData([]) |
| 152 | ->add($subReport); |
| 153 | } |
| 154 | |
| 155 | private function handleException(Throwable $exception): Report |
| 156 | { |
| 157 | if ($this->errorHandler) { |
| 158 | return $this->errorHandler->handle($exception); |
| 159 | } |
| 160 | |
| 161 | return Report::create( |
| 162 | Report::TYPE_ERROR, |
| 163 | $exception->getMessage() |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | private function getUploadedFile(tao_helpers_form_Form $importForm): string |
| 168 | { |
| 169 | return $importForm->getValue('importFile') ?: $importForm->getValue('source')['uploaded_file']; |
| 170 | } |
| 171 | } |