Total Complexity | 60 |
Total Lines | 365 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like ExcelImportExport 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 ExcelImportExport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class ExcelImportExport |
||
19 | { |
||
20 | use Configurable; |
||
21 | |||
22 | /** |
||
23 | * Get all db fields for a given dataobject class |
||
24 | * |
||
25 | * @param string $class |
||
26 | * @return array |
||
27 | */ |
||
28 | public static function allFieldsForClass($class) |
||
29 | { |
||
30 | $dataClasses = ClassInfo::dataClassesFor($class); |
||
31 | $fields = array(); |
||
32 | $dataObjectSchema = DataObject::getSchema(); |
||
33 | foreach ($dataClasses as $dataClass) { |
||
34 | $dataFields = $dataObjectSchema->databaseFields($dataClass); |
||
35 | $fields = array_merge($fields, array_keys($dataFields)); |
||
36 | } |
||
37 | return array_combine($fields, $fields); |
||
|
|||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get fields that should be exported by default for a class |
||
42 | * |
||
43 | * @param string $class |
||
44 | * @return array |
||
45 | */ |
||
46 | public static function exportFieldsForClass($class) |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get fields that can be imported by default for a class |
||
77 | * |
||
78 | * @param string $class |
||
79 | * @return array |
||
80 | */ |
||
81 | public static function importFieldsForClass($class) |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Output a sample file for a class |
||
103 | * |
||
104 | * A custom file can be provided with a custom sampleExcelFile method |
||
105 | * either as a file path or as a Excel instance |
||
106 | * |
||
107 | * @param string $class |
||
108 | * @return void |
||
109 | */ |
||
110 | public static function sampleFileForClass($class) |
||
111 | { |
||
112 | $fileName = "sample-file-for-$class.xlsx"; |
||
113 | $spreadsheet = null; |
||
114 | |||
115 | $sng = singleton($class); |
||
116 | if ($sng->hasMethod('sampleExcelFile')) { |
||
117 | $spreadsheet = $sng->sampleExcelFile(); |
||
118 | |||
119 | // We have a file, output directly |
||
120 | if (is_string($spreadsheet) && is_file($spreadsheet)) { |
||
121 | self::outputHeaders($fileName); |
||
122 | readfile($spreadsheet); |
||
123 | exit(); |
||
124 | } |
||
125 | } |
||
126 | if (!$spreadsheet) { |
||
127 | $spreadsheet = self::generateDefaultSampleFile($class); |
||
128 | } |
||
129 | |||
130 | $writer = self::getDefaultWriter($spreadsheet); |
||
131 | self::outputHeaders($fileName); |
||
132 | $writer->save('php://output'); |
||
133 | exit(); |
||
134 | } |
||
135 | |||
136 | public static function getDefaultWriter($spreadsheet) |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Output excel headers |
||
143 | * |
||
144 | * @param string $fileName |
||
145 | * @return void |
||
146 | */ |
||
147 | public static function outputHeaders($fileName) |
||
148 | { |
||
149 | $ext = pathinfo($fileName, PATHINFO_EXTENSION); |
||
150 | switch ($ext) { |
||
151 | case 'xlsx': |
||
152 | header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); |
||
153 | break; |
||
154 | default: |
||
155 | header('Content-type: application/vnd.ms-excel'); |
||
156 | break; |
||
157 | } |
||
158 | |||
159 | header('Content-Disposition: attachment; filename="' . $fileName . '"'); |
||
160 | header('Cache-Control: max-age=0'); |
||
161 | ob_clean(); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Generate a default import file with all field name |
||
166 | * |
||
167 | * @param string $class |
||
168 | * @return Spreadsheet |
||
169 | */ |
||
170 | public static function generateDefaultSampleFile($class) |
||
171 | { |
||
172 | $spreadsheet = new Spreadsheet(); |
||
173 | $spreadsheet->getProperties() |
||
174 | ->setCreator('SilverStripe') |
||
175 | ->setTitle("Sample file for $class"); |
||
176 | $sheet = $spreadsheet->getActiveSheet(); |
||
177 | |||
178 | $row = 1; |
||
179 | $col = 1; |
||
180 | $allFields = ExcelImportExport::importFieldsForClass($class); |
||
181 | foreach ($allFields as $header) { |
||
182 | $sheet->setCellValueByColumnAndRow($col, $row, $header); |
||
183 | $col++; |
||
184 | } |
||
185 | return $spreadsheet; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Show valid extensions helper (for uploaders) |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public static function getValidExtensionsText() |
||
194 | { |
||
195 | return _t( |
||
196 | 'ExcelImportExport.VALIDEXTENSIONS', |
||
197 | "Allowed extensions: {extensions}", |
||
198 | array('extensions' => implode(', ', self::getValidExtensions())) |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Extracted from PHPSpreadhseet |
||
204 | * |
||
205 | * @param string $ext |
||
206 | * @return string |
||
207 | */ |
||
208 | public static function getReaderForExtension($ext) |
||
209 | { |
||
210 | switch (strtolower($ext)) { |
||
211 | case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet |
||
212 | case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded) |
||
213 | case 'xltx': // Excel (OfficeOpenXML) Template |
||
214 | case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded) |
||
215 | return 'Xlsx'; |
||
216 | case 'xls': // Excel (BIFF) Spreadsheet |
||
217 | case 'xlt': // Excel (BIFF) Template |
||
218 | return 'Xls'; |
||
219 | case 'ods': // Open/Libre Offic Calc |
||
220 | case 'ots': // Open/Libre Offic Calc Template |
||
221 | return 'Ods'; |
||
222 | case 'slk': |
||
223 | return 'Slk'; |
||
224 | case 'xml': // Excel 2003 SpreadSheetML |
||
225 | return 'Xml'; |
||
226 | case 'gnumeric': |
||
227 | return 'Gnumeric'; |
||
228 | case 'htm': |
||
229 | case 'html': |
||
230 | return 'Html'; |
||
231 | case 'csv': |
||
232 | return 'Csv'; |
||
233 | default: |
||
234 | throw new Exception("Unsupported file type : $ext"); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get valid extensions |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | public static function getValidExtensions() |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Convert a file to an array |
||
254 | * |
||
255 | * @param string $filepath |
||
256 | * @param string $delimiter (csv only) |
||
257 | * @param string $enclosure (csv only) |
||
258 | * @return array |
||
259 | */ |
||
260 | public static function fileToArray($filepath, $delimiter = ';', $enclosure = '') |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Convert an excel file to an array |
||
286 | * |
||
287 | * @param string $filepath |
||
288 | * @param string $sheetname Load a specific worksheet by name |
||
289 | * @param true $onlyExisting Avoid reading empty columns |
||
290 | * @return array |
||
291 | */ |
||
292 | public static function excelToArray($filepath, $sheetname = null, $onlyExisting = true) |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Convert an excel file to an associative array |
||
326 | * |
||
327 | * Suppose the first line are the headers of the file |
||
328 | * Headers are trimmed in case you have crappy whitespace in your files |
||
329 | * |
||
330 | * @param string $filepath |
||
331 | * @param string $sheetname Load a specific worksheet by name |
||
332 | * @return array |
||
333 | */ |
||
334 | public static function excelToAssocArray($filepath, $sheetname = null) |
||
385 |
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.