Completed
Push — 3.2.3 ( 6612c6...3b762a )
by
unknown
02:23 queued 21s
created
src/ZohoDatabaseCopier.php 1 patch
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -72,11 +72,11 @@  discard block
 block discarded – undo
72 72
     {
73 73
         $users = $this->zohoUserService->getUsers();
74 74
         $tableName = 'users';
75
-        $this->logger->info('Fetched ' . count($users) . ' records for table ' . $tableName);
75
+        $this->logger->info('Fetched '.count($users).' records for table '.$tableName);
76 76
 
77 77
         $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
78 78
 
79
-        $select = $this->connection->prepare('SELECT * FROM ' . $tableName . ' WHERE id = :id');
79
+        $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id');
80 80
 
81 81
         $this->connection->beginTransaction();
82 82
         foreach ($users as $user) {
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
                                     $lastPageInConfig = '1';
198 198
                                 }
199 199
                                 $lastActivityTime = new \DateTime($lastDateInConfig, new \DateTimeZone($dao->getZohoClient()->getTimezone()));
200
-                                $recordsPage = (int)$lastPageInConfig;
200
+                                $recordsPage = (int) $lastPageInConfig;
201 201
                             } else {
202 202
                                 $findDateByModifiedTime = true;
203 203
                             }
@@ -210,10 +210,10 @@  discard block
 block discarded – undo
210 210
                                 $lastActivityTime = new \DateTime($modifiedSince);
