/**
  * @param array $requestData
  * @param array $unprocessedFiles
  * @param string $inputFile
  * @return array
  * @throws \Exception
  */
 public function import(array $requestData, array $unprocessedFiles, $inputFile)
 {
     $serviceHelpers = $this->buildServiceHelpers($requestData);
     // set default batchsize for adapter
     $requestData['batchSize'] = $serviceHelpers->getProfile()->getType() === 'articlesImages' ? 1 : 50;
     $this->initializeDataIO($serviceHelpers->getDataIO(), $requestData);
     $dataTransformerChain = $this->createDataTransformerChain($serviceHelpers->getProfile(), $serviceHelpers->getFileReader()->hasTreeStructure());
     $sessionState = $serviceHelpers->getSession()->getState();
     $dataWorkflow = new DataWorkflow($serviceHelpers->getDataIO(), $serviceHelpers->getProfile(), $dataTransformerChain, $serviceHelpers->getFileReader());
     try {
         $resultData = $dataWorkflow->import($requestData, $inputFile);
         if (!empty($resultData['unprocessedData'])) {
             $unprocessedData = ['data' => $resultData['unprocessedData'], 'session' => ['prevState' => $sessionState, 'currentState' => $serviceHelpers->getDataIO()->getSessionState()]];
             foreach ($unprocessedData['data'] as $profileName => $value) {
                 $outputFile = $this->uploadPathProvider->getRealPath($this->uploadPathProvider->getFileNameFromPath($inputFile) . '-' . $profileName . '-tmp.csv');
                 $this->afterImport($unprocessedData, $profileName, $outputFile);
                 $unprocessedFiles[$profileName] = $outputFile;
             }
         }
         if ($serviceHelpers->getSession()->getTotalCount() > 0 && $serviceHelpers->getSession()->getTotalCount() == $resultData['position']) {
             // unprocessed files
             $postProcessedData = null;
             if ($unprocessedFiles) {
                 $postProcessedData = $this->processData($unprocessedFiles);
             }
             if ($postProcessedData) {
                 unset($resultData['sessionId']);
                 unset($resultData['adapter']);
                 $resultData = array_merge($resultData, $postProcessedData);
             }
             if ($this->logger->getMessage() === null) {
                 $message = sprintf('%s %s %s', $resultData['position'], SnippetsHelper::getNamespace('backend/swag_import_export/default_profiles')->get($resultData['adapter']), SnippetsHelper::getNamespace('backend/swag_import_export/log')->get('import/success'));
                 $session = $serviceHelpers->getSession()->getEntity();
                 $this->logProcessing('false', $inputFile, $serviceHelpers->getProfile()->getName(), $message, 'true', $session);
             }
         }
         unset($resultData['unprocessedData']);
         $resultData['unprocessedFiles'] = json_encode($unprocessedFiles);
         $resultData['importFile'] = $this->uploadPathProvider->getFileNameFromPath($resultData['importFile']);
         return $resultData;
     } catch (\Exception $e) {
         $session = $serviceHelpers->getSession()->getEntity();
         $this->logProcessing('true', $inputFile, $serviceHelpers->getProfile()->getName(), $e->getMessage(), 'false', $session);
         throw $e;
     }
 }
 /**
  * Executes import action
  *
  * @return array
  */
 public function importAction()
 {
     /** @var UploadPathProvider $uploadPathProvider */
     $uploadPathProvider = Shopware()->Container()->get('swag_import_export.upload_path_provider');
     $postData = array('type' => 'import', 'profileId' => (int) $this->profileEntity->getId(), 'importFile' => $this->filePath, 'sessionId' => $this->sessionId, 'format' => $this->format, 'columnOptions' => null, 'limit' => array(), 'filter' => null, 'max_record_count' => null);
     $inputFile = $postData['importFile'];
     $logger = $this->getLogger();
     // we create the file reader that will read the result file
     /** @var FileIOFactory $fileFactory */
     $fileFactory = $this->Plugin()->getFileIOFactory();
     $fileReader = $fileFactory->createFileReader($postData['format']);
     //load profile
     /** @var Profile $profile */
     $profile = $this->Plugin()->getProfileFactory()->loadProfile($postData);
     //get profile type
     $postData['adapter'] = $profile->getType();
     //setting up the batch size
     $postData['batchSize'] = $profile->getType() === 'articlesImages' ? 1 : 50;
     /** @var DataFactory $dataFactory */
     $dataFactory = $this->Plugin()->getDataFactory();
     $dbAdapter = $dataFactory->createDbAdapter($profile->getType());
     $dataSession = $dataFactory->loadSession($postData);
     //create dataIO
     $dataIO = $dataFactory->createDataIO($dbAdapter, $dataSession, $logger);
     $colOpts = $dataFactory->createColOpts($postData['columnOptions']);
     $limit = $dataFactory->createLimit($postData['limit']);
     $filter = $dataFactory->createFilter($postData['filter']);
     $maxRecordCount = $postData['max_record_count'];
     $type = $postData['type'];
     $format = $postData['format'];
     $dataIO->initialize($colOpts, $limit, $filter, $type, $format, $maxRecordCount);
     $dataIO->setUsername($this->username);
     /** @var DataTransformerChain $dataTransformerChain */
     $dataTransformerChain = $this->Plugin()->getDataTransformerFactory()->createDataTransformerChain($profile, array('isTree' => $fileReader->hasTreeStructure()));
     $sessionState = $dataIO->getSessionState();
     $dataWorkflow = new DataWorkflow($dataIO, $profile, $dataTransformerChain, $fileReader);
     try {
         $post = $dataWorkflow->import($postData, $inputFile);
         if (isset($post['unprocessedData']) && $post['unprocessedData']) {
             $data = array('data' => $post['unprocessedData'], 'session' => array('prevState' => $sessionState, 'currentState' => $dataIO->getSessionState()));
             $pathInfo = pathinfo($inputFile);
             foreach ($data['data'] as $key => $value) {
                 $outputFile = $uploadPathProvider->getRealPath($pathInfo['filename'] . '-' . $key . '-tmp.csv');
                 $post['unprocessed'][] = array('profileName' => $key, 'fileName' => $outputFile);
                 $this->afterImport($data, $key, $outputFile);
             }
         }
         $this->sessionId = $post['sessionId'];
         if ($dataSession->getTotalCount() > 0 && $dataSession->getTotalCount() == $post['position'] && $logger->getMessage() === null) {
             $message = sprintf('%s %s %s', $post['position'], SnippetsHelper::getNamespace('backend/swag_import_export/default_profiles')->get($post['adapter']), SnippetsHelper::getNamespace('backend/swag_import_export/log')->get('import/success'));
             $logger->write($message, 'false', $dataSession->getEntity());
             $logDataStruct = new LogDataStruct(date("Y-m-d H:i:s"), $inputFile, $profile->getName(), $message, 'false');
             $logger->writeToFile($logDataStruct);
         }
         return array('success' => true, 'data' => $post);
     } catch (\Exception $e) {
         $logger->write($e->getMessage(), 'true', $dataSession->getEntity());
         $logDataStruct = new LogDataStruct(date("Y-m-d H:i:s"), $inputFile, $profile->getName(), $e->getMessage(), 'false');
         $logger->writeToFile($logDataStruct);
         throw $e;
     }
 }