Conditions | 44 |
Paths | 103 |
Total Lines | 229 |
Lines | 12 |
Ratio | 5.24 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
388 | public function fetchFromZohoInBulk(AbstractZohoDao $dao) |
||
389 | { |
||
390 | /* |
||
391 | * This method is really dirty, and do not use the php sdk because late development for the zoho v1 EOL in december. |
||
392 | * Should be re-written to make it clean. |
||
393 | */ |
||
394 | // Doc: https://www.zoho.com/crm/developer/docs/api/bulk-read/create-job.html |
||
395 | |||
396 | $tableName = ZohoDatabaseHelper::getTableName($dao, $this->prefix); |
||
397 | $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
||
398 | $apiModuleName = $dao->getPluralModuleName(); |
||
399 | |||
400 | $this->logger->notice('Starting bulk fetch for module ' . $apiModuleName . '...'); |
||
401 | |||
402 | $zohoClient = new ZohoClient([ |
||
403 | 'client_id' => ZOHO_CRM_CLIENT_ID, |
||
404 | 'client_secret' => ZOHO_CRM_CLIENT_SECRET, |
||
405 | 'redirect_uri' => ZOHO_CRM_CLIENT_REDIRECT_URI, |
||
406 | 'currentUserEmail' => ZOHO_CRM_CLIENT_CURRENT_USER_EMAIL, |
||
407 | 'applicationLogFilePath' => ZOHO_CRM_CLIENT_APPLICATION_LOGFILEPATH, |
||
408 | 'persistence_handler_class' => ZOHO_CRM_CLIENT_PERSISTENCE_HANDLER_CLASS, |
||
409 | 'token_persistence_path' => ZOHO_CRM_CLIENT_PERSITENCE_PATH, |
||
410 | 'sandbox' => ZOHO_CRM_SANDBOX |
||
411 | ], 'Europe/Paris'); |
||
412 | |||
413 | $client = new \GuzzleHttp\Client(); |
||
414 | $page = 1; |
||
415 | while (true) { |
||
416 | $oauthToken = $zohoClient->getZohoOAuthClient()->getAccessToken(ZOHO_CRM_CLIENT_CURRENT_USER_EMAIL); |
||
417 | |||
418 | // Step 1: Create a bulk read job |
||
419 | $this->logger->info('Creating read job for module ' . $apiModuleName . ' and page ' . $page . '...'); |
||
420 | $response = $client->request('POST', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read', [ |
||
421 | 'http_errors' => false, |
||
422 | 'headers' => [ |
||
423 | 'Authorization' => 'Zoho-oauthtoken ' . $oauthToken |
||
424 | ], |
||
425 | 'json' => [ |
||
426 | 'query' => [ |
||
427 | 'module' => $apiModuleName, |
||
428 | 'page' => $page |
||
429 | ] |
||
430 | ] |
||
431 | ]); |
||
432 | $jobId = null; |
||
433 | if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
||
434 | $resultStr = $response->getBody()->getContents(); |
||
435 | $json = json_decode($resultStr, true); |
||
436 | |||
437 | $jobId = $json['data'][0]['details']['id']; |
||
438 | |||
439 | // We don't care about the job status right now, it will be checked later |
||
440 | View Code Duplication | } else { |
|
441 | $this->logger->error('Cannot create bulk read query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents()); |
||
442 | break; |
||
443 | } |
||
444 | |||
445 | if ($jobId === null) { |
||
446 | $this->logger->error('JobID cannot be null. json:' . $resultStr); |
||
447 | break; |
||
448 | } |
||
449 | |||
450 | // Step 2: Check job status |
||
451 | $jobDetails = null; |
||
452 | while (true) { |
||
453 | $this->logger->info('Checking job ' . $jobId . ' status for module ' . $apiModuleName . ' and page ' . $page . '...'); |
||
454 | $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId, [ |
||
455 | 'http_errors' => false, |
||
456 | 'headers' => [ |
||
457 | 'Authorization' => 'Zoho-oauthtoken ' . $oauthToken |
||
458 | ] |
||
459 | ]); |
||
460 | if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
||
461 | $resultStr = $response->getBody()->getContents(); |
||
462 | $json = json_decode($resultStr, true); |
||
463 | |||
464 | if (isset($json['data'][0]['state'])) { |
||
465 | $status = $json['data'][0]['state']; |
||
466 | if ($status === 'ADDED' || $status === 'QUEUED') { |
||
467 | $this->logger->info('Job still waiting for process'); |
||
468 | } else if ($status === 'IN PROGRESS') { |
||
469 | $this->logger->info('Job in progress'); |
||
470 | } else if ($status === 'COMPLETED') { |
||
471 | $this->logger->info('Job completed'); |
||
472 | $jobDetails = $json; |
||
473 | break; |
||
474 | } else { |
||
475 | $this->logger->info('Unsupported job status: ' . $resultStr); |
||
476 | break; |
||
477 | } |
||
478 | } else { |
||
479 | $this->logger->error('Unsupported response: ' . $resultStr); |
||
480 | break; |
||
481 | } |
||
482 | View Code Duplication | } else { |
|
483 | $this->logger->error('Cannot get bulk job status query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents()); |
||
484 | break; |
||
485 | } |
||
486 | sleep(15); |
||
487 | } |
||
488 | |||
489 | // Step 3: Download the result |
||
490 | if ($jobDetails === null) { |
||
491 | $this->logger->error('JobDetails cannot be empty. json:' . $resultStr); |
||
492 | break; |
||
493 | } |
||
494 | $this->logger->debug(json_encode($jobDetails)); |
||
495 | $this->logger->info('Downloading zip file for module ' . $apiModuleName . ' and page ' . $page . '...'); |
||
496 | $jobZipFile = '/tmp/job_' . $dao->getZCRMModule()->getAPIName() . '_' . $jobDetails['data'][0]['id'] . '.zip'; |
||
497 | $jobCsvPath = '/tmp/job_extract'; |
||
498 | $jobCsvFile = '/tmp/job_extract/' . $jobDetails['data'][0]['id'] . '.csv'; |
||
499 | $canProcessCsv = false; |
||
500 | |||
501 | $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId . '/result', [ |
||
502 | 'http_errors' => false, |
||
503 | 'headers' => [ |
||
504 | 'Authorization' => 'Zoho-oauthtoken ' . $oauthToken |
||
505 | ], |
||
506 | 'sink' => $jobZipFile |
||
507 | ]); |
||
508 | if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
||
509 | $this->logger->info('Extracting ' . $jobZipFile . ' file for module ' . $apiModuleName . ' and page ' . $page . '...'); |
||
510 | $zip = new ZipArchive(); |
||
511 | $res = $zip->open($jobZipFile); |
||
512 | if ($res === TRUE) { |
||
513 | $zip->extractTo($jobCsvPath); |
||
514 | $zip->close(); |
||
515 | $this->logger->info('File extracted in ' . $jobCsvFile); |
||
516 | $canProcessCsv = true; |
||
517 | } else { |
||
518 | switch ($res) { |
||
519 | case ZipArchive::ER_EXISTS: |
||
520 | $zipErrorMessage = 'File already exists.'; |
||
521 | break; |
||
522 | case ZipArchive::ER_INCONS: |
||
523 | $zipErrorMessage = 'Zip archive inconsistent.'; |
||
524 | break; |
||
525 | case ZipArchive::ER_MEMORY: |
||
526 | $zipErrorMessage = 'Malloc failure.'; |
||
527 | break; |
||
528 | case ZipArchive::ER_NOENT: |
||
529 | $zipErrorMessage = 'No such file.'; |
||
530 | break; |
||
531 | case ZipArchive::ER_NOZIP: |
||
532 | $zipErrorMessage = 'Not a zip archive.'; |
||
533 | break; |
||
534 | case ZipArchive::ER_OPEN: |
||
535 | $zipErrorMessage = "Can't open file."; |
||
536 | break; |
||
537 | case ZipArchive::ER_READ: |
||
538 | $zipErrorMessage = 'Read error.'; |
||
539 | break; |
||
540 | case ZipArchive::ER_SEEK: |
||
541 | $zipErrorMessage = 'Seek error.'; |
||
542 | break; |
||
543 | default: |
||
544 | $zipErrorMessage = "Unknow (Code $res)"; |
||
545 | break; |
||
546 | } |
||
547 | $this->logger->error('Error when extracting zip file: ' . $zipErrorMessage); |
||
548 | break; |
||
549 | } |
||
550 | View Code Duplication | } else { |
|
551 | $this->logger->error('Cannot download results for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents()); |
||
552 | break; |
||
553 | } |
||
554 | |||
555 | // Step 4: Save data |
||
556 | if (!$canProcessCsv) { |
||
557 | $this->logger->error('Cannot process CSV'); |
||
558 | break; |
||
559 | } |
||
560 | |||
561 | $this->logger->info('Building list of users...'); |
||
562 | $usersQuery = $this->connection->executeQuery('SELECT id, full_name FROM users'); |
||
563 | $usersResults = $usersQuery->fetchAll(); |
||
564 | $users = []; |
||
565 | foreach ($usersResults as $user) { |
||
566 | $users[$user['id']] = $user['full_name']; |
||
567 | } |
||
568 | |||
569 | $this->logger->info('Saving records to db...'); |
||
570 | $nbRecords = $jobDetails['data'][0]['result']['count']; |
||
571 | $whenToLog = ceil($nbRecords / 100); |
||
572 | $this->logger->info($nbRecords . ' records to save'); |
||
573 | $nbSaved = 0; |
||
574 | $handle = fopen($jobCsvFile, 'r'); |
||
575 | $fields = []; |
||
576 | if ($handle) { |
||
577 | while (($row = fgetcsv($handle)) !== false) { |
||
578 | if (empty($fields)) { |
||
579 | $fields = $row; |
||
580 | continue; |
||
581 | } |
||
582 | $recordDataToInsert = []; |
||
583 | foreach ($row as $k => $value) { |
||
584 | $columnName = $fields[$k]; |
||
585 | $decodedColumnName = str_replace('_', '', $columnName); |
||
586 | if ($table->hasColumn($decodedColumnName)) { |
||
587 | $recordDataToInsert[$decodedColumnName] = $value === '' ? null : $value; |
||
588 | } else { |
||
589 | if ($columnName === 'Owner' || $columnName === 'Created_By' || $columnName === 'Modified_By') { |
||
590 | $recordDataToInsert[$decodedColumnName . '_OwnerID'] = $value === '' ? null : $value; |
||
591 | $recordDataToInsert[$decodedColumnName . '_OwnerName'] = $users[$value] ?? null; |
||
592 | } else if ($table->hasColumn($decodedColumnName . '_ID')) { |
||
593 | $recordDataToInsert[$decodedColumnName . '_ID'] = $value === '' ? null : $value; |
||
594 | } |
||
595 | } |
||
596 | } |
||
597 | $this->connection->insert($tableName, $recordDataToInsert); |
||
598 | ++$nbSaved; |
||
599 | if (($nbSaved % $whenToLog) === 0) { |
||
600 | $this->logger->info($nbSaved . '/' . $nbRecords . ' records processed'); |
||
601 | } |
||
602 | } |
||
603 | $this->logger->info($nbSaved . ' records saved for module ' . $apiModuleName . ' and page ' . $page); |
||
604 | fclose($handle); |
||
605 | } |
||
606 | |||
607 | // Step 5: Check if there is more results |
||
608 | $hasMoreRecords = $jobDetails['data'][0]['result']['more_records']; |
||
609 | if (!$hasMoreRecords) { |
||
610 | $this->logger->info('No more records for the module ' . $apiModuleName); |
||
611 | break; |
||
612 | } |
||
613 | $this->logger->info('More records to fetch for the module ' . $apiModuleName); |
||
614 | ++$page; |
||
615 | } |
||
616 | } |
||
617 | |||
640 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.