211 211
                             } else {
212 212
                                 if ($tableHasColumnModifiedTime) {
213
-                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
213
+                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
214 214
                                 }
215 215
                                 if (!$lastActivityTime && $tableHasColumnCreatedTime) {
216
-                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
216
+                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
217 217
                                 }
218 218
 
219 219
                                 if ($lastActivityTime !== null) {
@@ -253,7 +253,7 @@  discard block
 block discarded – undo
253 253
                         if ($zohoSyncConfigTableExists) {
254 254
                             $lastDate = $lastActivityTime ? $lastActivityTime->format('Y-m-d H:i:s') : date('Y-m-d H:i:s', 0);
255 255
                             $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__DATE', $tableName, $lastDate);
256
-                            $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__PAGE', $tableName, (string)$recordsPage);
256
+                            $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__PAGE', $tableName, (string) $recordsPage);
257 257
                         }
258 258
 
259 259
                         $recordsPaginationLastTime = $lastActivityTime;
@@ -262,10 +262,10 @@  discard block
 block discarded – undo
262 262
                         if ($zohoSyncConfigTableExists) {
263 263
                             $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
264 264
                             if ($tableHasColumnModifiedTime) {
265
-                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
265
+                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
266 266
                             }
267 267
                             if (!$latestDateToSave && $tableHasColumnCreatedTime) {
268
-                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
268
+                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
269 269
                             }
270 270
                             if (!$latestDateToSave) {
271 271
                                 $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
@@ -275,26 +275,26 @@  discard block
 block discarded – undo
275 275
                         }
276 276
                     }
277 277
                     $totalRecords = count($records);
278
-                    $this->logger->debug($totalRecords . ' records fetched.');
278
+                    $this->logger->debug($totalRecords.' records fetched.');
279 279
                     $deletedRecords = [];
280 280
                     $totalRecordsDeleted = 0;
281 281
                     if (($recordsPage - 1) === 1) {
282 282
                         $this->logger->notice(sprintf('Fetching the records to delete for module %s...', $dao->getPluralModuleName()));
283 283
                         $deletedRecords = $dao->getDeletedRecordIds($lastActivityTime);
284 284
                         $totalRecordsDeleted = count($deletedRecords);
285
-                        $this->logger->debug($totalRecordsDeleted . ' records fetched.');
285
+                        $this->logger->debug($totalRecordsDeleted.' records fetched.');
286 286
                     }
287 287
                 } else {
288 288
                     $this->logger->info(sprintf('Full copy started for module %s', $dao->getPluralModuleName()));
289 289
                     $this->logger->notice(sprintf('Fetching the records to insert/update for module ...%s', $dao->getPluralModuleName()));
290 290
                     $records = $dao->getRecords();
291 291
                     $totalRecords = count($records);
292
-                    $this->logger->debug($totalRecords . ' records fetched.');
292
+                    $this->logger->debug($totalRecords.' records fetched.');
293 293
                     $deletedRecords = [];
294 294
                     $stopAndhasMoreResults = false;
295 295
                 }
296 296
             } catch (ZCRMException $exception) {
297
-                $this->logger->error('Error when getting records for module ' . $dao->getPluralModuleName() . ': ' . $exception->getMessage(), [
297
+                $this->logger->error('Error when getting records for module '.$dao->getPluralModuleName().': '.$exception->getMessage(), [
298 298
                     'exception' => $exception
299 299
                 ]);
300 300
                 if ($throwErrors) {
@@ -304,7 +304,7 @@  discard block
 block discarded – undo
304 304
             }
305 305
             $this->logger->info(sprintf('Inserting/updating %s records into table %s...', $totalRecords, $tableName));
306 306
 
307
-            $select = $this->connection->prepare('SELECT * FROM ' . $tableName . ' WHERE id = :id');
307
+            $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id');
308 308
 
309 309
             $this->connection->beginTransaction();
310 310
 
@@ -376,7 +376,7 @@  discard block
 block discarded – undo
376 376
 
377 377
                     // If there is some columns updated in local_update, we skip them to avoid Zoho values to overide them
378 378
                     if ($localUpdateTableExists && $result['uid']) {
379
-                        $selectRecordInLocalUpdate = $this->connection->prepare('SELECT field_name FROM local_update WHERE table_name = "' . $tableName . '" AND uid = :uid');
379
+                        $selectRecordInLocalUpdate = $this->connection->prepare('SELECT field_name FROM local_update WHERE table_name = "'.$tableName.'" AND uid = :uid');
380 380
                         $selectRecordInLocalUpdate->execute(['uid' => $result['uid']]);
381 381
                         $resultsInLocalUpdate = $selectRecordInLocalUpdate->fetchAll(\PDO::FETCH_ASSOC);
382 382
                         $fieldsUpdated = [];
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
             }
408 408
 
409 409
             $this->logger->info(sprintf('Deleting %d records from table %s...', $totalRecordsDeleted, $tableName));
410
-            $sqlStatementUid = 'select uid from ' . $this->connection->quoteIdentifier($tableName) . ' where id = :id';
410
+            $sqlStatementUid = 'select uid from '.$this->connection->quoteIdentifier($tableName).' where id = :id';
411 411
             $processedRecords = 0;
412 412
             $logOffset = $totalRecordsDeleted >= 500 ? 100 : 50;
413 413
             foreach ($deletedRecords as $deletedRecord) {
@@ -439,10 +439,10 @@  discard block
 block discarded – undo
439 439
                 if ($zohoSyncConfigTableExists) {
440 440
                     $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
441 441
                     if ($tableHasColumnModifiedTime) {
442
-                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
442
+                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
443 443
                     }
444 444
                     if (!$latestDateToSave && $tableHasColumnCreatedTime) {
445
-                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
445
+                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
446 446
                     }
447 447
                     if (!$latestDateToSave) {
448 448
                         $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
@@ -466,7 +466,7 @@  discard block
 block discarded – undo
466 466
         $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
467 467
         $apiModuleName = $dao->getPluralModuleName();
468 468
 
469
-        $this->logger->notice('Starting bulk fetch for module ' . $apiModuleName . '...');
469
+        $this->logger->notice('Starting bulk fetch for module '.$apiModuleName.'...');
470 470
 
471 471
         $zohoClient = new ZohoClient([
472 472
             'client_id' => ZOHO_CRM_CLIENT_ID,
@@ -485,11 +485,11 @@  discard block
 block discarded – undo
485 485
             $oauthToken = $zohoClient->getZohoOAuthClient()->getAccessToken(ZOHO_CRM_CLIENT_CURRENT_USER_EMAIL);
486 486
 
487 487
             // Step 1: Create a bulk read job
488
-            $this->logger->info('Creating read job for module ' . $apiModuleName . ' and page ' . $page . '...');
489
-            $response = $client->request('POST', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read', [
488
+            $this->logger->info('Creating read job for module '.$apiModuleName.' and page '.$page.'...');
489
+            $response = $client->request('POST', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read', [
490 490
                 'http_errors' => false,
491 491
                 'headers' => [
492
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
492
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
493 493
                 ],
494 494
                 'json' => [
495 495
                     'query' => [
@@ -507,23 +507,23 @@  discard block
 block discarded – undo
507 507
 
508 508
                 // We don't care about the job status right now, it will be checked later
509 509
             } else {
510
-                $this->logger->error('Cannot create bulk read query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
510
+                $this->logger->error('Cannot create bulk read query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
511 511
                 break;
512 512
             }
513 513
 
514 514
             if ($jobId === null) {
515
-                $this->logger->error('JobID cannot be null. json:' . $resultStr);
515
+                $this->logger->error('JobID cannot be null. json:'.$resultStr);
516 516
                 break;
517 517
             }
518 518
 
519 519
             // Step 2: Check job status
520 520
             $jobDetails = null;
521 521
             while (true) {
522
-                $this->logger->info('Checking job ' . $jobId . ' status for module ' . $apiModuleName . ' and page ' . $page . '...');
523
-                $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId, [
522
+                $this->logger->info('Checking job '.$jobId.' status for module '.$apiModuleName.' and page '.$page.'...');
523
+                $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId, [
524 524
                     'http_errors' => false,
525 525
                     'headers' => [
526
-                        'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
526
+                        'Authorization' => 'Zoho-oauthtoken '.$oauthToken
527 527
                     ]
528 528
                 ]);
529 529
                 if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
@@ -541,15 +541,15 @@  discard block
 block discarded – undo
541 541
                             $jobDetails = $json;
542 542
                             break;
543 543
                         } else {
544
-                            $this->logger->info('Unsupported job status: ' . $resultStr);
544
+                            $this->logger->info('Unsupported job status: '.$resultStr);
545 545
                             break;
546 546
                         }
547 547
                     } else {
548
-                        $this->logger->error('Unsupported response: ' . $resultStr);
548
+                        $this->logger->error('Unsupported response: '.$resultStr);
549 549
                         break;
550 550
                     }
551 551
                 } else {
552
-                    $this->logger->error('Cannot get bulk job status query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
552
+                    $this->logger->error('Cannot get bulk job status query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
553 553
                     break;
554 554
                 }
555 555
                 sleep(15);
@@ -557,31 +557,31 @@  discard block
 block discarded – undo
557 557
 
558 558
             // Step 3: Download the result
559 559
             if ($jobDetails === null) {
560
-                $this->logger->error('JobDetails cannot be empty. json:' . $resultStr);
560
+                $this->logger->error('JobDetails cannot be empty. json:'.$resultStr);
561 561
                 break;
562 562
             }
563 563
             $this->logger->debug(json_encode($jobDetails));
564
-            $this->logger->info('Downloading zip file for module ' . $apiModuleName . ' and page ' . $page . '...');
565
-            $jobZipFile = '/tmp/job_' . $dao->getZCRMModule()->getAPIName() . '_' . $jobDetails['data'][0]['id'] . '.zip';
564
+            $this->logger->info('Downloading zip file for module '.$apiModuleName.' and page '.$page.'...');
565
+            $jobZipFile = '/tmp/job_'.$dao->getZCRMModule()->getAPIName().'_'.$jobDetails['data'][0]['id'].'.zip';
566 566
             $jobCsvPath = '/tmp/job_extract';
567
-            $jobCsvFile = '/tmp/job_extract/' . $jobDetails['data'][0]['id'] . '.csv';
567
+            $jobCsvFile = '/tmp/job_extract/'.$jobDetails['data'][0]['id'].'.csv';
568 568
             $canProcessCsv = false;
569 569
 
570
-            $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId . '/result', [
570
+            $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId.'/result', [
571 571
                 'http_errors' => false,
572 572
                 'headers' => [
573
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
573
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
574 574
                 ],
575 575
                 'sink' => $jobZipFile
576 576
             ]);
577 577
             if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
578
-                $this->logger->info('Extracting ' . $jobZipFile . ' file for module ' . $apiModuleName . ' and page ' . $page . '...');
578
+                $this->logger->info('Extracting '.$jobZipFile.' file for module '.$apiModuleName.' and page '.$page.'...');
579 579
                 $zip = new ZipArchive();
580 580
                 $res = $zip->open($jobZipFile);
581 581
                 if ($res === TRUE) {
582 582
                     $zip->extractTo($jobCsvPath);
583 583
                     $zip->close();
584
-                    $this->logger->info('File extracted in ' . $jobCsvFile);
584
+                    $this->logger->info('File extracted in '.$jobCsvFile);
585 585
                     $canProcessCsv = true;
586 586
                 } else {
587 587
                     switch ($res) {
@@ -613,11 +613,11 @@  discard block
 block discarded – undo
613 613
                             $zipErrorMessage = "Unknow (Code $res)";
614 614
                             break;
615 615
                     }
616
-                    $this->logger->error('Error when extracting zip file: ' . $zipErrorMessage);
616
+                    $this->logger->error('Error when extracting zip file: '.$zipErrorMessage);
617 617
                     break;
618 618
                 }
619 619
             } else {
620
-                $this->logger->error('Cannot download results for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
620
+                $this->logger->error('Cannot download results for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
621 621
                 break;
622 622
             }
623 623
 
@@ -638,7 +638,7 @@  discard block
 block discarded – undo
638 638
             $this->logger->info('Saving records to db...');
639 639
             $nbRecords = $jobDetails['data'][0]['result']['count'];
640 640
             $whenToLog = ceil($nbRecords / 100);
641
-            $this->logger->info($nbRecords . ' records to save');
641
+            $this->logger->info($nbRecords.' records to save');
642 642
             $nbSaved = 0;
643 643
             $handle = fopen($jobCsvFile, 'r');
644 644
             $fields = [];
@@ -656,30 +656,30 @@  discard block
 block discarded – undo
656 656
                             $recordDataToInsert[$decodedColumnName] = $value === '' ? null : $value;
657 657
                         } else {
658 658
                             if ($columnName === 'Owner' || $columnName === 'Created_By' || $columnName === 'Modified_By') {
659
-                                $recordDataToInsert[$decodedColumnName . '_OwnerID'] = $value === '' ? null : $value;
660
-                                $recordDataToInsert[$decodedColumnName . '_OwnerName'] = $users[$value] ?? null;
661
-                            } else if ($table->hasColumn($decodedColumnName . '_ID')) {
662
-                                $recordDataToInsert[$decodedColumnName . '_ID'] = $value === '' ? null : $value;
659
+                                $recordDataToInsert[$decodedColumnName.'_OwnerID'] = $value === '' ? null : $value;
660
+                                $recordDataToInsert[$decodedColumnName.'_OwnerName'] = $users[$value] ?? null;
661
+                            } else if ($table->hasColumn($decodedColumnName.'_ID')) {
662
+                                $recordDataToInsert[$decodedColumnName.'_ID'] = $value === '' ? null : $value;
663 663
                             }
664 664
                         }
665 665
                     }
666 666
                     $this->connection->insert($tableName, $recordDataToInsert);
667 667
                     ++$nbSaved;
668 668
                     if (($nbSaved % $whenToLog) === 0) {
669
-                        $this->logger->info($nbSaved . '/' . $nbRecords . ' records processed');
669
+                        $this->logger->info($nbSaved.'/'.$nbRecords.' records processed');
670 670
                     }
671 671
                 }
672
-                $this->logger->info($nbSaved . ' records saved for module ' . $apiModuleName . ' and page ' . $page);
672
+                $this->logger->info($nbSaved.' records saved for module '.$apiModuleName.' and page '.$page);
673 673
                 fclose($handle);
674 674
             }
675 675
 
676 676
             // Step 5: Check if there is more results
677 677
             $hasMoreRecords = $jobDetails['data'][0]['result']['more_records'];
678 678
             if (!$hasMoreRecords) {
679
-                $this->logger->info('No more records for the module ' . $apiModuleName);
679
+                $this->logger->info('No more records for the module '.$apiModuleName);
680 680
                 break;
681 681
             }
682
-            $this->logger->info('More records to fetch for the module ' . $apiModuleName);
682
+            $this->logger->info('More records to fetch for the module '.$apiModuleName);
683 683
             ++$page;
684 684
         }
685 685
     }
Please login to merge, or discard this patch.