Completed
Push — 3.2.3 ( 0535c0...dc6ae9 )
by
unknown
01:37
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) {
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
                                     $lastPageInConfig = '1';
182 182
                                 }
183 183
                                 $lastActivityTime = new \DateTime($lastDateInConfig);
184
-                                $recordsPage = (int)$lastPageInConfig;
184
+                                $recordsPage = (int) $lastPageInConfig;
185 185
                             } else {
186 186
                                 $findDateByModifiedTime = true;
187 187
                             }
@@ -194,10 +194,10 @@  discard block
 block discarded – undo
194 194
                                 $lastActivityTime = new \DateTime($modifiedSince);
195 195
                             } else {
196 196
                                 if ($tableDetail->hasColumn('modifiedTime')) {
197
-                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
197
+                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
198 198
                                 }
199 199
                                 if (!$lastActivityTime && $tableDetail->hasColumn('createdTime')) {
200
-                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
200
+                                    $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
201 201
                                 }
202 202
 
203 203
                                 if ($lastActivityTime !== null) {
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
                         if ($zohoSyncConfigTableExists) {
225 225
                             $lastDate = $lastActivityTime ? $lastActivityTime->format('Y-m-d H:i:s') : date('Y-m-d H:i:s', 0);
226 226
                             $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__DATE', $tableName, $lastDate);
227
-                            $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__PAGE', $tableName, (string)$recordsPage);
227
+                            $this->upsertZohoConfig('FETCH_RECORDS_MODIFIED_SINCE__PAGE', $tableName, (string) $recordsPage);
228 228
                         }
229 229
 
230 230
                         $recordsPaginationLastTime = $lastActivityTime;
@@ -234,10 +234,10 @@  discard block
 block discarded – undo
234 234
                             $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
235 235
                             $tableDetail = $this->connection->getSchemaManager()->listTableDetails($tableName);
236 236
                             if ($tableDetail->hasColumn('modifiedTime')) {
237
-                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
237
+                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
238 238
                             }
239 239
                             if (!$latestDateToSave && $tableDetail->hasColumn('createdTime')) {
240
-                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
240
+                                $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
241 241
                             }
242 242
                             if (!$latestDateToSave) {
243 243
                                 $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
@@ -247,26 +247,26 @@  discard block
 block discarded – undo
247 247
                         }
248 248
                     }
249 249
                     $totalRecords = count($records);
250
-                    $this->logger->debug($totalRecords . ' records fetched.');
250
+                    $this->logger->debug($totalRecords.' records fetched.');
251 251
                     $deletedRecords = [];
252 252
                     $totalRecordsDeleted = 0;
253 253
                     if (($recordsPage - 1) === 1) {
254 254
                         $this->logger->notice(sprintf('Fetching the records to delete for module %s...', $dao->getPluralModuleName()));
255 255
                         $deletedRecords = $dao->getDeletedRecordIds($lastActivityTime);
256 256
                         $totalRecordsDeleted = count($deletedRecords);
257
-                        $this->logger->debug($totalRecordsDeleted . ' records fetched.');
257
+                        $this->logger->debug($totalRecordsDeleted.' records fetched.');
258 258
                     }
259 259
                 } else {
260 260
                     $this->logger->info(sprintf('Full copy started for module %s', $dao->getPluralModuleName()));
261 261
                     $this->logger->notice(sprintf('Fetching the records to insert/update for module ...%s', $dao->getPluralModuleName()));
262 262
                     $records = $dao->getRecords();
263 263
                     $totalRecords = count($records);
264
-                    $this->logger->debug($totalRecords . ' records fetched.');
264
+                    $this->logger->debug($totalRecords.' records fetched.');
265 265
                     $deletedRecords = [];
266 266
                     $stopAndhasMoreResults = false;
267 267
                 }
