| Total Complexity | 65 |
| Total Lines | 437 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CsvBulkLoader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CsvBulkLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class CsvBulkLoader extends BulkLoader |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Delimiter character (Default: comma). |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $delimiter = ','; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Enclosure character (Default: doublequote) |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public $enclosure = '"'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Identifies if csv the has a header row. |
||
| 41 | * |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | public $hasHeaderRow = true; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Number of lines to split large CSV files into. |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | * |
||
| 51 | * @config |
||
| 52 | */ |
||
| 53 | private static $lines = 1000; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @inheritDoc |
||
| 57 | */ |
||
| 58 | public function preview($filepath) |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $filepath |
||
| 65 | * @param boolean $preview |
||
| 66 | * |
||
| 67 | * @return null|BulkLoader_Result |
||
| 68 | */ |
||
| 69 | protected function processAll($filepath, $preview = false) |
||
| 70 | { |
||
| 71 | $previousDetectLE = ini_get('auto_detect_line_endings'); |
||
| 72 | |||
| 73 | ini_set('auto_detect_line_endings', true); |
||
|
|
|||
| 74 | $result = BulkLoader_Result::create(); |
||
| 75 | |||
| 76 | try { |
||
| 77 | $filepath = Director::getAbsFile($filepath); |
||
| 78 | $csvReader = Reader::createFromPath($filepath, 'r'); |
||
| 79 | $csvReader->setDelimiter($this->delimiter); |
||
| 80 | $csvReader->skipInputBOM(); |
||
| 81 | |||
| 82 | $tabExtractor = function ($row) { |
||
| 83 | foreach ($row as &$item) { |
||
| 84 | // [SS-2017-007] Ensure all cells with leading tab and then [@=+] have the tab removed on import |
||
| 85 | $specialChars = implode('', EscapeFormula::FORMULA_STARTING_CHARS); |
||
| 86 | $specialChars = preg_quote($specialChars, '/'); |
||
| 87 | if (preg_match("/^\t[$specialChars]+.*/", $item)) { |
||
| 88 | $item = ltrim($item, "\t"); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | return $row; |
||
| 92 | }; |
||
| 93 | |||
| 94 | if ($this->columnMap) { |
||
| 95 | $headerMap = $this->getNormalisedColumnMap(); |
||
| 96 | $remapper = function ($row) use ($headerMap, $tabExtractor) { |
||
| 97 | $row = $tabExtractor($row); |
||
| 98 | foreach ($headerMap as $column => $renamedColumn) { |
||
| 99 | if ($column == $renamedColumn) { |
||
| 100 | continue; |
||
| 101 | } |
||
| 102 | if (array_key_exists($column, $row)) { |
||
| 103 | if (strpos($renamedColumn, '_ignore_') !== 0) { |
||
| 104 | $row[$renamedColumn] = $row[$column]; |
||
| 105 | } |
||
| 106 | unset($row[$column]); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | return $row; |
||
| 110 | }; |
||
| 111 | } else { |
||
| 112 | $remapper = $tabExtractor; |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($this->hasHeaderRow) { |
||
| 116 | $csvReader->setHeaderOffset(0); |
||
| 117 | $rows = $csvReader->getRecords(); |
||
| 118 | } elseif ($this->columnMap) { |
||
| 119 | $rows = $csvReader->getRecords($headerMap); |
||
| 120 | } |
||
| 121 | |||
| 122 | foreach ($rows as $row) { |
||
| 123 | $row = $remapper($row); |
||
| 124 | $this->processRecord($row, $this->columnMap, $result, $preview); |
||
| 125 | } |
||
| 126 | } catch (\Exception $e) { |
||
| 127 | $failedMessage = sprintf("Failed to parse %s", $filepath); |
||
| 128 | if (Director::isDev()) { |
||
| 129 | $failedMessage = sprintf($failedMessage . " because %s", $e->getMessage()); |
||
| 130 | } |
||
| 131 | print $failedMessage . PHP_EOL; |
||
| 132 | } finally { |
||
| 133 | ini_set('auto_detect_line_endings', $previousDetectLE); |
||
| 134 | } |
||
| 135 | return $result; |
||
| 136 | } |
||
| 137 | |||
| 138 | protected function getNormalisedColumnMap() |
||
| 139 | { |
||
| 140 | $map = []; |
||
| 141 | foreach ($this->columnMap as $column => $newColumn) { |
||
| 142 | if (strpos($newColumn, "->") === 0) { |
||
| 143 | $map[$column] = $column; |
||
| 144 | } elseif (is_null($newColumn)) { |
||
| 145 | // the column map must consist of unique scalar values |
||
| 146 | // `null` can be present multiple times and is not scalar |
||
| 147 | // so we name it in a standard way so we can remove it later |
||
| 148 | $map[$column] = '_ignore_' . $column; |
||
| 149 | } else { |
||
| 150 | $map[$column] = $newColumn; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | return $map; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Splits a large file up into many smaller files. |
||
| 158 | * |
||
| 159 | * @param string $path Path to large file to split |
||
| 160 | * @param int $lines Number of lines per file |
||
| 161 | * |
||
| 162 | * @return array List of file paths |
||
| 163 | */ |
||
| 164 | protected function splitFile($path, $lines = null) |
||
| 165 | { |
||
| 166 | Deprecation::notice('5.0', 'splitFile is deprecated, please process files using a stream'); |
||
| 167 | $previous = ini_get('auto_detect_line_endings'); |
||
| 168 | |||
| 169 | ini_set('auto_detect_line_endings', true); |
||
| 170 | |||
| 171 | if (!is_int($lines)) { |
||
| 172 | $lines = $this->config()->get("lines"); |
||
| 173 | } |
||
| 174 | |||
| 175 | $new = $this->getNewSplitFileName(); |
||
| 176 | |||
| 177 | $to = fopen($new, 'w+'); |
||
| 178 | $from = fopen($path, 'r'); |
||
| 179 | |||
| 180 | $header = null; |
||
| 181 | |||
| 182 | if ($this->hasHeaderRow) { |
||
| 183 | $header = fgets($from); |
||
| 184 | fwrite($to, $header); |
||
| 185 | } |
||
| 186 | |||
| 187 | $files = array(); |
||
| 188 | $files[] = $new; |
||
| 189 | |||
| 190 | $count = 0; |
||
| 191 | |||
| 192 | while (!feof($from)) { |
||
| 193 | fwrite($to, fgets($from)); |
||
| 194 | |||
| 195 | $count++; |
||
| 196 | |||
| 197 | if ($count >= $lines) { |
||
| 198 | fclose($to); |
||
| 199 | |||
| 200 | // get a new temporary file name, to write the next lines to |
||
| 201 | $new = $this->getNewSplitFileName(); |
||
| 202 | |||
| 203 | $to = fopen($new, 'w+'); |
||
| 204 | |||
| 205 | if ($this->hasHeaderRow) { |
||
| 206 | // add the headers to the new file |
||
| 207 | fwrite($to, $header); |
||
| 208 | } |
||
| 209 | |||
| 210 | $files[] = $new; |
||
| 211 | |||
| 212 | $count = 0; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | fclose($to); |
||
| 217 | |||
| 218 | ini_set('auto_detect_line_endings', $previous); |
||
| 219 | |||
| 220 | return $files; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | protected function getNewSplitFileName() |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $filepath |
||
| 234 | * @param boolean $preview |
||
| 235 | * |
||
| 236 | * @return BulkLoader_Result |
||
| 237 | */ |
||
| 238 | protected function processChunk($filepath, $preview = false) |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @todo Better messages for relation checks and duplicate detection |
||
| 279 | * Note that columnMap isn't used. |
||
| 280 | * |
||
| 281 | * @param array $record |
||
| 282 | * @param array $columnMap |
||
| 283 | * @param BulkLoader_Result $results |
||
| 284 | * @param boolean $preview |
||
| 285 | * |
||
| 286 | * @return int |
||
| 287 | */ |
||
| 288 | protected function processRecord($record, $columnMap, &$results, $preview = false) |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Find an existing objects based on one or more uniqueness columns |
||
| 399 | * specified via {@link self::$duplicateChecks}. |
||
| 400 | * |
||
| 401 | * @todo support $columnMap |
||
| 402 | * |
||
| 403 | * @param array $record CSV data column |
||
| 404 | * @param array $columnMap |
||
| 405 | * @return DataObject |
||
| 406 | */ |
||
| 407 | public function findExistingObject($record, $columnMap = []) |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Determine whether any loaded files should be parsed with a |
||
| 452 | * header-row (otherwise we rely on {@link self::$columnMap}. |
||
| 453 | * |
||
| 454 | * @return boolean |
||
| 455 | */ |
||
| 456 | public function hasHeaderRow() |
||
| 459 | } |
||
| 460 | } |
||
| 461 |