Total Complexity | 65 |
Total Lines | 431 |
Duplicated Lines | 0 % |
Changes | 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 |
||
21 | class CsvBulkLoader extends BulkLoader |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Delimiter character (Default: comma). |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | public $delimiter = ','; |
||
30 | |||
31 | /** |
||
32 | * Enclosure character (Default: doublequote) |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | public $enclosure = '"'; |
||
37 | |||
38 | /** |
||
39 | * Identifies if csv the has a header row. |
||
40 | * |
||
41 | * @var boolean |
||
42 | */ |
||
43 | public $hasHeaderRow = true; |
||
44 | |||
45 | /** |
||
46 | * Number of lines to split large CSV files into. |
||
47 | * |
||
48 | * @var int |
||
49 | * |
||
50 | * @config |
||
51 | */ |
||
52 | private static $lines = 1000; |
||
53 | |||
54 | /** |
||
55 | * @inheritDoc |
||
56 | */ |
||
57 | public function preview($filepath) |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $filepath |
||
64 | * @param boolean $preview |
||
65 | * |
||
66 | * @return null|BulkLoader_Result |
||
67 | */ |
||
68 | protected function processAll($filepath, $preview = false) |
||
69 | { |
||
70 | $previousDetectLE = ini_get('auto_detect_line_endings'); |
||
71 | |||
72 | ini_set('auto_detect_line_endings', true); |
||
|
|||
73 | try { |
||
74 | $filepath = Director::getAbsFile($filepath); |
||
75 | $csvReader = Reader::createFromPath($filepath, 'r'); |
||
76 | |||
77 | $tabExtractor = function ($row, $rowOffset, $iterator) { |
||
78 | foreach ($row as &$item) { |
||
79 | // [SS-2017-007] Ensure all cells with leading tab and then [@=+] have the tab removed on import |
||
80 | if (preg_match("/^\t[\-@=\+]+.*/", $item)) { |
||
81 | $item = ltrim($item, "\t"); |
||
82 | } |
||
83 | } |
||
84 | return $row; |
||
85 | }; |
||
86 | |||
87 | if ($this->columnMap) { |
||
88 | $headerMap = $this->getNormalisedColumnMap(); |
||
89 | $remapper = function ($row, $rowOffset, $iterator) use ($headerMap, $tabExtractor) { |
||
90 | $row = $tabExtractor($row, $rowOffset, $iterator); |
||
91 | foreach ($headerMap as $column => $renamedColumn) { |
||
92 | if ($column == $renamedColumn) { |
||
93 | continue; |
||
94 | } |
||
95 | if (array_key_exists($column, $row)) { |
||
96 | if (strpos($renamedColumn, '_ignore_') !== 0) { |
||
97 | $row[$renamedColumn] = $row[$column]; |
||
98 | } |
||
99 | unset($row[$column]); |
||
100 | } |
||
101 | } |
||
102 | return $row; |
||
103 | }; |
||
104 | } else { |
||
105 | $remapper = $tabExtractor; |
||
106 | } |
||
107 | |||
108 | if ($this->hasHeaderRow) { |
||
109 | $rows = $csvReader->fetchAssoc(0, $remapper); |
||
110 | } elseif ($this->columnMap) { |
||
111 | $rows = $csvReader->fetchAssoc($headerMap, $remapper); |
||
112 | } |
||
113 | |||
114 | $result = BulkLoader_Result::create(); |
||
115 | |||
116 | foreach ($rows as $row) { |
||
117 | $this->processRecord($row, $this->columnMap, $result, $preview); |
||
118 | } |
||
119 | } catch (\Exception $e) { |
||
120 | $failedMessage = sprintf("Failed to parse %s", $filepath); |
||
121 | if (Director::isDev()) { |
||
122 | $failedMessage = sprintf($failedMessage . " because %s", $e->getMessage()); |
||
123 | } |
||
124 | print $failedMessage . PHP_EOL; |
||
125 | } finally { |
||
126 | ini_set('auto_detect_line_endings', $previousDetectLE); |
||
127 | } |
||
128 | return $result; |
||
129 | } |
||
130 | |||
131 | protected function getNormalisedColumnMap() |
||
132 | { |
||
133 | $map = []; |
||
134 | foreach ($this->columnMap as $column => $newColumn) { |
||
135 | if (strpos($newColumn, "->") === 0) { |
||
136 | $map[$column] = $column; |
||
137 | } elseif (is_null($newColumn)) { |
||
138 | // the column map must consist of unique scalar values |
||
139 | // `null` can be present multiple times and is not scalar |
||
140 | // so we name it in a standard way so we can remove it later |
||
141 | $map[$column] = '_ignore_' . $column; |
||
142 | } else { |
||
143 | $map[$column] = $newColumn; |
||
144 | } |
||
145 | } |
||
146 | return $map; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Splits a large file up into many smaller files. |
||
151 | * |
||
152 | * @param string $path Path to large file to split |
||
153 | * @param int $lines Number of lines per file |
||
154 | * |
||
155 | * @return array List of file paths |
||
156 | */ |
||
157 | protected function splitFile($path, $lines = null) |
||
158 | { |
||
159 | Deprecation::notice('5.0', 'splitFile is deprecated, please process files using a stream'); |
||
160 | $previous = ini_get('auto_detect_line_endings'); |
||
161 | |||
162 | ini_set('auto_detect_line_endings', true); |
||
163 | |||
164 | if (!is_int($lines)) { |
||
165 | $lines = $this->config()->get("lines"); |
||
166 | } |
||
167 | |||
168 | $new = $this->getNewSplitFileName(); |
||
169 | |||
170 | $to = fopen($new, 'w+'); |
||
171 | $from = fopen($path, 'r'); |
||
172 | |||
173 | $header = null; |
||
174 | |||
175 | if ($this->hasHeaderRow) { |
||
176 | $header = fgets($from); |
||
177 | fwrite($to, $header); |
||
178 | } |
||
179 | |||
180 | $files = array(); |
||
181 | $files[] = $new; |
||
182 | |||
183 | $count = 0; |
||
184 | |||
185 | while (!feof($from)) { |
||
186 | fwrite($to, fgets($from)); |
||
187 | |||
188 | $count++; |
||
189 | |||
190 | if ($count >= $lines) { |
||
191 | fclose($to); |
||
192 | |||
193 | // get a new temporary file name, to write the next lines to |
||
194 | $new = $this->getNewSplitFileName(); |
||
195 | |||
196 | $to = fopen($new, 'w+'); |
||
197 | |||
198 | if ($this->hasHeaderRow) { |
||
199 | // add the headers to the new file |
||
200 | fwrite($to, $header); |
||
201 | } |
||
202 | |||
203 | $files[] = $new; |
||
204 | |||
205 | $count = 0; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | fclose($to); |
||
210 | |||
211 | ini_set('auto_detect_line_endings', $previous); |
||
212 | |||
213 | return $files; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return string |
||
218 | */ |
||
219 | protected function getNewSplitFileName() |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param string $filepath |
||
227 | * @param boolean $preview |
||
228 | * |
||
229 | * @return BulkLoader_Result |
||
230 | */ |
||
231 | protected function processChunk($filepath, $preview = false) |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @todo Better messages for relation checks and duplicate detection |
||
272 | * Note that columnMap isn't used. |
||
273 | * |
||
274 | * @param array $record |
||
275 | * @param array $columnMap |
||
276 | * @param BulkLoader_Result $results |
||
277 | * @param boolean $preview |
||
278 | * |
||
279 | * @return int |
||
280 | */ |
||
281 | protected function processRecord($record, $columnMap, &$results, $preview = false) |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Find an existing objects based on one or more uniqueness columns |
||
392 | * specified via {@link self::$duplicateChecks}. |
||
393 | * |
||
394 | * @todo support $columnMap |
||
395 | * |
||
396 | * @param array $record CSV data column |
||
397 | * @param array $columnMap |
||
398 | * @return DataObject |
||
399 | */ |
||
400 | public function findExistingObject($record, $columnMap = []) |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Determine whether any loaded files should be parsed with a |
||
445 | * header-row (otherwise we rely on {@link self::$columnMap}. |
||
446 | * |
||
447 | * @return boolean |
||
448 | */ |
||
449 | public function hasHeaderRow() |
||
454 |