Completed
Push — 3.2.1 ( 63da03 )
by
unknown
06:11
created
src/ZohoDatabaseCopier.php 1 patch
Spacing   +41 added lines, -41 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) {
@@ -162,10 +162,10 @@  discard block
 block discarded – undo
162 162
                             $lastActivityTime = new \DateTime($modifiedSince);
163 163
                         } else {
164 164
                             if ($tableDetail->hasColumn('modifiedTime')) {
165
-                                $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName);
165
+                                $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName);
166 166
                             }
167 167
                             if (!$lastActivityTime && $tableDetail->hasColumn('createdTime')) {
168
-                                $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName);
168
+                                $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName);
169 169
                             }
170 170
 
171 171
                             if ($lastActivityTime !== null) {
@@ -191,26 +191,26 @@  discard block
 block discarded – undo
191 191
                         $recordsPage++;
192 192
                     }
193 193
                     $totalRecords = count($records);
194
-                    $this->logger->debug($totalRecords . ' records fetched.');
194
+                    $this->logger->debug($totalRecords.' records fetched.');
195 195
                     $deletedRecords = [];
196 196
                     $totalRecordsDeleted = 0;
197 197
                     if (($recordsPage - 1) === 1) {
198 198
                         $this->logger->notice(sprintf('Fetching the records to delete for module %s...', $dao->getPluralModuleName()));
199 199
                         $deletedRecords = $dao->getDeletedRecordIds($lastActivityTime);
200 200
                         $totalRecordsDeleted = count($deletedRecords);
201
-                        $this->logger->debug($totalRecordsDeleted . ' records fetched.');
201
+                        $this->logger->debug($totalRecordsDeleted.' records fetched.');
202 202
                     }
203 203
                 } else {
204 204
                     $this->logger->info(sprintf('Full copy started for module %s', $dao->getPluralModuleName()));
205 205
                     $this->logger->notice(sprintf('Fetching the records to insert/update for module ...%s', $dao->getPluralModuleName()));
206 206
                     $records = $dao->getRecords();
207 207
                     $totalRecords = count($records);
208
-                    $this->logger->debug($totalRecords . ' records fetched.');
208
+                    $this->logger->debug($totalRecords.' records fetched.');
209 209
                     $deletedRecords = [];
210 210
                     $stopAndhasMoreResults = false;
211 211
                 }
