Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AbstractImportService | |
0.00% |
0 / 56 |
|
0.00% |
0 / 4 |
380 | |
0.00% |
0 / 1 |
| persist | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| import | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
156 | |||
| getMapper | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setMapper | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getCsvControls | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| 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 oat\oatbox\filesystem\File; |
| 24 | use oat\oatbox\log\LoggerAwareTrait; |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use common_report_Report as Report; |
| 27 | |
| 28 | abstract class AbstractImportService extends ConfigurableService implements ImportServiceInterface |
| 29 | { |
| 30 | use LoggerAwareTrait; |
| 31 | |
| 32 | /** @var array Default CSV controls */ |
| 33 | protected $csvControls = [ |
| 34 | 'delimiter' => ',', |
| 35 | 'enclosure' => '"', |
| 36 | 'escape' => '\\', |
| 37 | ]; |
| 38 | |
| 39 | /** @var array */ |
| 40 | protected $headerColumns = []; |
| 41 | |
| 42 | /** @var ImportMapperInterface */ |
| 43 | protected $mapper; |
| 44 | |
| 45 | /** |
| 46 | * @param ImportMapperInterface $mapper |
| 47 | * @return \core_kernel_classes_Resource |
| 48 | */ |
| 49 | abstract protected function persist(ImportMapperInterface $mapper); |
| 50 | |
| 51 | /** |
| 52 | * @param $file |
| 53 | * @param array $extraProperties |
| 54 | * @param array $options |
| 55 | * @return Report |
| 56 | * @throws \Exception |
| 57 | * @throws \common_exception_Error |
| 58 | */ |
| 59 | public function import($file, $extraProperties = [], $options = []) |
| 60 | { |
| 61 | $report = \common_report_Report::createInfo(); |
| 62 | |
| 63 | if ($file instanceof File) { |
| 64 | if ($file->exists()) { |
| 65 | $fileHandler = $file->readStream(); |
| 66 | } else { |
| 67 | throw new \Exception('File to import cannot be loaded.'); |
| 68 | } |
| 69 | } elseif (!file_exists($file) || !is_readable($file) || ($fileHandler = fopen($file, 'r')) === false) { |
| 70 | throw new \Exception('File to import cannot be loaded.'); |
| 71 | } |
| 72 | |
| 73 | $csvControls = $this->getCsvControls($options); |
| 74 | list($delimiter, $enclosure, $escape) = array_values($csvControls); |
| 75 | $index = 0; |
| 76 | while (($line = fgetcsv($fileHandler, 0, $delimiter, $enclosure, $escape)) !== false) { |
| 77 | $index++; |
| 78 | $data = array_map('trim', $line); |
| 79 | try { |
| 80 | if ($index === 1) { |
| 81 | $this->headerColumns = array_map('strtolower', $data); |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | if (count($this->headerColumns) !== count($data)) { |
| 86 | $message = 'CSV file is malformed at line ' . $index . '. Data skipped'; |
| 87 | $this->logWarning($message); |
| 88 | $report->add(Report::createFailure($message)); |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | $combinedRow = array_combine($this->headerColumns, $data); |
| 93 | $combinedRow = array_merge($combinedRow, $extraProperties); |
| 94 | |
| 95 | $mapper = $this->getMapper()->map($combinedRow)->combine($extraProperties); |
| 96 | $report->add($mapper->getReport()); |
| 97 | |
| 98 | if ($mapper->isEmpty()) { |
| 99 | $message = 'Mapper doesn\'t achieve to extract data for line ' . $index . '. Data skipped'; |
| 100 | $this->logWarning($message); |
| 101 | $report->add(Report::createFailure($message)); |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | $resource = $this->persist($mapper); |
| 106 | $message = 'Resource imported with success: ' . $resource->getUri(); |
| 107 | $this->logInfo($message); |
| 108 | $report->add(Report::createSuccess($message)); |
| 109 | } catch (\Exception $exception) { |
| 110 | $report->add(Report::createFailure($exception->getMessage())); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if ($report->containsError()) { |
| 115 | $report->setMessage(__('Import failed.')); |
| 116 | $report->setType(Report::TYPE_ERROR); |
| 117 | } else { |
| 118 | $report->setMessage(__('Import succeeded.')); |
| 119 | $report->setType(Report::TYPE_SUCCESS); |
| 120 | } |
| 121 | |
| 122 | return $report; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get the mapper |
| 127 | * |
| 128 | * @return ImportMapperInterface |
| 129 | */ |
| 130 | public function getMapper() |
| 131 | { |
| 132 | if (is_null($this->mapper)) { |
| 133 | throw new \LogicException('Mapper is not initialized and importer cannot process.'); |
| 134 | } |
| 135 | |
| 136 | return $this->mapper; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Set the mapper |
| 141 | * |
| 142 | * @param ImportMapperInterface $mapper |
| 143 | * @return $this |
| 144 | */ |
| 145 | public function setMapper(ImportMapperInterface $mapper) |
| 146 | { |
| 147 | $this->mapper = $mapper; |
| 148 | |
| 149 | return $this; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Merge the given $options csv controls to default |
| 154 | * |
| 155 | * @param array $options |
| 156 | * @return array |
| 157 | */ |
| 158 | protected function getCsvControls(array $options) |
| 159 | { |
| 160 | $csvControls = $this->csvControls; |
| 161 | if (isset($options['delimiter'])) { |
| 162 | $csvControls['delimiter'] = $options['delimiter']; |
| 163 | } |
| 164 | if (isset($options['enclosure'])) { |
| 165 | $csvControls['enclosure'] = $options['enclosure']; |
| 166 | } |
| 167 | if (isset($options['escape'])) { |
| 168 | $csvControls['escape'] = $options['escape']; |
| 169 | } |
| 170 | return $csvControls; |
| 171 | } |
| 172 | } |