Completed
Push — 3.2.3 ( 0535c0 )
by
unknown
01:29
created
src/ZohoDatabaseCopier.php 1 patch
Spacing   +45 added lines, -45 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) {
@@ -397,7 +397,7 @@  discard block
 block discarded – undo
397 397
         $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
398 398
         $apiModuleName = $dao->getPluralModuleName();
399 399
 
400
-        $this->logger->notice('Starting bulk fetch for module ' . $apiModuleName . '...');
400
+        $this->logger->notice('Starting bulk fetch for module '.$apiModuleName.'...');
401 401
 
402 402
         $zohoClient = new ZohoClient([
403 403
             'client_id' => ZOHO_CRM_CLIENT_ID,
@@ -416,11 +416,11 @@  discard block
 block discarded – undo
416 416
             $oauthToken = $zohoClient->getZohoOAuthClient()->getAccessToken(ZOHO_CRM_CLIENT_CURRENT_USER_EMAIL);
417 417
 
418 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', [
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 421
                 'http_errors' => false,
422 422
                 'headers' => [
423
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
423
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
424 424
                 ],
425 425
                 'json' => [
426 426
                     'query' => [
@@ -438,23 +438,23 @@  discard block
 block discarded – undo
438 438
 
439 439
                 // We don't care about the job status right now, it will be checked later
440 440
             } else {
441
-                $this->logger->error('Cannot create bulk read query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
441
+                $this->logger->error('Cannot create bulk read query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
442 442
                 break;
443 443
             }
444 444
 
445 445
             if ($jobId === null) {
446
-                $this->logger->error('JobID cannot be null. json:' . $resultStr);
446
+                $this->logger->error('JobID cannot be null. json:'.$resultStr);
447 447
                 break;
448 448
             }
449 449
 
450 450
             // Step 2: Check job status
451 451
             $jobDetails = null;
452 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, [
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 455
                     'http_errors' => false,
456 456
                     'headers' => [
457
-                        'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
457
+                        'Authorization' => 'Zoho-oauthtoken '.$oauthToken
458 458
                     ]
459 459
                 ]);
460 460
                 if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
@@ -472,15 +472,15 @@  discard block
 block discarded – undo
472 472
                             $jobDetails = $json;
473 473
                             break;
474 474
                         } else {
475
-                            $this->logger->info('Unsupported job status: ' . $resultStr);
475
+                            $this->logger->info('Unsupported job status: '.$resultStr);
476 476
                             break;
477 477
                         }
478 478
                     } else {
479
-                        $this->logger->error('Unsupported response: ' . $resultStr);
479
+                        $this->logger->error('Unsupported response: '.$resultStr);
480 480
                         break;
481 481
                     }
482 482
                 } else {
483
-                    $this->logger->error('Cannot get bulk job status query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
483
+                    $this->logger->error('Cannot get bulk job status query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
484 484
                     break;
485 485
                 }
486 486
                 sleep(15);
@@ -488,31 +488,31 @@  discard block
 block discarded – undo
488 488
 
489 489
             // Step 3: Download the result
490 490
             if ($jobDetails === null) {
491
-                $this->logger->error('JobDetails cannot be empty. json:' . $resultStr);
491
+                $this->logger->error('JobDetails cannot be empty. json:'.$resultStr);
492 492
                 break;
493 493
             }
494 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';
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 497
             $jobCsvPath = '/tmp/job_extract';
498
-            $jobCsvFile = '/tmp/job_extract/' . $jobDetails['data'][0]['id'] . '.csv';
498
+            $jobCsvFile = '/tmp/job_extract/'.$jobDetails['data'][0]['id'].'.csv';
499 499
             $canProcessCsv = false;
500 500
 
501
-            $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId . '/result', [
501
+            $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId.'/result', [
502 502
                 'http_errors' => false,
503 503
                 'headers' => [
504
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
504
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
505 505
                 ],
506 506
                 'sink' => $jobZipFile
507 507
             ]);
508 508
             if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
509
-                $this->logger->info('Extracting ' . $jobZipFile . ' file for module ' . $apiModuleName . ' and page ' . $page . '...');
509
+                $this->logger->info('Extracting '.$jobZipFile.' file for module '.$apiModuleName.' and page '.$page.'...');
510 510
                 $zip = new ZipArchive();
511 511
                 $res = $zip->open($jobZipFile);
512 512
                 if ($res === TRUE) {
513 513
                     $zip->extractTo($jobCsvPath);
514 514
                     $zip->close();
515
-                    $this->logger->info('File extracted in ' . $jobCsvFile);
515
+                    $this->logger->info('File extracted in '.$jobCsvFile);
516 516
                     $canProcessCsv = true;
517 517
                 } else {
518 518
                     switch ($res) {
@@ -544,11 +544,11 @@  discard block
 block discarded – undo
544 544
                             $zipErrorMessage = "Unknow (Code $res)";
545 545
                             break;
546 546
                     }
547
-                    $this->logger->error('Error when extracting zip file: ' . $zipErrorMessage);
547
+                    $this->logger->error('Error when extracting zip file: '.$zipErrorMessage);
548 548
                     break;
549 549
                 }
550 550
             } else {
551
-                $this->logger->error('Cannot download results for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
551
+                $this->logger->error('Cannot download results for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
552 552
                 break;
553 553
             }
554 554
 
@@ -569,7 +569,7 @@  discard block
 block discarded – undo
569 569
             $this->logger->info('Saving records to db...');
570 570
             $nbRecords = $jobDetails['data'][0]['result']['count'];
571 571
             $whenToLog = ceil($nbRecords / 100);
572
-            $this->logger->info($nbRecords . ' records to save');
572
+            $this->logger->info($nbRecords.' records to save');
573 573
             $nbSaved = 0;
574 574
             $handle = fopen($jobCsvFile, 'r');
575 575
             $fields = [];
@@ -587,30 +587,30 @@  discard block
 block discarded – undo
587 587
                             $recordDataToInsert[$decodedColumnName] = $value === '' ? null : $value;
588 588
                         } else {
589 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;
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 594
                             }
595 595
                         }
596 596
                     }
597 597
                     $this->connection->insert($tableName, $recordDataToInsert);
598 598
                     ++$nbSaved;
599 599
                     if (($nbSaved % $whenToLog) === 0) {
600
-                        $this->logger->info($nbSaved . '/' . $nbRecords . ' records processed');
600
+                        $this->logger->info($nbSaved.'/'.$nbRecords.' records processed');
601 601
                     }
602 602
                 }
603
-                $this->logger->info($nbSaved . ' records saved for module ' . $apiModuleName . ' and page ' . $page);
603
+                $this->logger->info($nbSaved.' records saved for module '.$apiModuleName.' and page '.$page);
604 604
                 fclose($handle);
605 605
             }
606 606
 
607 607
             // Step 5: Check if there is more results
608 608
             $hasMoreRecords = $jobDetails['data'][0]['result']['more_records'];
609 609
             if (!$hasMoreRecords) {
610
-                $this->logger->info('No more records for the module ' . $apiModuleName);
610
+                $this->logger->info('No more records for the module '.$apiModuleName);
611 611
                 break;
612 612
             }
613
-            $this->logger->info('More records to fetch for the module ' . $apiModuleName);
613
+            $this->logger->info('More records to fetch for the module '.$apiModuleName);
614 614
             ++$page;
615 615
         }
616 616
     }
Please login to merge, or discard this patch.