212 212
             } catch (ZCRMException $exception) {
213
-                $this->logger->error('Error when getting records for module ' . $dao->getPluralModuleName() . ': ' . $exception->getMessage(), [
213
+                $this->logger->error('Error when getting records for module '.$dao->getPluralModuleName().': '.$exception->getMessage(), [
214 214
                     'exception' => $exception
215 215
                 ]);
216 216
                 if ($throwErrors) {
@@ -222,7 +222,7 @@  discard block
 block discarded – undo
222 222
 
223 223
             $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
224 224
 
225
-            $select = $this->connection->prepare('SELECT * FROM ' . $tableName . ' WHERE id = :id');
225
+            $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id');
226 226
 
227 227
             $this->connection->beginTransaction();
228 228
 
@@ -292,7 +292,7 @@  discard block
 block discarded – undo
292 292
             }
293 293
 
294 294
             $this->logger->info(sprintf('Deleting %d records from table %s...', $totalRecordsDeleted, $tableName));
295
-            $sqlStatementUid = 'select uid from ' . $this->connection->quoteIdentifier($tableName) . ' where id = :id';
295
+            $sqlStatementUid = 'select uid from '.$this->connection->quoteIdentifier($tableName).' where id = :id';
296 296
             $processedRecords = 0;
297 297
             $logOffset = $totalRecordsDeleted >= 500 ? 100 : 50;
298 298
             foreach ($deletedRecords as $deletedRecord) {
@@ -333,7 +333,7 @@  discard block
 block discarded – undo
333 333
         $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
334 334
         $apiModuleName = $dao->getPluralModuleName();
335 335
 
336
-        $this->logger->notice('Starting bulk fetch for module ' . $apiModuleName . '...');
336
+        $this->logger->notice('Starting bulk fetch for module '.$apiModuleName.'...');
337 337
 
338 338
         $zohoClient = new ZohoClient([
339 339
             'client_id' => ZOHO_CRM_CLIENT_ID,
@@ -352,11 +352,11 @@  discard block
 block discarded – undo
352 352
             $oauthToken = $zohoClient->getZohoOAuthClient()->getAccessToken(ZOHO_CRM_CLIENT_CURRENT_USER_EMAIL);
353 353
 
354 354
             // Step 1: Create a bulk read job
355
-            $this->logger->info('Creating read job for module ' . $apiModuleName . ' and page ' . $page . '...');
356
-            $response = $client->request('POST', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read', [
355
+            $this->logger->info('Creating read job for module '.$apiModuleName.' and page '.$page.'...');
356
+            $response = $client->request('POST', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read', [
357 357
                 'http_errors' => false,
358 358
                 'headers' => [
359
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
359
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
360 360
                 ],
361 361
                 'json' => [
362 362
                     'query' => [
@@ -374,23 +374,23 @@  discard block
 block discarded – undo
374 374
 
375 375
                 // We don't care about the job status right now, it will be checked later
376 376
             } else {
377
-                $this->logger->error('Cannot create bulk read query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
377
+                $this->logger->error('Cannot create bulk read query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
378 378
                 break;
379 379
             }
380 380
 
381 381
             if ($jobId === null) {
382
-                $this->logger->error('JobID cannot be null. json:' . $resultStr);
382
+                $this->logger->error('JobID cannot be null. json:'.$resultStr);
383 383
                 break;
384 384
             }
385 385
 
386 386
             // Step 2: Check job status
387 387
             $jobDetails = null;
388 388
             while (true) {
389
-                $this->logger->info('Checking job ' . $jobId . ' status for module ' . $apiModuleName . ' and page ' . $page . '...');
390
-                $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId, [
389
+                $this->logger->info('Checking job '.$jobId.' status for module '.$apiModuleName.' and page '.$page.'...');
390
+                $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId, [
391 391
                     'http_errors' => false,
392 392
                     'headers' => [
393
-                        'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
393
+                        'Authorization' => 'Zoho-oauthtoken '.$oauthToken
394 394
                     ]
395 395
                 ]);
396 396
                 if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
@@ -408,15 +408,15 @@  discard block
 block discarded – undo
408 408
                             $jobDetails = $json;
409 409
                             break;
410 410
                         } else {
411
-                            $this->logger->info('Unsupported job status: ' . $resultStr);
411
+                            $this->logger->info('Unsupported job status: '.$resultStr);
412 412
                             break;
413 413
                         }
414 414
                     } else {
415
-                        $this->logger->error('Unsupported response: ' . $resultStr);
415
+                        $this->logger->error('Unsupported response: '.$resultStr);
416 416
                         break;
417 417
                     }
418 418
                 } else {
419
-                    $this->logger->error('Cannot get bulk job status query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
419
+                    $this->logger->error('Cannot get bulk job status query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
420 420
                     break;
421 421
                 }
422 422
                 sleep(15);
@@ -424,31 +424,31 @@  discard block
 block discarded – undo
424 424
 
425 425
             // Step 3: Download the result
426 426
             if ($jobDetails === null) {
427
-                $this->logger->error('JobDetails cannot be empty. json:' . $resultStr);
427
+                $this->logger->error('JobDetails cannot be empty. json:'.$resultStr);
428 428
                 break;
429 429
             }
430 430
             $this->logger->debug(json_encode($jobDetails));
431
-            $this->logger->info('Downloading zip file for module ' . $apiModuleName . ' and page ' . $page . '...');
432
-            $jobZipFile = '/tmp/job_' . $dao->getZCRMModule()->getAPIName() . '_' . $jobDetails['data'][0]['id'] . '.zip';
431
+            $this->logger->info('Downloading zip file for module '.$apiModuleName.' and page '.$page.'...');
432
+            $jobZipFile = '/tmp/job_'.$dao->getZCRMModule()->getAPIName().'_'.$jobDetails['data'][0]['id'].'.zip';
433 433
             $jobCsvPath = '/tmp/job_extract';
434
-            $jobCsvFile = '/tmp/job_extract/' . $jobDetails['data'][0]['id'] . '.csv';
434
+            $jobCsvFile = '/tmp/job_extract/'.$jobDetails['data'][0]['id'].'.csv';
435 435
             $canProcessCsv = false;
436 436
 
437
-            $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId . '/result', [
437
+            $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId.'/result', [
438 438
                 'http_errors' => false,
439 439
                 'headers' => [
440
-                    'Authorization' => 'Zoho-oauthtoken ' . $oauthToken
440
+                    'Authorization' => 'Zoho-oauthtoken '.$oauthToken
441 441
                 ],
442 442
                 'sink' => $jobZipFile
443 443
             ]);
444 444
             if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
445
-                $this->logger->info('Extracting ' . $jobZipFile . ' file for module ' . $apiModuleName . ' and page ' . $page . '...');
445
+                $this->logger->info('Extracting '.$jobZipFile.' file for module '.$apiModuleName.' and page '.$page.'...');
446 446
                 $zip = new ZipArchive();
447 447
                 $res = $zip->open($jobZipFile);
448 448
                 if ($res === TRUE) {
449 449
                     $zip->extractTo($jobCsvPath);
450 450
                     $zip->close();
451
-                    $this->logger->info('File extracted in ' . $jobCsvFile);
451
+                    $this->logger->info('File extracted in '.$jobCsvFile);
452 452
                     $canProcessCsv = true;
453 453
                 } else {
454 454
                     switch ($res) {
@@ -480,11 +480,11 @@  discard block
 block discarded – undo
480 480
                             $zipErrorMessage = "Unknow (Code $res)";
481 481
                             break;
482 482
                     }
483
-                    $this->logger->error('Error when extracting zip file: ' . $zipErrorMessage);
483
+                    $this->logger->error('Error when extracting zip file: '.$zipErrorMessage);
484 484
                     break;
485 485
                 }
486 486
             } else {
487
-                $this->logger->error('Cannot download results for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents());
487
+                $this->logger->error('Cannot download results for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents());
488 488
                 break;
489 489
             }
490 490
 
@@ -505,7 +505,7 @@  discard block
 block discarded – undo
505 505
             $this->logger->info('Saving records to db...');
506 506
             $nbRecords = $jobDetails['data'][0]['result']['count'];
507 507
             $whenToLog = ceil($nbRecords / 100);
508
-            $this->logger->info($nbRecords . ' records to save');
508
+            $this->logger->info($nbRecords.' records to save');
509 509
             $nbSaved = 0;
510 510
             $handle = fopen($jobCsvFile, 'r');
511 511
             $fields = [];
@@ -523,30 +523,30 @@  discard block
 block discarded – undo
523 523
                             $recordDataToInsert[$decodedColumnName] = $value === '' ? null : $value;
524 524
                         } else {
525 525
                             if ($columnName === 'Owner' || $columnName === 'Created_By' || $columnName === 'Modified_By') {
526
-                                $recordDataToInsert[$decodedColumnName . '_OwnerID'] = $value === '' ? null : $value;
527
-                                $recordDataToInsert[$decodedColumnName . '_OwnerName'] = $users[$value] ?? null;
528
-                            } else if ($table->hasColumn($decodedColumnName . '_ID')) {
529
-                                $recordDataToInsert[$decodedColumnName . '_ID'] = $value === '' ? null : $value;
526
+                                $recordDataToInsert[$decodedColumnName.'_OwnerID'] = $value === '' ? null : $value;
527
+                                $recordDataToInsert[$decodedColumnName.'_OwnerName'] = $users[$value] ?? null;
528
+                            } else if ($table->hasColumn($decodedColumnName.'_ID')) {
529
+                                $recordDataToInsert[$decodedColumnName.'_ID'] = $value === '' ? null : $value;
530 530
                             }
531 531
                         }
532 532
                     }
533 533
                     $this->connection->insert($tableName, $recordDataToInsert);
534 534
                     ++$nbSaved;
535 535
                     if (($nbSaved % $whenToLog) === 0) {
536
-                        $this->logger->info($nbSaved . '/' . $nbRecords . ' records processed');
536
+                        $this->logger->info($nbSaved.'/'.$nbRecords.' records processed');
537 537
                     }
538 538
                 }
539
-                $this->logger->info($nbSaved . ' records saved for module ' . $apiModuleName . ' and page ' . $page);
539
+                $this->logger->info($nbSaved.' records saved for module '.$apiModuleName.' and page '.$page);
540 540
                 fclose($handle);
541 541
             }
542 542
 
543 543
             // Step 5: Check if there is more results
544 544
             $hasMoreRecords = $jobDetails['data'][0]['result']['more_records'];
545 545
             if (!$hasMoreRecords) {
546
-                $this->logger->info('No more records for the module ' . $apiModuleName);
546
+                $this->logger->info('No more records for the module '.$apiModuleName);
547 547
                 break;
548 548
             }
549
-            $this->logger->info('More records to fetch for the module ' . $apiModuleName);
549
+            $this->logger->info('More records to fetch for the module '.$apiModuleName);
550 550
             ++$page;
551 551
         }
552 552
     }
Please login to merge, or discard this patch.