/** * Creates a data transformer chain by consuming data found a profile. * The $dataUserOptions is an object that will return info for the output file structure - tree or flat. * * @param \Shopware\Components\SwagImportExport\Profile\Profile $profile * @param array $dataUserOptions * @return \Shopware\Components\SwagImportExport\Transformers\DataTransformerChain */ public function createDataTransformerChain($profile, $dataUserOptions) { // this can be put in a separate hookable function $dataTransformerChain = new DataTransformerChain(); // for every config we create a transformer and add it to the chain $names = $profile->getConfigNames(); foreach ($names as $name) { $config = $profile->getConfig($name); $transformer = $this->createDataTransformer($name, $config); $dataTransformerChain->add($transformer); } // a little hack: if we are in csv, we flatten the tree by adding a flattener at the end if (!$dataUserOptions['isTree']) { $transformer = $this->createDataTransformer('flatten', $profile->getConfig('tree')); $dataTransformerChain->add($transformer); } return $dataTransformerChain; }
/** * @param $postData * @param $inputFile * @return mixed * @throws \Exception */ public function import($postData, $inputFile) { $tree = json_decode($this->profile->getConfig("tree"), true); if ($postData['format'] === 'xml') { $this->fileIO->setTree($tree); } if ($this->dataIO->getSessionState() == 'new') { $totalCount = $this->fileIO->getTotalCount($inputFile); $this->dataIO->setFileName($postData['importFile']); $this->dataIO->setFileSize(filesize($inputFile)); $this->dataIO->getDataSession()->setTotalCount($totalCount); $this->dataIO->startSession($this->profile); } else { // session has already loaded ids and some position, so we simply activate it $this->dataIO->resumeSession(); } $this->dataIO->usernameSession(); if ($this->dataIO->getSessionState() == 'active') { //get current session position $batchSize = (int) $postData['batchSize']; $position = $this->dataIO->getSessionPosition(); $records = $this->fileIO->readRecords($inputFile, $position, $batchSize); $data = $this->transformerChain->transformBackward($records); $defaultValues = $this->profile->getDefaultValues($tree); //inserts/update data into the database $this->dataIO->write($data, $defaultValues); //writes into database log table $profileName = $this->profile->getName(); $this->dataIO->writeLog($inputFile, $profileName); $this->dataIO->progressSession($batchSize); //gets unprocessed data from the adapter $postData['unprocessedData'] = $this->dataIO->getUnprocessedData(); } if ($this->dataIO->getSessionState() == 'finished') { $this->dataIO->closeSession(); } $postData['position'] = $this->dataIO->getSessionPosition(); if (!$postData['sessionId']) { $postData['sessionId'] = $this->dataIO->getDataSession()->getId(); } return $postData; }
/** * @param Profile $profile * @param $format * @return FileReader */ private function createFileReader(Profile $profile, $format) { $fileReader = $this->fileIOFactory->createFileReader($format); if ($format === 'xml') { $tree = json_decode($profile->getConfig("tree"), true); $fileReader->setTree($tree); } return $fileReader; }