Total Complexity | 42 |
Total Lines | 435 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Import 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 Import, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Import |
||
35 | { |
||
36 | /** |
||
37 | * @var DoliDB Database handler. |
||
|
|||
38 | */ |
||
39 | public $db; |
||
40 | |||
41 | /** |
||
42 | * @var string Error code (or message) |
||
43 | */ |
||
44 | public $error = ''; |
||
45 | |||
46 | /** |
||
47 | * @var string[] Error codes (or messages) |
||
48 | */ |
||
49 | public $errors = array(); |
||
50 | |||
51 | /** |
||
52 | * @var string DB Error number |
||
53 | */ |
||
54 | public $errno; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | public $array_import_module; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | public $array_import_perms; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | public $array_import_icon; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | public $array_import_code; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | public $array_import_label; |
||
80 | |||
81 | /** |
||
82 | * @var array |
||
83 | */ |
||
84 | public $array_import_tables; |
||
85 | |||
86 | /** |
||
87 | * @var array |
||
88 | */ |
||
89 | public $array_import_tables_creator; |
||
90 | |||
91 | /** |
||
92 | * @var array |
||
93 | */ |
||
94 | public $array_import_fields; |
||
95 | |||
96 | /** |
||
97 | * @var array |
||
98 | */ |
||
99 | public $array_import_fieldshidden; |
||
100 | |||
101 | /** |
||
102 | * @var array |
||
103 | */ |
||
104 | public $array_import_entities; |
||
105 | |||
106 | /** |
||
107 | * @var array |
||
108 | */ |
||
109 | public $array_import_regex; |
||
110 | |||
111 | /** |
||
112 | * @var array |
||
113 | */ |
||
114 | public $array_import_updatekeys; |
||
115 | |||
116 | /** |
||
117 | * @var array |
||
118 | */ |
||
119 | public $array_import_preselected_updatekeys; |
||
120 | |||
121 | /** |
||
122 | * @var array |
||
123 | */ |
||
124 | public $array_import_examplevalues; |
||
125 | |||
126 | /** |
||
127 | * @var array |
||
128 | */ |
||
129 | public $array_import_convertvalue; |
||
130 | |||
131 | /** |
||
132 | * @var array |
||
133 | */ |
||
134 | public $array_import_run_sql_after; |
||
135 | |||
136 | // To store import templates |
||
137 | public $id; |
||
138 | public $hexa; // List of fields in the export profile |
||
139 | public $datatoimport; |
||
140 | |||
141 | /** |
||
142 | * @var string Name of export profile |
||
143 | */ |
||
144 | public $model_name; |
||
145 | |||
146 | /** |
||
147 | * @var int ID |
||
148 | */ |
||
149 | public $fk_user; |
||
150 | |||
151 | |||
152 | /** |
||
153 | * Constructor |
||
154 | * |
||
155 | * @param DoliDB $db Database handler |
||
156 | */ |
||
157 | public function __construct($db) |
||
158 | { |
||
159 | $this->db = $db; |
||
160 | } |
||
161 | |||
162 | |||
163 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
164 | /** |
||
165 | * Load description int this->array_import_module, this->array_import_fields, ... of an importable dataset |
||
166 | * |
||
167 | * @param User $user Object user making import |
||
168 | * @param string $filter Load a particular dataset only. Index will start to 0. |
||
169 | * @return int Return integer <0 if KO, >0 if OK |
||
170 | */ |
||
171 | public function load_arrays($user, $filter = '') |
||
289 | } |
||
290 | |||
291 | |||
292 | |||
293 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
294 | /** |
||
295 | * Build an import example file. |
||
296 | * Arrays this->array_export_xxx are already loaded for required datatoexport |
||
297 | * |
||
298 | * @param string $model Name of import engine ('csv', ...) |
||
299 | * @param string $headerlinefields Array of values for first line of example file |
||
300 | * @param string $contentlinevalues Array of values for content line of example file |
||
301 | * @param string $datatoimport Dataset to import |
||
302 | * @return string Return integer <0 if KO, >0 if OK |
||
303 | */ |
||
304 | public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport) |
||
305 | { |
||
306 | // phpcs:enable |
||
307 | global $conf, $langs; |
||
308 | |||
309 | $indice = 0; |
||
310 | |||
311 | dol_syslog(get_class($this) . "::build_example_file " . $model); |
||
312 | |||
313 | // Create the import class for the model Import_XXX |
||
314 | $dir = DOL_DOCUMENT_ROOT . "/core/modules/import/"; |
||
315 | $file = "import_" . $model . ".modules.php"; |
||
316 | $classname = "Import" . $model; |
||
317 | require_once $dir . $file; |
||
318 | $objmodel = new $classname($this->db, $datatoimport); |
||
319 | |||
320 | $outputlangs = $langs; // Lang for output |
||
321 | $s = ''; |
||
322 | |||
323 | // Genere en-tete |
||
324 | $s .= $objmodel->write_header_example($outputlangs); |
||
325 | |||
326 | // Genere ligne de titre |
||
327 | $s .= $objmodel->write_title_example($outputlangs, $headerlinefields); |
||
328 | |||
329 | // Genere ligne de titre |
||
330 | $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues); |
||
331 | |||
332 | // Genere pied de page |
||
333 | $s .= $objmodel->write_footer_example($outputlangs); |
||
334 | |||
335 | return $s; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Save an export model in database |
||
340 | * |
||
341 | * @param User $user Object user that save |
||
342 | * @return int Return integer <0 if KO, >0 if OK |
||
343 | */ |
||
344 | public function create($user) |
||
345 | { |
||
346 | global $conf; |
||
347 | |||
348 | dol_syslog("Import.class.php::create"); |
||
349 | |||
350 | // Check parameters |
||
351 | if (empty($this->model_name)) { |
||
352 | $this->error = 'ErrorWrongParameters'; |
||
353 | return -1; |
||
354 | } |
||
355 | if (empty($this->datatoimport)) { |
||
356 | $this->error = 'ErrorWrongParameters'; |
||
357 | return -1; |
||
358 | } |
||
359 | if (empty($this->hexa)) { |
||
360 | $this->error = 'ErrorWrongParameters'; |
||
361 | return -1; |
||
362 | } |
||
363 | |||
364 | $this->db->begin(); |
||
365 | |||
366 | $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'import_model ('; |
||
367 | $sql .= 'fk_user,'; |
||
368 | $sql .= ' label,'; |
||
369 | $sql .= ' type,'; |
||
370 | $sql .= ' field'; |
||
371 | $sql .= ')'; |
||
372 | $sql .= " VALUES ("; |
||
373 | $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null') . ","; |
||
374 | $sql .= " '" . $this->db->escape($this->model_name) . "',"; |
||
375 | $sql .= " '" . $this->db->escape($this->datatoimport) . "',"; |
||
376 | $sql .= " '" . $this->db->escape($this->hexa) . "'"; |
||
377 | $sql .= ")"; |
||
378 | |||
379 | $resql = $this->db->query($sql); |
||
380 | if ($resql) { |
||
381 | $this->db->commit(); |
||
382 | return 1; |
||
383 | } else { |
||
384 | $this->error = $this->db->lasterror(); |
||
385 | $this->errno = $this->db->lasterrno(); |
||
386 | $this->db->rollback(); |
||
387 | return -1; |
||
388 | } |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Load an import profil from database |
||
393 | * |
||
394 | * @param int $id Id of profil to load |
||
395 | * @return int Return integer <0 if KO, >0 if OK |
||
396 | */ |
||
397 | public function fetch($id) |
||
398 | { |
||
399 | $sql = 'SELECT em.rowid, em.field, em.label, em.type'; |
||
400 | $sql .= ' FROM ' . MAIN_DB_PREFIX . 'import_model as em'; |
||
401 | $sql .= ' WHERE em.rowid = ' . ((int) $id); |
||
402 | |||
403 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
404 | $result = $this->db->query($sql); |
||
405 | if ($result) { |
||
406 | $obj = $this->db->fetch_object($result); |
||
407 | if ($obj) { |
||
408 | $this->id = $obj->rowid; |
||
409 | $this->hexa = $obj->field; |
||
410 | $this->model_name = $obj->label; |
||
411 | $this->datatoimport = $obj->type; |
||
412 | $this->fk_user = $obj->fk_user; |
||
413 | return 1; |
||
414 | } else { |
||
415 | $this->error = "Model not found"; |
||
416 | return -2; |
||
417 | } |
||
418 | } else { |
||
419 | dol_print_error($this->db); |
||
420 | return -3; |
||
421 | } |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Delete object in database |
||
426 | * |
||
427 | * @param User $user User that delete |
||
428 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
429 | * @return int Return integer <0 if KO, >0 if OK |
||
430 | */ |
||
431 | public function delete($user, $notrigger = 0) |
||
469 | } |
||
470 | } |
||
471 | } |
||
472 |