@@ -72,11 +72,11 @@ discard block |
||
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) { |
@@ -157,10 +157,10 @@ discard block |
||
157 | 157 | $lastActivityTime = new \DateTime($modifiedSince); |
158 | 158 | } else { |
159 | 159 | if ($tableDetail->hasColumn('modifiedTime')) { |
160 | - $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM ' . $tableName); |
|
160 | + $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(modifiedTime) FROM '.$tableName); |
|
161 | 161 | } |
162 | 162 | if (!$lastActivityTime && $tableDetail->hasColumn('createdTime')) { |
163 | - $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM ' . $tableName); |
|
163 | + $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(createdTime) FROM '.$tableName); |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | if ($lastActivityTime !== null) { |
@@ -179,21 +179,21 @@ discard block |
||
179 | 179 | $this->logger->notice(sprintf('Fetching the records to insert/update for module %s...', $dao->getPluralModuleName())); |
180 | 180 | $records = $dao->getRecords(null, null, null, $lastActivityTime); |
181 | 181 | $totalRecords = count($records); |
182 | - $this->logger->debug($totalRecords . ' records fetched.'); |
|
182 | + $this->logger->debug($totalRecords.' records fetched.'); |
|
183 | 183 | $this->logger->notice(sprintf('Fetching the records to delete for module %s...', $dao->getPluralModuleName())); |
184 | 184 | $deletedRecords = $dao->getDeletedRecordIds($lastActivityTime); |
185 | 185 | $totalRecordsDeleted = count($deletedRecords); |
186 | - $this->logger->debug($totalRecordsDeleted . ' records fetched.'); |
|
186 | + $this->logger->debug($totalRecordsDeleted.' records fetched.'); |
|
187 | 187 | } else { |
188 | 188 | $this->logger->info(sprintf('Full copy started for module %s', $dao->getPluralModuleName())); |
189 | 189 | $this->logger->notice(sprintf('Fetching the records to insert/update for module ...%s', $dao->getPluralModuleName())); |
190 | 190 | $records = $dao->getRecords(); |
191 | 191 | $totalRecords = count($records); |
192 | - $this->logger->debug($totalRecords . ' records fetched.'); |
|
192 | + $this->logger->debug($totalRecords.' records fetched.'); |
|
193 | 193 | $deletedRecords = []; |
194 | 194 | } |
195 | 195 | } catch (ZCRMException $exception) { |
196 | - $this->logger->error('Error when getting records for module ' . $dao->getPluralModuleName() . ': ' . $exception->getMessage(), [ |
|
196 | + $this->logger->error('Error when getting records for module '.$dao->getPluralModuleName().': '.$exception->getMessage(), [ |
|
197 | 197 | 'exception' => $exception |
198 | 198 | ]); |
199 | 199 | if ($throwErrors) { |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | |
206 | 206 | $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
207 | 207 | |
208 | - $select = $this->connection->prepare('SELECT * FROM ' . $tableName . ' WHERE id = :id'); |
|
208 | + $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id'); |
|
209 | 209 | |
210 | 210 | $this->connection->beginTransaction(); |
211 | 211 | |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | } |
276 | 276 | |
277 | 277 | $this->logger->info(sprintf('Deleting %d records from table %s...', $totalRecordsDeleted, $tableName)); |
278 | - $sqlStatementUid = 'select uid from ' . $this->connection->quoteIdentifier($tableName) . ' where id = :id'; |
|
278 | + $sqlStatementUid = 'select uid from '.$this->connection->quoteIdentifier($tableName).' where id = :id'; |
|
279 | 279 | $processedRecords = 0; |
280 | 280 | $logOffset = $totalRecordsDeleted >= 500 ? 100 : 50; |
281 | 281 | foreach ($deletedRecords as $deletedRecord) { |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
316 | 316 | $apiModuleName = $dao->getPluralModuleName(); |
317 | 317 | |
318 | - $this->logger->notice('Starting bulk fetch for module ' . $apiModuleName . '...'); |
|
318 | + $this->logger->notice('Starting bulk fetch for module '.$apiModuleName.'...'); |
|
319 | 319 | |
320 | 320 | $zohoClient = new ZohoClient([ |
321 | 321 | 'client_id' => ZOHO_CRM_CLIENT_ID, |
@@ -334,11 +334,11 @@ discard block |
||
334 | 334 | $oauthToken = $zohoClient->getZohoOAuthClient()->getAccessToken(ZOHO_CRM_CLIENT_CURRENT_USER_EMAIL); |
335 | 335 | |
336 | 336 | // Step 1: Create a bulk read job |
337 | - $this->logger->info('Creating read job for module ' . $apiModuleName . ' and page ' . $page . '...'); |
|
338 | - $response = $client->request('POST', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read', [ |
|
337 | + $this->logger->info('Creating read job for module '.$apiModuleName.' and page '.$page.'...'); |
|
338 | + $response = $client->request('POST', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read', [ |
|
339 | 339 | 'http_errors' => false, |
340 | 340 | 'headers' => [ |
341 | - 'Authorization' => 'Zoho-oauthtoken ' . $oauthToken |
|
341 | + 'Authorization' => 'Zoho-oauthtoken '.$oauthToken |
|
342 | 342 | ], |
343 | 343 | 'json' => [ |
344 | 344 | 'query' => [ |
@@ -356,23 +356,23 @@ discard block |
||
356 | 356 | |
357 | 357 | // We don't care about the job status right now, it will be checked later |
358 | 358 | } else { |
359 | - $this->logger->error('Cannot create bulk read query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents()); |
|
359 | + $this->logger->error('Cannot create bulk read query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents()); |
|
360 | 360 | break; |
361 | 361 | } |
362 | 362 | |
363 | 363 | if ($jobId === null) { |
364 | - $this->logger->error('JobID cannot be null. json:' . $resultStr); |
|
364 | + $this->logger->error('JobID cannot be null. json:'.$resultStr); |
|
365 | 365 | break; |
366 | 366 | } |
367 | 367 | |
368 | 368 | // Step 2: Check job status |
369 | 369 | $jobDetails = null; |
370 | 370 | while (true) { |
371 | - $this->logger->info('Checking job ' . $jobId . ' status for module ' . $apiModuleName . ' and page ' . $page . '...'); |
|
372 | - $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId, [ |
|
371 | + $this->logger->info('Checking job '.$jobId.' status for module '.$apiModuleName.' and page '.$page.'...'); |
|
372 | + $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId, [ |
|
373 | 373 | 'http_errors' => false, |
374 | 374 | 'headers' => [ |
375 | - 'Authorization' => 'Zoho-oauthtoken ' . $oauthToken |
|
375 | + 'Authorization' => 'Zoho-oauthtoken '.$oauthToken |
|
376 | 376 | ] |
377 | 377 | ]); |
378 | 378 | if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
@@ -390,15 +390,15 @@ discard block |
||
390 | 390 | $jobDetails = $json; |
391 | 391 | break; |
392 | 392 | } else { |
393 | - $this->logger->info('Unsupported job status: ' . $resultStr); |
|
393 | + $this->logger->info('Unsupported job status: '.$resultStr); |
|
394 | 394 | break; |
395 | 395 | } |
396 | 396 | } else { |
397 | - $this->logger->error('Unsupported response: ' . $resultStr); |
|
397 | + $this->logger->error('Unsupported response: '.$resultStr); |
|
398 | 398 | break; |
399 | 399 | } |
400 | 400 | } else { |
401 | - $this->logger->error('Cannot get bulk job status query for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents()); |
|
401 | + $this->logger->error('Cannot get bulk job status query for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents()); |
|
402 | 402 | break; |
403 | 403 | } |
404 | 404 | sleep(15); |
@@ -406,31 +406,31 @@ discard block |
||
406 | 406 | |
407 | 407 | // Step 3: Download the result |
408 | 408 | if ($jobDetails === null) { |
409 | - $this->logger->error('JobDetails cannot be empty. json:' . $resultStr); |
|
409 | + $this->logger->error('JobDetails cannot be empty. json:'.$resultStr); |
|
410 | 410 | break; |
411 | 411 | } |
412 | 412 | $this->logger->debug(json_encode($jobDetails)); |
413 | - $this->logger->info('Downloading zip file for module ' . $apiModuleName . ' and page ' . $page . '...'); |
|
414 | - $jobZipFile = '/tmp/job_' . $dao->getZCRMModule()->getAPIName() . '_' . $jobDetails['data'][0]['id'] . '.zip'; |
|
413 | + $this->logger->info('Downloading zip file for module '.$apiModuleName.' and page '.$page.'...'); |
|
414 | + $jobZipFile = '/tmp/job_'.$dao->getZCRMModule()->getAPIName().'_'.$jobDetails['data'][0]['id'].'.zip'; |
|
415 | 415 | $jobCsvPath = '/tmp/job_extract'; |
416 | - $jobCsvFile = '/tmp/job_extract/' . $jobDetails['data'][0]['id'] . '.csv'; |
|
416 | + $jobCsvFile = '/tmp/job_extract/'.$jobDetails['data'][0]['id'].'.csv'; |
|
417 | 417 | $canProcessCsv = false; |
418 | 418 | |
419 | - $response = $client->request('GET', 'https://' . (ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www') . '.zohoapis.com/crm/bulk/v2/read/' . $jobId . '/result', [ |
|
419 | + $response = $client->request('GET', 'https://'.(ZOHO_CRM_SANDBOX === 'true' ? 'sandbox' : 'www').'.zohoapis.com/crm/bulk/v2/read/'.$jobId.'/result', [ |
|
420 | 420 | 'http_errors' => false, |
421 | 421 | 'headers' => [ |
422 | - 'Authorization' => 'Zoho-oauthtoken ' . $oauthToken |
|
422 | + 'Authorization' => 'Zoho-oauthtoken '.$oauthToken |
|
423 | 423 | ], |
424 | 424 | 'sink' => $jobZipFile |
425 | 425 | ]); |
426 | 426 | if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
427 | - $this->logger->info('Extracting ' . $jobZipFile . ' file for module ' . $apiModuleName . ' and page ' . $page . '...'); |
|
427 | + $this->logger->info('Extracting '.$jobZipFile.' file for module '.$apiModuleName.' and page '.$page.'...'); |
|
428 | 428 | $zip = new ZipArchive(); |
429 | 429 | $res = $zip->open($jobZipFile); |
430 | 430 | if ($res === TRUE) { |
431 | 431 | $zip->extractTo($jobCsvPath); |
432 | 432 | $zip->close(); |
433 | - $this->logger->info('File extracted in ' . $jobCsvFile); |
|
433 | + $this->logger->info('File extracted in '.$jobCsvFile); |
|
434 | 434 | $canProcessCsv = true; |
435 | 435 | } else { |
436 | 436 | switch ($res) { |
@@ -462,11 +462,11 @@ discard block |
||
462 | 462 | $zipErrorMessage = "Unknow (Code $res)"; |
463 | 463 | break; |
464 | 464 | } |
465 | - $this->logger->error('Error when extracting zip file: ' . $zipErrorMessage); |
|
465 | + $this->logger->error('Error when extracting zip file: '.$zipErrorMessage); |
|
466 | 466 | break; |
467 | 467 | } |
468 | 468 | } else { |
469 | - $this->logger->error('Cannot download results for module ' . $apiModuleName . ': status: ' . $response->getStatusCode() . '. Status: ' . $response->getBody()->getContents()); |
|
469 | + $this->logger->error('Cannot download results for module '.$apiModuleName.': status: '.$response->getStatusCode().'. Status: '.$response->getBody()->getContents()); |
|
470 | 470 | break; |
471 | 471 | } |
472 | 472 | |
@@ -487,7 +487,7 @@ discard block |
||
487 | 487 | $this->logger->info('Saving records to db...'); |
488 | 488 | $nbRecords = $jobDetails['data'][0]['result']['count']; |
489 | 489 | $whenToLog = ceil($nbRecords / 100); |
490 | - $this->logger->info($nbRecords . ' records to save'); |
|
490 | + $this->logger->info($nbRecords.' records to save'); |
|
491 | 491 | $nbSaved = 0; |
492 | 492 | $handle = fopen($jobCsvFile, 'r'); |
493 | 493 | $fields = []; |
@@ -505,30 +505,30 @@ discard block |
||
505 | 505 | $recordDataToInsert[$decodedColumnName] = $value === '' ? null : $value; |
506 | 506 | } else { |
507 | 507 | if ($columnName === 'Owner' || $columnName === 'Created_By' || $columnName === 'Modified_By') { |
508 | - $recordDataToInsert[$decodedColumnName . '_OwnerID'] = $value === '' ? null : $value; |
|
509 | - $recordDataToInsert[$decodedColumnName . '_OwnerName'] = $users[$value] ?? null; |
|
510 | - } else if ($table->hasColumn($decodedColumnName . '_ID')) { |
|
511 | - $recordDataToInsert[$decodedColumnName . '_ID'] = $value === '' ? null : $value; |
|
508 | + $recordDataToInsert[$decodedColumnName.'_OwnerID'] = $value === '' ? null : $value; |
|
509 | + $recordDataToInsert[$decodedColumnName.'_OwnerName'] = $users[$value] ?? null; |
|
510 | + } else if ($table->hasColumn($decodedColumnName.'_ID')) { |
|
511 | + $recordDataToInsert[$decodedColumnName.'_ID'] = $value === '' ? null : $value; |
|
512 | 512 | } |
513 | 513 | } |
514 | 514 | } |
515 | 515 | $this->connection->insert($tableName, $recordDataToInsert); |
516 | 516 | ++$nbSaved; |
517 | 517 | if (($nbSaved % $whenToLog) === 0) { |
518 | - $this->logger->info($nbSaved . '/' . $nbRecords . ' records processed'); |
|
518 | + $this->logger->info($nbSaved.'/'.$nbRecords.' records processed'); |
|
519 | 519 | } |
520 | 520 | } |
521 | - $this->logger->info($nbSaved . ' records saved for module ' . $apiModuleName . ' and page ' . $page); |
|
521 | + $this->logger->info($nbSaved.' records saved for module '.$apiModuleName.' and page '.$page); |
|
522 | 522 | fclose($handle); |
523 | 523 | } |
524 | 524 | |
525 | 525 | // Step 5: Check if there is more results |
526 | 526 | $hasMoreRecords = $jobDetails['data'][0]['result']['more_records']; |
527 | 527 | if (!$hasMoreRecords) { |
528 | - $this->logger->info('No more records for the module ' . $apiModuleName); |
|
528 | + $this->logger->info('No more records for the module '.$apiModuleName); |
|
529 | 529 | break; |
530 | 530 | } |
531 | - $this->logger->info('More records to fetch for the module ' . $apiModuleName); |
|
531 | + $this->logger->info('More records to fetch for the module '.$apiModuleName); |
|
532 | 532 | ++$page; |
533 | 533 | } |
534 | 534 | } |