Total Complexity | 69 |
Total Lines | 542 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 0 |
Complex classes like ImportStudents 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 ImportStudents, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ImportStudents extends Command |
||
27 | { |
||
28 | use Importable; |
||
29 | /** |
||
30 | * The name and signature of the console command. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $signature = 'import:students {node}'; |
||
35 | |||
36 | /** |
||
37 | * The console command description. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $description = 'Bulk students data upload'; |
||
42 | |||
43 | /** |
||
44 | * Create a new command instance. |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public function __construct() |
||
49 | { |
||
50 | $this->output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
51 | parent::__construct(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Execute the console command. |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function handle() |
||
60 | { |
||
61 | $files = $this->getFiles(); |
||
62 | // if(empty($files)){ |
||
63 | // $files = $this->getTerminatedFiles(); |
||
64 | // } |
||
65 | while ($this->checkTime()) { |
||
66 | if ($this->checkTime()) { |
||
67 | try { |
||
68 | if (!empty($files)) { |
||
69 | $this->process($files); |
||
70 | unset($files); |
||
71 | exit(); |
||
|
|||
72 | } else { |
||
73 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
74 | $this->output->writeln('No files found,Waiting for files'); |
||
75 | exit(); |
||
76 | } |
||
77 | } catch (Exception $e) { |
||
78 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
79 | $this->output->writeln($e); |
||
80 | sleep(300); |
||
81 | $this->handle(); |
||
82 | } |
||
83 | } else { |
||
84 | exit(); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | |||
90 | protected function process($files) |
||
91 | { |
||
92 | $time = Carbon::now()->tz('Asia/Colombo'); |
||
93 | $node = $this->argument('node'); |
||
94 | $files[0]['node'] = $node; |
||
95 | $this->processSheet($files[0]); |
||
96 | $now = Carbon::now()->tz('Asia/Colombo'); |
||
97 | $this->output->writeln('=============== Time taken to batch ' . $now->diffInMinutes($time)); |
||
98 | } |
||
99 | |||
100 | protected function getTerminatedFiles() |
||
101 | { |
||
102 | $files = Upload::where('is_processed', '=', 3) |
||
103 | ->where('updated_at', '<=', Carbon::now()->tz('Asia/Colombo')->subHours(3)) |
||
104 | ->limit(1) |
||
105 | ->orderBy('updated_at', 'desc') |
||
106 | ->get()->toArray(); |
||
107 | if (!empty($files)) { |
||
108 | $this->output->writeln('******************* Processing a terminated file **********************'); |
||
109 | DB::beginTransaction(); |
||
110 | DB::table('uploads') |
||
111 | ->where('id', $files[0]['id']) |
||
112 | ->update(['is_processed' => 4, 'updated_at' => now()]); |
||
113 | DB::commit(); |
||
114 | } |
||
115 | return $files; |
||
116 | } |
||
117 | |||
118 | protected function getFiles() |
||
119 | { |
||
120 | $query = Upload::where('is_processed', '=', 0) |
||
121 | ->select( |
||
122 | 'uploads.id', |
||
123 | 'uploads.security_user_id', |
||
124 | 'uploads.institution_class_id', |
||
125 | 'uploads.model', |
||
126 | 'uploads.filename', |
||
127 | 'uploads.is_processed', |
||
128 | 'uploads.deleted_at', |
||
129 | 'uploads.created_at', |
||
130 | 'uploads.updated_at', |
||
131 | 'uploads.is_email_sent', |
||
132 | 'uploads.update', |
||
133 | 'uploads.insert', |
||
134 | 'uploads.node' |
||
135 | ) |
||
136 | ->join('user_contacts', 'uploads.security_user_id', '=', 'user_contacts.security_user_id') |
||
137 | ->join('contact_types', 'user_contacts.contact_type_id', '=', 'contact_types.id') |
||
138 | |||
139 | ; |
||
140 | if(env('APP_ENV') == 'stage'){ |
||
141 | $query->where('contact_types.contact_option_id', '=', 5) |
||
142 | ->where('contact_types.name', '=', 'TestEmail'); |
||
143 | }else{ |
||
144 | $query->where('contact_types.contact_option_id', '!=', 5); |
||
145 | } |
||
146 | |||
147 | $files = $query->limit(1)->get()->toArray(); |
||
148 | $node = $this->argument('node'); |
||
149 | if (!empty($files)) { |
||
150 | DB::beginTransaction(); |
||
151 | DB::table('uploads') |
||
152 | ->where('id', $files[0]['id']) |
||
153 | ->update(['is_processed' => 3, 'updated_at' => now(), 'node' => $node]); |
||
154 | DB::commit(); |
||
155 | } |
||
156 | return $files; |
||
157 | } |
||
158 | |||
159 | protected function checkTime() |
||
160 | { |
||
161 | $time = Carbon::now()->tz('Asia/Colombo'); |
||
162 | $morning = Carbon::create($time->year, $time->month, $time->day, env('CRON_START_TIME', 0), 29, 0)->tz('Asia/Colombo')->setHour(0); //set time to 05:59 |
||
163 | |||
164 | $evening = Carbon::create($time->year, $time->month, $time->day, env('CRON_END_TIME', 0), 30, 0)->tz('Asia/Colombo')->setHour(23); //set time to 18:00 |
||
165 | |||
166 | $check = $time->between($morning, $evening, true); |
||
167 | return true; |
||
168 | } |
||
169 | |||
170 | public function processSuccessEmail($file, $user, $subject) |
||
171 | { |
||
172 | $file['subject'] = $subject; |
||
173 | $this->output->writeln('Processing the file: ' . $file['filename']); |
||
174 | try { |
||
175 | Mail::to($user->email)->send(new StudentImportSuccess($file)); |
||
176 | DB::table('uploads') |
||
177 | ->where('id', $file['id']) |
||
178 | ->update(['is_processed' => 1, 'is_email_sent' => 1, 'updated_at' => now()]); |
||
179 | } catch (\Exception $ex) { |
||
180 | $this->output->writeln($ex->getMessage()); |
||
181 | DB::table('uploads') |
||
182 | ->where('id', $file['id']) |
||
183 | ->update(['is_processed' => 1, 'is_email_sent' => 2, 'updated_at' => now()]); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | public function processFailedEmail($file, $user, $subject) |
||
188 | { |
||
189 | $file['subject'] = $subject; |
||
190 | try { |
||
191 | Mail::to($user->email)->send(new StudentImportFailure($file)); |
||
192 | DB::table('uploads') |
||
193 | ->where('id', $file['id']) |
||
194 | ->update(['is_processed' => 2, 'is_email_sent' => 1, 'updated_at' => now()]); |
||
195 | } catch (\Exception $ex) { |
||
196 | $this->output->writeln($ex->getMessage()); |
||
197 | DB::table('uploads') |
||
198 | ->where('id', $file['id']) |
||
199 | ->update(['is_processed' => 2, 'is_email_sent' => 2, 'updated_at' => now()]); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | public function processEmptyEmail($file, $user, $subject) |
||
204 | { |
||
205 | $file['subject'] = $subject; |
||
206 | try { |
||
207 | Mail::to($user->email)->send(new EmptyFile($file)); |
||
208 | DB::table('uploads') |
||
209 | ->where('id', $file['id']) |
||
210 | ->update(['is_processed' => 2, 'is_email_sent' => 1, 'updated_at' => now()]); |
||
211 | } catch (\Exception $ex) { |
||
212 | $this->output->writeln($ex->getMessage()); |
||
213 | DB::table('uploads') |
||
214 | ->where('id', $file['id']) |
||
215 | ->update(['is_processed' => 2, 'is_email_sent' => 2, 'updated_at' => now()]); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | protected function checkNode($file) |
||
220 | { |
||
221 | $node = $this->argument('node'); |
||
222 | if ($node == $file['node']) { |
||
223 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
224 | $this->output->writeln('Processing from:' . $node); |
||
225 | return true; |
||
226 | } else { |
||
227 | exit; |
||
228 | return false; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | protected function processSheet($file) |
||
233 | { |
||
234 | $this->startTime = Carbon::now()->tz('Asia/Colombo'); |
||
235 | $user = User::find($file['security_user_id']); |
||
236 | $this->checkNode($file); |
||
237 | $this->output->writeln('##########################################################################################################################'); |
||
238 | $this->output->writeln('Processing the file: ' . $file['filename']); |
||
239 | if ($this->checkTime()) { |
||
240 | try { |
||
241 | $this->import($file, 1, 'C'); |
||
242 | sleep(10); |
||
243 | $this->import($file, 2, 'B'); |
||
244 | } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { |
||
245 | $this->output->writeln($e->getMessage()); |
||
246 | try { |
||
247 | Mail::to($user->email)->send(new IncorrectTemplate($file)); |
||
248 | DB::table('uploads') |
||
249 | ->where('id', $file['id']) |
||
250 | ->update(['is_processed' => 2, 'is_email_sent' => 1, 'updated_at' => now()]); |
||
251 | } catch (\Exception $ex) { |
||
252 | $this->output->writeln($e->getMessage()); |
||
253 | $this->handle(); |
||
254 | DB::table('uploads') |
||
255 | ->where('id', $file['id']) |
||
256 | ->update(['is_processed' => 2, 'is_email_sent' => 2, 'updated_at' => now()]); |
||
257 | } |
||
258 | } |
||
259 | } else { |
||
260 | exit(); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | protected function getType($file) |
||
265 | { |
||
266 | $file = storage_path() . '/app/sis-bulk-data-files/' . $file; |
||
267 | $inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file); |
||
268 | return $inputFileType; |
||
269 | } |
||
270 | |||
271 | |||
272 | protected function getSheetWriter($file, $reader) |
||
273 | { |
||
274 | switch ($this->getType($file['filename'])) { |
||
275 | case 'Xlsx': |
||
276 | return new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($reader); |
||
277 | break; |
||
278 | case 'Ods': |
||
279 | return new \PhpOffice\PhpSpreadsheet\Writer\Ods($reader); |
||
280 | break; |
||
281 | default: |
||
282 | return new \PhpOffice\PhpSpreadsheet\Writer\Xls($reader); |
||
283 | break; |
||
284 | } |
||
285 | } |
||
286 | |||
287 | protected function getSheetType($file) |
||
288 | { |
||
289 | switch ($this->getType($file)) { |
||
290 | case 'Xlsx': |
||
291 | return \Maatwebsite\Excel\Excel::XLSX; |
||
292 | break; |
||
293 | case 'Ods': |
||
294 | return \Maatwebsite\Excel\Excel::ODS; |
||
295 | break; |
||
296 | case 'Xml': |
||
297 | return \Maatwebsite\Excel\Excel::XML; |
||
298 | break; |
||
299 | default: |
||
300 | return \Maatwebsite\Excel\Excel::XLS; |
||
301 | break; |
||
302 | } |
||
303 | } |
||
304 | |||
305 | |||
306 | protected function getSheetCount($file) |
||
307 | { |
||
308 | $objPHPExcel = $this->setReader($file); |
||
309 | return $objPHPExcel->getSheetCount(); |
||
310 | } |
||
311 | |||
312 | |||
313 | |||
314 | /** |
||
315 | * @param $file |
||
316 | * @param $sheet |
||
317 | * @param $column |
||
318 | */ |
||
319 | protected function import($file, $sheet, $column) |
||
320 | { |
||
321 | try { |
||
322 | ini_set('memory_limit', '2048M'); |
||
323 | $this->getFileSize($file); |
||
324 | $user = User::find($file['security_user_id']); |
||
325 | $excelFile = '/sis-bulk-data-files/' . $file['filename']; |
||
326 | $this->highestRow = $this->getHighestRow($file, $sheet, $column); |
||
327 | switch ($sheet) { |
||
328 | case 1; |
||
329 | $this->output->writeln('Trying to insert Students'); |
||
330 | if (($this->getSheetName($file, 'Insert Students')) && $this->highestRow > 0) { // |
||
331 | $import = new UsersImport($file); |
||
332 | $import->import($excelFile, 'local', $this->getSheetType($file['filename'])); |
||
333 | // Excel::import($import, $excelFile, 'local'); |
||
334 | DB::table('uploads') |
||
335 | ->where('id', $file['id']) |
||
336 | ->update(['insert' => 1, 'is_processed' => 1, 'updated_at' => now()]); |
||
337 | if ($import->failures()->count() > 0) { |
||
338 | self::writeErrors($import, $file, 'Insert Students'); |
||
339 | DB::table('uploads') |
||
340 | ->where('id', $file['id']) |
||
341 | ->update(['insert' => 3, 'updated_at' => now()]); |
||
342 | $this->processFailedEmail($file, $user, 'Fresh Student Data Upload:Partial Success '); |
||
343 | $this->stdOut('Insert Students', $this->highestRow); |
||
344 | } else { |
||
345 | DB::table('uploads') |
||
346 | ->where('id', $file['id']) |
||
347 | ->update(['insert' => 1, 'updated_at' => now()]); |
||
348 | $this->processSuccessEmail($file, $user, 'Fresh Student Data Upload:Success '); |
||
349 | $this->stdOut('Insert Students', $this->highestRow); |
||
350 | } |
||
351 | } else if (($this->getSheetName($file, 'Insert Students')) && $this->highestRow > 0) { |
||
352 | DB::table('uploads') |
||
353 | ->where('id', $file['id']) |
||
354 | ->update(['is_processed' => 2]); |
||
355 | $this->processEmptyEmail($file, $user, 'Fresh Student Data Upload '); |
||
356 | } |
||
357 | break; |
||
358 | case 2; |
||
359 | $this->output->writeln('Trying to update Students'); |
||
360 | if (($this->getSheetName($file, 'Update Students')) && $this->highestRow > 0) { |
||
361 | $import = new StudentUpdate($file); |
||
362 | $import->import($excelFile, 'local', $this->getSheetType($file['filename'])); |
||
363 | if ($import->failures()->count() > 0) { |
||
364 | self::writeErrors($import, $file, 'Update Students'); |
||
365 | DB::table('uploads') |
||
366 | ->where('id', $file['id']) |
||
367 | ->update(['update' => 3, 'is_processed' => 1, 'updated_at' => now()]); |
||
368 | $this->processFailedEmail($file, $user, 'Existing Student Data Update:Partial Success '); |
||
369 | $this->stdOut('Update Students', $this->highestRow); |
||
370 | } else { |
||
371 | DB::table('uploads') |
||
372 | ->where('id', $file['id']) |
||
373 | ->update(['update' => 1, 'is_processed' => 1, 'updated_at' => now()]); |
||
374 | $this->processSuccessEmail($file, $user, 'Existing Student Data Update:Success '); |
||
375 | $this->stdOut('Update Students', $this->highestRow); |
||
376 | } |
||
377 | } else if (($this->getSheetName($file, 'Update Students')) && $this->highestRow == 0) { |
||
378 | DB::table('uploads') |
||
379 | ->where('id', $file['id']) |
||
380 | ->update(['is_processed' => 2, 'updated_at' => now()]); |
||
381 | $this->processEmptyEmail($file, $user, 'Existing Student Data Update'); |
||
382 | } |
||
383 | break; |
||
384 | } |
||
385 | } catch (\Maatwebsite\Excel\Validators\ValidationException $e) { |
||
386 | $this->output->writeln($e->getMessage()); |
||
387 | if ($sheet == 1) { |
||
388 | self::writeErrors($e, $file, 'Insert Students'); |
||
389 | DB::table('uploads') |
||
390 | ->where('id', $file['id']) |
||
391 | ->update(['insert' => 2, 'updated_at' => now()]); |
||
392 | $this->processFailedEmail($file, $user, 'Fresh Student Data Upload:Failed'); |
||
393 | } else if ($sheet == 2) { |
||
394 | self::writeErrors($e, $file, 'Update Students'); |
||
395 | DB::table('uploads') |
||
396 | ->where('id', $file['id']) |
||
397 | ->update(['update' => 2, 'updated_at' => now()]); |
||
398 | $this->processFailedEmail($file, $user, 'Existing Student Data Update:Failed'); |
||
399 | } |
||
400 | DB::table('uploads') |
||
401 | ->where('id', $file['id']) |
||
402 | ->update(['is_processed' => 2, 'updated_at' => now()]); |
||
403 | } |
||
404 | } |
||
405 | |||
406 | protected function processErrors($failure) |
||
407 | { |
||
408 | $error_mesg = implode(',', $failure->errors()); |
||
409 | $failure = [ |
||
410 | 'row' => $failure->row(), |
||
411 | 'errors' => [$error_mesg], |
||
412 | 'attribute' => $failure->attribute() |
||
413 | ]; |
||
414 | return $failure; |
||
415 | } |
||
416 | |||
417 | protected function getFileSize($file) |
||
418 | { |
||
419 | $excelFile = '/sis-bulk-data-files/' . $file['filename']; |
||
420 | $size = Storage::disk('local')->size($excelFile); |
||
421 | $user = User::find($file['security_user_id']); |
||
422 | if ($size > 0) { |
||
423 | return true; |
||
424 | } else { |
||
425 | DB::table('uploads') |
||
426 | ->where('id', $file['id']) |
||
427 | ->update(['is_processed' => 2, 'updated_at' => now()]); |
||
428 | $this->stdOut('No valid data found :Please re-upload the file', 0); |
||
429 | $this->processEmptyEmail($file, $user, 'No valid data found :Please re-upload the file'); |
||
430 | } |
||
431 | } |
||
432 | |||
433 | protected function setReader($file) |
||
434 | { |
||
435 | try { |
||
436 | $excelFile = '/sis-bulk-data-files/processed/' . $file['filename']; |
||
437 | $exists = Storage::disk('local')->exists($excelFile); |
||
438 | if (!$exists) { |
||
439 | |||
440 | $excelFile = "/sis-bulk-data-files/" . $file['filename']; |
||
441 | } |
||
442 | $excelFile = storage_path() . "/app" . $excelFile; |
||
443 | $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($this->getType($file['filename'])); |
||
444 | $objPHPExcel = $reader->load($excelFile); |
||
445 | return $objPHPExcel; |
||
446 | } catch (Exception $e) { |
||
447 | $this->output->writeln($e->getMessage()); |
||
448 | $user = User::find($file['security_user_id']); |
||
449 | DB::table('uploads') |
||
450 | ->where('id', $file['id']) |
||
451 | ->update(['is_processed' => 2, 'updated_at' => now()]); |
||
452 | $this->stdOut('No valid data found :Please re-upload the file', 0); |
||
453 | $this->processEmptyEmail($file, $user, 'No valid data found :Please re-upload the file'); |
||
454 | } |
||
455 | } |
||
456 | |||
457 | protected function getSheetName($file, $sheet) |
||
458 | { |
||
459 | try {; |
||
460 | $objPHPExcel = $this->setReader($file); |
||
461 | return $objPHPExcel->getSheetByName($sheet) !== null; |
||
462 | } catch (Exception $e) { |
||
463 | $this->output->writeln($e->getMessage()); |
||
464 | $user = User::find($file['security_user_id']); |
||
465 | DB::table('uploads') |
||
466 | ->where('id', $file['id']) |
||
467 | ->update(['is_processed' => 2, 'updated_at' => now()]); |
||
468 | $this->stdOut('No valid data found :Please re-upload the file', 0); |
||
469 | $this->processEmptyEmail($file, $user, 'No valid data found :Please re-upload the file'); |
||
470 | } |
||
471 | } |
||
472 | |||
473 | protected function getHighestRow($file, $sheet, $column) |
||
474 | { |
||
475 | try { |
||
476 | $reader = $this->setReader($file); |
||
477 | $highestRowCount = 0; |
||
478 | $sheet = $reader->getSheet($sheet); |
||
479 | $highestRow = $sheet->getHighestDataRow($column); |
||
480 | for ($row = 3; $row <= $highestRow; $row++){ |
||
481 | $rowData = $sheet->rangeToArray('A' . $row . ':' . $column . $row,NULL,TRUE,FALSE); |
||
482 | if(isEmptyRow(reset($rowData))) { continue; } // skip empty row |
||
483 | $highestRowCount += 1; |
||
484 | } |
||
485 | return $highestRowCount; |
||
486 | } catch (\Exception $e) { |
||
487 | $this->output->writeln($e->getMessage()); |
||
488 | $user = User::find($file['security_user_id']); |
||
489 | DB::beginTransaction(); |
||
490 | DB::table('uploads') |
||
491 | ->where('id', $file['id']) |
||
492 | ->update(['is_processed' => 2, 'updated_at' => now()]); |
||
493 | DB::commit(); |
||
494 | $this->processEmptyEmail($file, $user, 'No valid data found'); |
||
495 | exit(); |
||
496 | } |
||
497 | } |
||
498 | |||
499 | protected function stdOut($title, $rows) |
||
500 | { |
||
501 | $this->output->writeln($title . ' Process completed at . ' . ' ' . now()); |
||
502 | $now = Carbon::now()->tz('Asia/Colombo'); |
||
503 | $this->output->writeln('Total Processed lines: ' . $rows); |
||
504 | $this->output->writeln('Time taken to process : ' . $now->diffInSeconds($this->startTime) . ' Seconds'); |
||
505 | $this->output->writeln('--------------------------------------------------------------------------------------------------------------------------'); |
||
506 | } |
||
507 | |||
508 | |||
509 | |||
510 | protected function removeRows($row, $count, $params) |
||
518 | } |
||
519 | } |
||
520 | |||
521 | |||
522 | protected function writeErrors($e, $file, $sheet) |
||
523 | { |
||
524 | try { |
||
525 | ini_set('memory_limit', '2048M'); |
||
526 | $baseMemory = memory_get_usage(); |
||
527 | gc_enable(); |
||
528 | gc_collect_cycles(); |
||
529 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
530 | ini_set('memory_limit', -1); |
||
531 | $failures = $e->failures(); |
||
532 | $reader = $this->setReader($file); |
||
533 | $reader->setActiveSheetIndexByName($sheet); |
||
534 | |||
535 | $failures = gettype($failures) == 'object' ? array_map(array($this, 'processErrors'), iterator_to_array($failures)) : array_map(array($this, 'processErrors'), ($failures)); |
||
536 | if (count($failures) > 0) { |
||
537 | $rows = array_map('rows', $failures); |
||
538 | $rows = array_unique($rows); |
||
539 | $rowIndex = range(3, $this->highestRow + 2); |
||
568 | } |
||
569 | } |
||
571 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.