268 268
             } catch (ZCRMException $exception) {
269
-                $this->logger->error('Error when getting records for module ' . $dao->getPluralModuleName() . ': ' . $exception->getMessage(), [
269
+                $this->logger->error('Error when getting records for module '.$dao->getPluralModuleName().': '.$exception->getMessage(), [
270 270
                     'exception' => $exception
271 271
                 ]);
272 272
                 if ($throwErrors) {
@@ -278,7 +278,7 @@  discard block
 block discarded – undo
278 278
 
279 279
             $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
280 280
 
281
-            $select = $this->connection->prepare('SELECT * FROM ' . $tableName . ' WHERE id = :id');
281
+            $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id');
282 282
 
283 283
             $this->connection->beginTransaction();
284 284
 
@@ -348,7 +348,7 @@  discard block
 block discarded – undo
348 348
             }
349 349
 
350 350
             $this->logger->info(sprintf('Deleting %d records from table %s...', $totalRecordsDeleted, $tableName));
351
-            $sqlStatementUid = 'select uid from ' . $this->connection->quoteIdentifier($tableName) . ' where id = :id';
351
+            $sqlStatementUid = 'select uid from '.$this->connection->quoteIdentifier($tableName).' where id = :id';
352 352
             $processedRecords = 0;
353 353
             $logOffset = $totalRecordsDeleted >= 500 ? 100 : 50;
354 354
             foreach ($deletedRecords as $deletedRecord) {
@@ -381,10 +381,10 @@  discard block
 block discarded – undo
381 381
                     $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
382 382
                     $tableDetail = $this->connection->getSchemaManager()->listTableDetails($tableName);
383 383
                     if ($tableDetail->hasColumn('modifiedTime')) {
384
-                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
384
+                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
385 385
                     }
386 386
                     if (!$latestDateToSave && $tableDetail->hasColumn('createdTime')) {
387
-                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
387
+                        $latestDateToSave = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
388 388
                     }
389 389
                     if (!$latestDateToSave) {
390 390
                         $latestDateToSave = $currentDateTime->format('Y-m-d H:i:s');
@@ -408,7 +408,7 @@  discard block
 block discarded – undo
408 408
         $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
409 409
         $apiModuleName = $dao->getPluralModuleName();
410 410
 
411
-        $this->logger->notice('Starting bulk fetch for module ' . $apiModuleName . '...');
411
+        $this->logger->notice('Starting bulk fetch for module '.$apiModuleName.'...');
412 412
 
413 413
         $zohoClient = new ZohoClient([
414 414
             'client_id' => ZOHO_CRM_CLIENT_ID,
@@ -427,11 +427,11 @@  discard block
 block discarded – undo
427 427
             $oauthToken = $zohoClient->getZohoOAuthClient()->getAccessToken(ZOHO_CRM_CLIENT_CURRENT_USER_EMAIL);
428 428
 
429 429
             // Step 1: Create a bulk read job
430
-            $this->logger->info('Creating read job for module ' . $apiModuleName . ' and page ' . $page . '...');
431
-            $response = $client->request('POST', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read', [
430
+            $this->logger->info('Creating read job for module '.$apiModuleName.' and page '.$page.'...');
431
+            $response = $client->request('POST', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read', [
432 432
                 'http_errors' => false,
433 433
                 'headers' => [
434
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
434
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
435 435
                 ],
436 436
                 'json' => [
437 437
                     'query' => [
@@ -449,23 +449,23 @@  discard block
 block discarded – undo
449 449
 
450 450
                 // We don't care about the job status right now, it will be checked later
451 451
             } else {
452
-                $this->logger->error('Cannot create bulk read query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
452
+                $this->logger->error('Cannot create bulk read query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
453 453
                 break;
454 454
             }
455 455
 
456 456
             if ($jobId === null) {
457
-                $this->logger->error('JobID cannot be null. json:' . $resultStr);
457
+                $this->logger->error('JobID cannot be null. json:'.$resultStr);
458 458
                 break;
459 459
             }
460 460
 
461 461
             // Step 2: Check job status
462 462
             $jobDetails = null;
463 463
             while (true) {
464
-                $this->logger->info('Checking job ' . $jobId . ' status for module ' . $apiModuleName . ' and page ' . $page . '...');
465
-                $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId, [
464
+                $this->logger->info('Checking job '.$jobId.' status for module '.$apiModuleName.' and page '.$page.'...');
465
+                $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId, [
466 466
                     'http_errors' => false,
467 467
                     'headers' => [
468
-                        'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
468
+                        'Authorization' => 'Zoho-oauthtoken '.$oauthToken
469 469
                     ]
470 470
                 ]);
471 471
                 if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
@@ -483,15 +483,15 @@  discard block
 block discarded – undo
483 483
                             $jobDetails = $json;
484 484
                             break;
485 485
                         } else {
486
-                            $this->logger->info('Unsupported job status: ' . $resultStr);
486
+                            $this->logger->info('Unsupported job status: '.$resultStr);
487 487
                             break;
488 488
                         }
489 489
                     } else {
490
-                        $this->logger->error('Unsupported response: ' . $resultStr);
490
+                        $this->logger->error('Unsupported response: '.$resultStr);
491 491
                         break;
492 492
                     }
493 493
                 } else {
494
-                    $this->logger->error('Cannot get bulk job status query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
494
+                    $this->logger->error('Cannot get bulk job status query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
495 495
                     break;
496 496
                 }
497 497
                 sleep(15);
@@ -499,31 +499,31 @@  discard block
 block discarded – undo
499 499
 
500 500
             // Step 3: Download the result
501 501
             if ($jobDetails === null) {
502
-                $this->logger->error('JobDetails cannot be empty. json:' . $resultStr);
502
+                $this->logger->error('JobDetails cannot be empty. json:'.$resultStr);
503 503
                 break;
504 504
             }
505 505
             $this->logger->debug(json_encode($jobDetails));
506
-            $this->logger->info('Downloading zip file for module ' . $apiModuleName . ' and page ' . $page . '...');
507
-            $jobZipFile = '/tmp/job_' . $dao->getZCRMModule()->getAPIName() . '_' . $jobDetails['data'][0]['id'] . '.zip';
506
+            $this->logger->info('Downloading zip file for module '.$apiModuleName.' and page '.$page.'...');
507
+            $jobZipFile = '/tmp/job_'.$dao->getZCRMModule()->getAPIName().'_'.$jobDetails['data'][0]['id'].'.zip';
508 508
             $jobCsvPath = '/tmp/job_extract';
509
-            $jobCsvFile = '/tmp/job_extract/' . $jobDetails['data'][0]['id'] . '.csv';
509
+            $jobCsvFile = '/tmp/job_extract/'.$jobDetails['data'][0]['id'].'.csv';
510 510
             $canProcessCsv = false;
511 511
 
512
-            $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId . '/result', [
512
+            $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId.'/result', [
513 513
                 'http_errors' => false,
514 514
                 'headers' => [
515
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
515
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
516 516
                 ],
517 517
                 'sink' => $jobZipFile
518 518
             ]);
519 519
             if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
520
-                $this->logger->info('Extracting ' . $jobZipFile . ' file for module ' . $apiModuleName . ' and page ' . $page . '...');
520
+                $this->logger->info('Extracting '.$jobZipFile.' file for module '.$apiModuleName.' and page '.$page.'...');
521 521
                 $zip = new ZipArchive();
522 522
                 $res = $zip->open($jobZipFile);
523 523
                 if ($res === TRUE) {
524 524
                     $zip->extractTo($jobCsvPath);
525 525
                     $zip->close();
526
-                    $this->logger->info('File extracted in ' . $jobCsvFile);
526
+                    $this->logger->info('File extracted in '.$jobCsvFile);
527 527
                     $canProcessCsv = true;
528 528
                 } else {
529 529
                     switch ($res) {
@@ -555,11 +555,11 @@  discard block
 block discarded – undo
555 555
                             $zipErrorMessage = "Unknow (Code $res)";
556 556
                             break;
557 557
                     }
558
-                    $this->logger->error('Error when extracting zip file: ' . $zipErrorMessage);
558
+                    $this->logger->error('Error when extracting zip file: '.$zipErrorMessage);
559 559
                     break;
560 560
                 }
561 561
             } else {
562
-                $this->logger->error('Cannot download results for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
562
+                $this->logger->error('Cannot download results for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
563 563
                 break;
564 564
             }
565 565
 
@@ -580,7 +580,7 @@  discard block
 block discarded – undo
580 580
             $this->logger->info('Saving records to db...');
581 581
             $nbRecords = $jobDetails['data'][0]['result']['count'];
582 582
             $whenToLog = ceil($nbRecords / 100);
583
-            $this->logger->info($nbRecords . ' records to save');
583
+            $this->logger->info($nbRecords.' records to save');
584 584
             $nbSaved = 0;
585 585
             $handle = fopen($jobCsvFile, 'r');
586 586
             $fields = [];
@@ -598,30 +598,30 @@  discard block
 block discarded – undo
598 598
                             $recordDataToInsert[$decodedColumnName] = $value === '' ? null : $value;
599 599
                         } else {
600 600
                             if ($columnName === 'Owner' || $columnName === 'Created_By' || $columnName === 'Modified_By') {
601
-                                $recordDataToInsert[$decodedColumnName . '_OwnerID'] = $value === '' ? null : $value;
602
-                                $recordDataToInsert[$decodedColumnName . '_OwnerName'] = $users[$value] ?? null;
603
-                            } else if ($table->hasColumn($decodedColumnName . '_ID')) {
604
-                                $recordDataToInsert[$decodedColumnName . '_ID'] = $value === '' ? null : $value;
601
+                                $recordDataToInsert[$decodedColumnName.'_OwnerID'] = $value === '' ? null : $value;
602
+                                $recordDataToInsert[$decodedColumnName.'_OwnerName'] = $users[$value] ?? null;
603
+                            } else if ($table->hasColumn($decodedColumnName.'_ID')) {
604
+                                $recordDataToInsert[$decodedColumnName.'_ID'] = $value === '' ? null : $value;
605 605
                             }
606 606
                         }
607 607
                     }
608 608
                     $this->connection->insert($tableName, $recordDataToInsert);
609 609
                     ++$nbSaved;
610 610
                     if (($nbSaved % $whenToLog) === 0) {
611
-                        $this->logger->info($nbSaved . '/' . $nbRecords . ' records processed');
611
+                        $this->logger->info($nbSaved.'/'.$nbRecords.' records processed');
612 612
                     }
613 613
                 }
614
-                $this->logger->info($nbSaved . ' records saved for module ' . $apiModuleName . ' and page ' . $page);
614
+                $this->logger->info($nbSaved.' records saved for module '.$apiModuleName.' and page '.$page);
615 615
                 fclose($handle);
616 616
             }
617 617
 
618 618
             // Step 5: Check if there is more results
619 619
             $hasMoreRecords = $jobDetails['data'][0]['result']['more_records'];
620 620
             if (!$hasMoreRecords) {
621
-                $this->logger->info('No more records for the module ' . $apiModuleName);
621
+                $this->logger->info('No more records for the module '.$apiModuleName);
622 622
                 break;
623 623
             }
624
-            $this->logger->info('More records to fetch for the module ' . $apiModuleName);
624
+            $this->logger->info('More records to fetch for the module '.$apiModuleName);
625 625
             ++$page;
626 626
         }
627 627
     }
Please login to merge, or discard this patch.