[]]; $rows = explode("\n", $result); if (count($rows) < 2) { return null; } $headers = array_shift($rows); $headers = explode(',', trim($headers, "\r\n ")); $headersCount = count($headers); foreach ($rows as $oneResult) { $oneResult = explode(',', trim($oneResult, "\r\n ")); if (count($oneResult) !== $headersCount) continue; $formattedResults['results'][] = array_combine($headers, $oneResult); } if (empty($formattedResults['results'])) { return null; } return $formattedResults; } return null; } /** * @param int $testId * @param string|array $result Either a CSV full content or a decoded json * * @return void */ public function handleBatchResults($testId, $result) { $result = $this->decodeResult($result); $testClass = new TestClass(); if (empty($result['results'])) { DebugService::logMessage('callback_controller.log', 'The results from the API are empty for the batch ID '.$testId); $testClass->setBatchAsFailed($testId); $this->sendNextBatch(); return; } $apiService = new ApiService(); $results = $result['results']; $allTests = []; foreach ($results as $oneResult) { $allTests[$oneResult[ApiService::API_TEST_KEY_EMAIL]] = get_object_vars($apiService->prepareTest($oneResult, true)); $allTests[$oneResult[ApiService::API_TEST_KEY_EMAIL]]['block_reason'] = 'NULL'; } // We get the selected table to check $tablesSelected = $this->config->get('tables_selected'); if (empty($tablesSelected)) { DebugService::logMessage('callback_controller.log', 'No table selected in the configuration'); } else { $tablesSelected = explode(',', $tablesSelected); $actionSelected = $this->config->get('action_selected'); if ($actionSelected !== 'do_nothing') { if (in_array('acymailing', $tablesSelected)) { $acymailingUserClass = new AcymailingUserClass(); $acymailingUserClass->handleBatchCallback($results, $allTests); } if (in_array('acymailing5', $tablesSelected)) { $acymailing5UserClass = new Acymailing5UserClass(); $acymailing5UserClass->handleBatchCallback($results, $allTests); } if (in_array('cms', $tablesSelected)) { $cmsUserClass = new CmsUserClass(); $cmsUserClass->handleBatchCallback($results, $allTests); } } } foreach ($allTests as $email => $databaseValues) { $allTests[$email] = implode(',', array_values($databaseValues)); } $emailsToInsert = array_keys($allTests); $i = 0; $tests = []; while (isset($emailsToInsert[$i])) { $tests[] = $allTests[$emailsToInsert[$i]]; $i++; if ($i % 100 == 0) { $this->insertTestResult($tests); $tests = []; } } // Handle remaining tests if (!empty($tests)) { $this->insertTestResult($tests); } $this->sendNextBatch(); } private function insertTestResult($tests) { $insertQuery = 'INSERT INTO #__acyc_test (`email`, `date`, `raw_result`, `test_result`, `disposable`, `free`, `accept_all`, `role_email`, `current_step`, `block_reason`) VALUES '; $insertQuery .= '('.implode('),(', array_values($tests)).')'; $insertQuery .= ' ON DUPLICATE KEY UPDATE email=VALUES(email), `date`=VALUES(`date`), raw_result=VALUES(raw_result), test_result=VALUES(test_result), disposable=VALUES(disposable), free=VALUES(free), accept_all=VALUES(accept_all), role_email=VALUES(role_email), current_step=VALUES(current_step), block_reason=VALUES(block_reason)'; try { // We execute the query to store the email tested Database::query($insertQuery); } catch (\Exception $exception) { DebugService::logMessage('callback_controller.log', 'Error on updating the table test - '.$exception->getMessage()); } } private function sendNextBatch() { $cronService = new CronService(); $cronService->sendNextBatchToAPI(); } }