objResponse = $response; $this->objConfig = $config; $this->objPillPut = $poisonPillPut; $this->objResourceConnection = $objResourceConnection; } private function getToken() { $strToken = $this->objConfig->getImportToken(); if (strlen($strToken) < 10) { throw new \Exception('Invalid Import Token. The token must be at least 10 characters long.'); } return $strToken; } private function parseInput() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $config = $objectManager->get('saws\sawsconnector\Helper\Config'); $input = trim(file_get_contents('php://input')); if (!$input) { throw new \Exception('invalid data'); } $arrData = json_decode($input, true); if (is_array($arrData)) { $this->arrData = array(); } $strMethod = $arrData['method'] ?? ''; $strToken = $arrData['token']; $arrData = $arrData['data'] ?? '[]'; if (is_array($arrData)) { $strData = json_encode($arrData); } else { $strData = $arrData; $arrData = json_decode($strData, true); if (!is_array($arrData)) { $arrData = []; } } $strHash = md5(sha1($strMethod) . strlen($strData) . '--' . $this->getToken()); $strMethod = 'execute' . ucfirst(strtolower($strMethod)); if (($strHash != $strToken) || !is_callable(array($this, $strMethod))) { throw new \Exception('invalid method'); } $this->objData = $objectManager->create('saws\sawsconnector\Model\Import\Data\Data', array('data' => $arrData)); return $strMethod; } private function getData($strKey = null) { if (!$this->objData) { return false; } return $this->objData->getData($strKey); } private function createProcessID() { $strTime = time(); return substr(md5(microtime() . rand(0, 999999)), 0, strlen($strTime) * (-1)) . $strTime; } public function executeOpenprocess() { $strProcessID = $this->createProcessID(); return array('process' => array('id' => $strProcessID, 'state' => 'listening')); } public function executeFinalizeprocess() { $strProcessID = $this->getData('processid'); $this->objPillPut->put(); return array('process' => array('id' => $strProcessID, 'state' => 'finished')); } public function executeAbortprocess() { try { $arrReturn = $this->executeFinalizeprocess(); $arrReturn['state'] = 'aborted'; return $arrReturn; } catch (\Exception $e) { $this->objPillPut->put(); throw new \Exception('abort failed'); } } public function executeGetmagentoversion() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface'); return $productMetadata->getVersion(); //will return the magento version } public function executeGetconnectorversion() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $moduleInfo = $objectManager->get('Magento\Framework\Module\ModuleList')->getOne('saws_sawsconnector'); // SR_Learning is module name return $moduleInfo['setup_version']; } public function executeData() { ignore_user_abort(true); error_reporting(E_ERROR); ini_set('display_errors', '0'); set_time_limit(7200); ini_set('memory_limit', "1024M"); try { error_reporting(E_ERROR); $strMethod = $this->parseInput(); return json_encode(call_user_func(array($this, $strMethod))); } catch (\Exception $e) { //$this->objResponse->addLogEntry($e->getMessage(), 'global', 'execData', 0, true); $this->objPillPut->put(); return $e->getMessage(); } } public function executeQueueData($message) { if (!is_array($message)) { $message = @json_decode($message, true) ?: []; } $this->objResponse->resetEntity(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->objData = $objectManager->create('saws\sawsconnector\Model\Import\Data\Data', array('data' => $message['data'] ?? [])); $strMethod = 'execute' . ucfirst(strtolower($message['method'] ?? '')); try { ini_set('display_errors', '0'); ini_set('memory_limit', "1024M"); error_reporting(E_ERROR); $arrMethodResponse = call_user_func(array($this, $strMethod)); if (!is_array($arrMethodResponse)) { $arrMethodResponse = []; } } catch (\Exception $e) { $this->objPillPut->put(); $this->objResponse->addLogEntry($e->getMessage(), 'global', 'execData', 0, true); } $arrResponse = array_replace($arrMethodResponse, $this->objResponse->getEntityMap()); $arrResponse['processid'] = $this->getData('processid'); return $arrResponse; } public function executeGetversion() { $response = array( 'version' => $this->executeGetconnectorversion(), 'magentoversion' => $this->executeGetmagentoversion(), 'module' => true, ); return $response; } public function executeImport() { $objResponse = $this->objResponse; $arrImportData = array_filter($this->getData('importdata')); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $objSCImportFactory = $objectManager->get('saws\sawsconnector\Model\Import\SawsConnector\Factory'); $arrImportOptions = isset($arrImportData['options']) ? $arrImportData['options'] : array(); unset($arrImportData['options']); $arrImportOrder = array_intersect($this->arrImportOrder, array_keys($arrImportData)); foreach ($arrImportOrder as $type) { try { $options = isset($arrImportOptions[$type]) ? $arrImportOptions[$type] : array(); $objSCImport = $objSCImportFactory->create($type); $objSCImport->processImport($arrImportData[$type], $options); } catch (\InvalidArgumentException $e) { $this->objResponse->addLogEntry($e->getMessage(), 'global', $type, 0, true); } catch (\Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException $e) { $this->objResponse->addLogEntry($e->getMessage(), 'global', $type, 0, true, $e->getUrls()); } catch (\Exception $e) { $this->objResponse->addLogEntry($e->getMessage(), 'global', $type, 0, true); } } $this->cleanImporttable(); return $objResponse->getEntityMap(); } function executeSyncronizemagento() { $arrResponse['syncronizemagento'] = array(); $connection = $this->objResourceConnection; $arrTables = $connection->getConnection()->fetchCol('show tables'); if (in_array($connection->getTableName('cms_block'), $arrTables)) { $objQuery = 'SELECT DISTINCT identifier as code, title as name FROM ' . $connection->getTableName('cms_block'); $arrRows = $connection->getConnection()->fetchAll($objQuery); $arrResponse['syncronizemagento']['Cmsblocks'] = $arrRows; } $objQuery = 'SELECT code, name FROM ' . $connection->getTableName('store'); $arrRows = $connection->getConnection()->fetchAll($objQuery); $arrResponse['syncronizemagento']['Storeviews'] = $arrRows; $objQuery = 'SELECT customer_group_id as code, customer_group_code as name FROM ' . $connection->getTableName('customer_group'); $arrRows = $connection->getConnection()->fetchAll($objQuery); $arrResponse['syncronizemagento']['Customergroups'] = $arrRows; $objQuery = 'SELECT code, name FROM ' . $connection->getTableName('store_website'); $arrRows = $connection->getConnection()->fetchAll($objQuery); $arrResponse['syncronizemagento']['Websiteshidden'] = $arrRows; if (in_array($connection->getTableName('seo_block'), $arrTables)) { $objQuery = 'SELECT DISTINCT identifier as code, title as name FROM ' . $connection->getTableName('seo_block'); $arrRows = $connection->getConnection()->fetchAll($objQuery); $arrResponse['syncronizemagento']['Seoblocks'] = $arrRows; } $arrResponse['responsecode'] = 200; $arrResponse['importtable'] = $this->cleanImporttable() ? 'ready' : 'error'; return $arrResponse; } private function cleanImporttable() { try { $strTableName = $this->objResourceConnection->getTableName('importexport_importdata'); $this->objResourceConnection->getConnection()->delete($strTableName); return true; } catch (\Exception $e) { return false; } } public function executeReindex() { $objResponse = $this->objResponse; $intStartTime = $objResponse->microtimeFloat(); $objResponse->addLogEntry('Start reindex process.', 'global', 'reindex', 0); $registry = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Indexer\Model\Processor'); $registry->reindexAllInvalid(); $objResponse->addLogEntry('Finish reindex process.', 'global', 'reindex', $objResponse->getDuration($intStartTime)); return $objResponse->getEntityMap(); } public function executeUpdateversion() { $objResponse = $this->objResponse; $objResponse->addLogEntry('not supported anymore', 'global', 'updateversion', true); return $objResponse->getEntityMap(); } }