Completed
Push — 3.2.3 ( dc6ae9...b87a24 )
by
unknown
01:36
created
src/ZohoDatabaseCopier.php 1 patch
Spacing   +47 added lines, -47 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) {
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
                                     $lastPageInConfig = '1';
194 194
                                 }
195 195
                                 $lastActivityTime = new \DateTime($lastDateInConfig, new \DateTimeZone($dao->getZohoClient()->getTimezone()));
196
-                                $recordsPage = (int)$lastPageInConfig;
196
+                                $recordsPage = (int) $lastPageInConfig;
197 197
                             } else {
198 198
                                 $findDateByModifiedTime = true;
199 199
                             }
@@ -206,10 +206,10 @@  discard block
 block discarded – undo
206 206
                                 $lastActivityTime = new \DateTime($modifiedSince);
207 207
                             } else {
208 208
                                 if ($tableHasColumnModifiedTime) {
209
-                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
209
+                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
210 210
                                 }
211 211
                                 if (!$lastActivityTime && $tableHasColumnCreatedTime) {
212
-                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
212
+                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
213 213
                                 }
214 214
 
215 215
                                 if ($lastActivityTime !== null) {
@@ -248,7 +248,7 @@  discard block
 block discarded – undo
248 248
                         if ($zohoSyncConfigTableExists) {
249 249
                             $lastDate = $lastActivityTime ? $lastActivityTime->format('Y-m-d H:i:s') : date('Y-m-d H:i:s', 0);
250 250
                             $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__DATE', $tableName, $lastDate);
251
-                            $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__PAGE', $tableName, (string)$recordsPage);
251
+                            $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__PAGE', $tableName, (string) $recordsPage);
252 252
                         }
253 253
 
254 254
                         $recordsPaginationLastTime = $lastActivityTime;
@@ -257,10 +257,10 @@  discard block
 block discarded – undo
257 257
                         if ($zohoSyncConfigTableExists) {
258 258
                             $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
259 259
                             if ($tableHasColumnModifiedTime) {
260
-                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
260
+                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
261 261
                             }
262 262
                             if (!$latestDateToSave && $tableHasColumnCreatedTime) {
263
-                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
263
+                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
264 264
                             }
265 265
                             if (!$latestDateToSave) {
266 266
                                 $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
@@ -270,26 +270,26 @@  discard block
 block discarded – undo
270 270
                         }
271 271
                     }
272 272
                     $totalRecords = count($records);
273
-                    $this->logger->debug($totalRecords . ' records fetched.');
273
+                    $this->logger->debug($totalRecords.' records fetched.');
274 274
                     $deletedRecords = [];
275 275
                     $totalRecordsDeleted = 0;
276 276
                     if (($recordsPage - 1) === 1) {
277 277
                         $this->logger->notice(sprintf('Fetching the records to delete for module %s...', $dao->getPluralModuleName()));
278 278
                         $deletedRecords = $dao->getDeletedRecordIds($lastActivityTime);
279 279
                         $totalRecordsDeleted = count($deletedRecords);
280
-                        $this->logger->debug($totalRecordsDeleted . ' records fetched.');
280
+                        $this->logger->debug($totalRecordsDeleted.' records fetched.');
281 281
                     }
282 282
                 } else {
283 283
                     $this->logger->info(sprintf('Full copy started for module %s', $dao->getPluralModuleName()));
284 284
                     $this->logger->notice(sprintf('Fetching the records to insert/update for module ...%s', $dao->getPluralModuleName()));
285 285
                     $records = $dao->getRecords();
286 286
                     $totalRecords = count($records);
287
-                    $this->logger->debug($totalRecords . ' records fetched.');
287
+                    $this->logger->debug($totalRecords.' records fetched.');
288 288
                     $deletedRecords = [];
289 289
                     $stopAndhasMoreResults = false;
290 290
                 }
291 291
             } catch (ZCRMException $exception) {
292
-                $this->logger->error('Error when getting records for module ' . $dao->getPluralModuleName() . ': ' . $exception->getMessage(), [
292
+                $this->logger->error('Error when getting records for module '.$dao->getPluralModuleName().': '.$exception->getMessage(), [
293 293
                     'exception' => $exception
294 294
                 ]);
295 295
                 if ($throwErrors) {
@@ -299,7 +299,7 @@  discard block
 block discarded – undo
299 299
             }
300 300
             $this->logger->info(sprintf('Inserting/updating %s records into table %s...', $totalRecords, $tableName));
301 301
 
302
-            $select = $this->connection->prepare('SELECT * FROM ' . $tableName . ' WHERE id = :id');
302
+            $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id');
303 303
 
304 304
             $this->connection->beginTransaction();
305 305
 
@@ -380,7 +380,7 @@  discard block
 block discarded – undo
380 380
             }
381 381
 
382 382
             $this->logger->info(sprintf('Deleting %d records from table %s...', $totalRecordsDeleted, $tableName));
383
-            $sqlStatementUid = 'select uid from ' . $this->connection->quoteIdentifier($tableName) . ' where id = :id';
383
+            $sqlStatementUid = 'select uid from '.$this->connection->quoteIdentifier($tableName).' where id = :id';
384 384
             $processedRecords = 0;
385 385
             $logOffset = $totalRecordsDeleted >= 500 ? 100 : 50;
386 386
             foreach ($deletedRecords as $deletedRecord) {
@@ -412,10 +412,10 @@  discard block
 block discarded – undo
412 412
                 if ($zohoSyncConfigTableExists) {
413 413
                     $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
414 414
                     if ($tableHasColumnModifiedTime) {
415
-                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
415
+                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
416 416
                     }
417 417
                     if (!$latestDateToSave && $tableHasColumnCreatedTime) {
418
-                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
418
+                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
419 419
                     }
420 420
                     if (!$latestDateToSave) {
421 421
                         $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
@@ -439,7 +439,7 @@  discard block
 block discarded – undo
439 439
         $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
440 440
         $apiModuleName = $dao->getPluralModuleName();
441 441
 
442
-        $this->logger->notice('Starting bulk fetch for module ' . $apiModuleName . '...');
442
+        $this->logger->notice('Starting bulk fetch for module '.$apiModuleName.'...');
443 443
 
444 444
         $zohoClient = new ZohoClient([
445 445
             'client_id' => ZOHO_CRM_CLIENT_ID,
@@ -458,11 +458,11 @@  discard block
 block discarded – undo
458 458
             $oauthToken = $zohoClient->getZohoOAuthClient()->getAccessToken(ZOHO_CRM_CLIENT_CURRENT_USER_EMAIL);
459 459
 
460 460
             // Step 1: Create a bulk read job
461
-            $this->logger->info('Creating read job for module ' . $apiModuleName . ' and page ' . $page . '...');
462
-            $response = $client->request('POST', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read', [
461
+            $this->logger->info('Creating read job for module '.$apiModuleName.' and page '.$page.'...');
462
+            $response = $client->request('POST', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read', [
463 463
                 'http_errors' => false,
464 464
                 'headers' => [
465
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
465
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
466 466
                 ],
467 467
                 'json' => [
468 468
                     'query' => [
@@ -480,23 +480,23 @@  discard block
 block discarded – undo
480 480
 
481 481
                 // We don't care about the job status right now, it will be checked later
482 482
             } else {
483
-                $this->logger->error('Cannot create bulk read query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
483
+                $this->logger->error('Cannot create bulk read query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
484 484
                 break;
485 485
             }
486 486
 
487 487
             if ($jobId === null) {
488
-                $this->logger->error('JobID cannot be null. json:' . $resultStr);
488
+                $this->logger->error('JobID cannot be null. json:'.$resultStr);
489 489
                 break;
490 490
             }
491 491
 
492 492
             // Step 2: Check job status
493 493
             $jobDetails = null;
494 494
             while (true) {
495
-                $this->logger->info('Checking job ' . $jobId . ' status for module ' . $apiModuleName . ' and page ' . $page . '...');
496
-                $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId, [
495
+                $this->logger->info('Checking job '.$jobId.' status for module '.$apiModuleName.' and page '.$page.'...');
496
+                $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId, [
497 497
                     'http_errors' => false,
498 498
                     'headers' => [
499
-                        'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
499
+                        'Authorization' => 'Zoho-oauthtoken '.$oauthToken
500 500
                     ]
501 501
                 ]);
502 502
                 if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
@@ -514,15 +514,15 @@  discard block
 block discarded – undo
514 514
                             $jobDetails = $json;
515 515
                             break;
516 516
                         } else {
517
-                            $this->logger->info('Unsupported job status: ' . $resultStr);
517
+                            $this->logger->info('Unsupported job status: '.$resultStr);
518 518
                             break;
519 519
                         }
520 520
                     } else {
521
-                        $this->logger->error('Unsupported response: ' . $resultStr);
521
+                        $this->logger->error('Unsupported response: '.$resultStr);
522 522
                         break;
523 523
                     }
524 524
                 } else {
525
-                    $this->logger->error('Cannot get bulk job status query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
525
+                    $this->logger->error('Cannot get bulk job status query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
526 526
                     break;
527 527
                 }
528 528
                 sleep(15);
@@ -530,31 +530,31 @@  discard block
 block discarded – undo
530 530
 
531 531
             // Step 3: Download the result
532 532
             if ($jobDetails === null) {
533
-                $this->logger->error('JobDetails cannot be empty. json:' . $resultStr);
533
+                $this->logger->error('JobDetails cannot be empty. json:'.$resultStr);
534 534
                 break;
535 535
             }
536 536
             $this->logger->debug(json_encode($jobDetails));
537
-            $this->logger->info('Downloading zip file for module ' . $apiModuleName . ' and page ' . $page . '...');
538
-            $jobZipFile = '/tmp/job_' . $dao->getZCRMModule()->getAPIName() . '_' . $jobDetails['data'][0]['id'] . '.zip';
537
+            $this->logger->info('Downloading zip file for module '.$apiModuleName.' and page '.$page.'...');
538
+            $jobZipFile = '/tmp/job_'.$dao->getZCRMModule()->getAPIName().'_'.$jobDetails['data'][0]['id'].'.zip';
539 539
             $jobCsvPath = '/tmp/job_extract';
540
-            $jobCsvFile = '/tmp/job_extract/' . $jobDetails['data'][0]['id'] . '.csv';
540
+            $jobCsvFile = '/tmp/job_extract/'.$jobDetails['data'][0]['id'].'.csv';
541 541
             $canProcessCsv = false;
542 542
 
543
-            $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId . '/result', [
543
+            $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId.'/result', [
544 544
                 'http_errors' => false,
545 545
                 'headers' => [
546
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
546
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
547 547
                 ],
548 548
                 'sink' => $jobZipFile
549 549
             ]);
550 550
             if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
551
-                $this->logger->info('Extracting ' . $jobZipFile . ' file for module ' . $apiModuleName . ' and page ' . $page . '...');
551
+                $this->logger->info('Extracting '.$jobZipFile.' file for module '.$apiModuleName.' and page '.$page.'...');
552 552
                 $zip = new ZipArchive();
553 553
                 $res = $zip->open($jobZipFile);
554 554
                 if ($res === TRUE) {
555 555
                     $zip->extractTo($jobCsvPath);
556 556
                     $zip->close();
557
-                    $this->logger->info('File extracted in ' . $jobCsvFile);
557
+                    $this->logger->info('File extracted in '.$jobCsvFile);
558 558
                     $canProcessCsv = true;
559 559
                 } else {
560 560
                     switch ($res) {
@@ -586,11 +586,11 @@  discard block
 block discarded – undo
586 586
                             $zipErrorMessage = "Unknow (Code $res)";
587 587
                             break;
588 588
                     }
589
-                    $this->logger->error('Error when extracting zip file: ' . $zipErrorMessage);
589
+                    $this->logger->error('Error when extracting zip file: '.$zipErrorMessage);
590 590
                     break;
591 591
                 }
592 592
             } else {
593
-                $this->logger->error('Cannot download results for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
593
+                $this->logger->error('Cannot download results for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
594 594
                 break;
595 595
             }
596 596
 
@@ -611,7 +611,7 @@  discard block
 block discarded – undo
611 611
             $this->logger->info('Saving records to db...');
612 612
             $nbRecords = $jobDetails['data'][0]['result']['count'];
613 613
             $whenToLog = ceil($nbRecords / 100);
614
-            $this->logger->info($nbRecords . ' records to save');
614
+            $this->logger->info($nbRecords.' records to save');
615 615
             $nbSaved = 0;
616 616
             $handle = fopen($jobCsvFile, 'r');
617 617
             $fields = [];
@@ -629,30 +629,30 @@  discard block
 block discarded – undo
629 629
                             $recordDataToInsert[$decodedColumnName] = $value === '' ? null : $value;
630 630
                         } else {
631 631
                             if ($columnName === 'Owner' || $columnName === 'Created_By' || $columnName === 'Modified_By') {
632
-                                $recordDataToInsert[$decodedColumnName . '_OwnerID'] = $value === '' ? null : $value;
633
-                                $recordDataToInsert[$decodedColumnName . '_OwnerName'] = $users[$value] ?? null;
634
-                            } else if ($table->hasColumn($decodedColumnName . '_ID')) {
635
-                                $recordDataToInsert[$decodedColumnName . '_ID'] = $value === '' ? null : $value;
632
+                                $recordDataToInsert[$decodedColumnName.'_OwnerID'] = $value === '' ? null : $value;
633
+                                $recordDataToInsert[$decodedColumnName.'_OwnerName'] = $users[$value] ?? null;
634
+                            } else if ($table->hasColumn($decodedColumnName.'_ID')) {
635
+                                $recordDataToInsert[$decodedColumnName.'_ID'] = $value === '' ? null : $value;
636 636
                             }
637 637
                         }
638 638
                     }
639 639
                     $this->connection->insert($tableName, $recordDataToInsert);
640 640
                     ++$nbSaved;
641 641
                     if (($nbSaved % $whenToLog) === 0) {
642
-                        $this->logger->info($nbSaved . '/' . $nbRecords . ' records processed');
642
+                        $this->logger->info($nbSaved.'/'.$nbRecords.' records processed');
643 643
                     }
644 644
                 }
645
-                $this->logger->info($nbSaved . ' records saved for module ' . $apiModuleName . ' and page ' . $page);
645
+                $this->logger->info($nbSaved.' records saved for module '.$apiModuleName.' and page '.$page);
646 646
                 fclose($handle);
647 647
             }
648 648
 
649 649
             // Step 5: Check if there is more results
650 650
             $hasMoreRecords = $jobDetails['data'][0]['result']['more_records'];
651 651
             if (!$hasMoreRecords) {
652
-                $this->logger->info('No more records for the module ' . $apiModuleName);
652
+                $this->logger->info('No more records for the module '.$apiModuleName);
653 653
                 break;
654 654
             }
655
-            $this->logger->info('More records to fetch for the module ' . $apiModuleName);
655
+            $this->logger->info('More records to fetch for the module '.$apiModuleName);
656 656
             ++$page;
657 657
         }
658 658
     }
Please login to merge, or discard this patch.