Process import

@param \Thelia\ImportExport\Import\AbstractImport  $import     An import
@param \Thelia\Core\Serializer\SerializerInterface $serializer A serializer interface

@return array List of errors
328440
protected function processImport(AbstractImport $import, SerializerInterface $serializer)
    {
        $errors = [];

        $import->setData($serializer->unserialize($import->getFile()->openFile('r')));

        foreach ($import as $idx => $data) {
            $import->checkMandatoryColumns($data);

            $error = $import->importData($data);
            if ($error !== null) {
                $errors[] = $error;
            }
        }

        return $errors;
    }
47753
public function process(Import $import, array $callbacks, $dryRun)
	{
		// create new iterator
		$iterator = new Reader($import->getDatapath(), $this->delimiter);

		// get the import params
		$options = new Parameter($import->get('params'));

		// get the mode
		$mode = $import->get('mode', 'UPDATE');

		// loop through each item
		foreach ($iterator as $index => $record)
		{
			// make sure we have a record
			if ($record === null)
			{
				continue;
			}

			// Make sure we didn't get an empty row
			$data = array_map('trim', (array)$record);
			if (!array_filter($data))
			{
				continue;
			}

			// do we have a post parse callback ?
			$record = $this->map($record, $callbacks['postparse'], $dryRun);

			// convert to resource objects
			$entry = $import->getRecord($record, $options->toArray(), $mode);

			// do we have a post map callback ?
			$entry = $this->map($entry, $callbacks['postmap'], $dryRun);

			// run resource check & store
			$entry->check()->store($dryRun);

			// do we have a post convert callback ?
			$entry = $this->map($entry, $callbacks['postconvert'], $dryRun);

			// add to data array
			array_push($this->data, $entry);

			// mark record processed
			$import->currentRun()->processed(1);
		}

		return $this->data;
	}
1169109
public function add(ClassInterface $import, $alias = NULL) {
    if (empty($alias)) {
      $alias = $import->getName();
    }

    if (empty($alias)) {
      throw new InvalidArgumentException('GClass: '.$import.' must have a valid FQN: '.$import->getFQN());
    }
    
    if (array_key_exists($lowerAlias = mb_strtolower($alias), $this->aliases)) {
      $usedBy = $this->classes[ $this->aliases[$lowerAlias] ];

      if (!$import->equals($usedBy)) {
        throw new LogicException('Alias: '.$alias.' is already used by Class '.$usedBy);
      }
    }
    
    $this->classes[$alias] = $import;
    $this->aliases[$lowerAlias] = $alias;
    return $this;
  }
1036202
public function importAction($id)
    {
        /** @var \Thelia\Handler\Importhandler $importHandler */
        $importHandler = $this->container->get('thelia.import.handler');

        $import = $importHandler->getImport($id);
        if ($import === null) {
            return $this->pageNotFound();
        }

        $form = $this->createForm(AdminForm::IMPORT);

        try {
            $validatedForm = $this->validateForm($form);

            /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
            $file = $validatedForm->get('file_upload')->getData();
            $file = $file->move(
                THELIA_CACHE_DIR . 'import' . DS . (new\DateTime)->format('Ymd'),
                uniqid() . '-' . $file->getClientOriginalName()
            );

            $lang = (new LangQuery)->findPk($validatedForm->get('language')->getData());

            $importEvent = $importHandler->import($import, $file, $lang);

            if (\count($importEvent->getErrors()) > 0) {
                $this->getSession()->getFlashBag()->add(
                    'thelia.import.error',
                    $this->getTranslator()->trans(
                        'Error(s) in import&nbsp;:<br />%errors',
                        [
                            '%errors' => implode('<br />', $importEvent->getErrors())
                        ]
                    )
                );
            }

            $this->getSession()->getFlashBag()->add(
                'thelia.import.success',
                $this->getTranslator()->trans(
                    'Import successfully done, %count row(s) have been changed',
                    [
                        '%count' => $importEvent->getImport()->getImportedRows()
                    ]
                )
            );

            return $this->generateRedirectFromRoute('import.view', [], ['id' => $id]);
        } catch (FormValidationException $e) {
            $form->setErrorMessage($this->createStandardFormValidationErrorMessage($e));
        } catch (\Exception $e) {
            $this->getParserContext()->setGeneralError($e->getMessage());
        }

        $this->getParserContext()
            ->addForm($form)
        ;

        return $this->configureAction($id);
    }
1318289
public function import(?DataImporterConfigurationTransfer $dataImporterConfigurationTransfer = null): DataImporterReportTransfer
    {
        return $this->getFactory()->createCompanyDataImport()->import($dataImporterConfigurationTransfer);
    }
720892
public function import(?DataImporterConfigurationTransfer $dataImporterConfigurationTransfer = null): DataImporterReportTransfer
    {
        return $this->getFactory()->getCompanyBusinessUnitDataImport()->import($dataImporterConfigurationTransfer);
    }
582027
public function getImport($importId, $dispatchException = false)
    {
        $import = (new ImportQuery)->findPk($importId);

        if ($import === null && $dispatchException) {
            throw new \ErrorException(
                Translator::getInstance()->trans(
                    'There is no id "%id" in the imports',
                    [
                        '%id' => $importId
                    ]
                )
            );
        }

        return $import;
    }
1371561
public function importShoppingListItems(?DataImporterConfigurationTransfer $dataImporterConfigurationTransfer = null): DataImporterReportTransfer
    {
        return $this->getFactory()
            ->getShoppingListItemDataImporter()
            ->import($dataImporterConfigurationTransfer);
    }
268572
public function importAction()
    {
        if (!$this->getRequest() instanceof ConsoleRequest) {
            throw new \RuntimeException('You can only use this action from a console!');
        }

        $filter = $this->inputFilterSearch;

        $filter->setData(
            $this->params()
                 ->fromRoute()
        );

        if ($filter->isValid() && $filter->getValue('help') == null) {
            if ($filter->getValue('confirm') == 'yes') {
                $confirm = new Confirm(
                    'Are you sure you want to Import Search Results? [y/n]',
                    'y',
                    'n'
                );

                $result = $confirm->show();
            } else {
                $result = true;
            }

            if ($result) {
                $this->serviceSearch->import();
            }
        } else {
            if ($filter->getValue('help') != null) {
                $this->getEventManager()
                     ->trigger(
                         'Mp3Help',
                         null,
                         ['help' => 'import']
                     );

                exit;
            }
        }
    }
1065669
private function autoDetectAdapter(Import $import)
	{
		// make sure we dont already have an adapter
		if ($this->adapter)
		{
			return;
		}

		// get path to data file
		$dataPath = $import->getDataPath();

		// get the mime type of file
		$file = finfo_open(FILEINFO_MIME_TYPE);
		$mime = finfo_file($file, $dataPath);
		if ($mime == 'text/plain')
		{
			$mime = pathinfo($dataPath, PATHINFO_EXTENSION);
		}

		// anonymous function to see if we can use any
		$respondsTo = function($class) use ($mime)
		{
			return $class::accepts($mime);
		};

		// set the adapter if we found one
		$responded = array_filter($this->adapters, $respondsTo);
		if ($adapter = array_shift($responded))
		{
			$this->setAdapter(new $adapter());
		}

		// do we still not have adapter
		if (!$this->adapter)
		{
			throw new \Exception(\Lang::txt('Content Import: No adapter found to count import data.'));
		}
	}
269224
public function flush(ToModel $import, bool $massInsert = false)
    {
        if ($import instanceof WithValidation) {
            $this->validateRows($import);
        }

        if ($massInsert) {
            $this->massFlush($import);
        } else {
            $this->singleFlush($import);
        }

        $this->rows = [];
    }
1333795
public function add($serializer)
    {
        if (!method_exists($serializer, 'serialize')) {
            throw new \Exception('The serializer '.get_class($serializer).' must implement the method serialize');
        }

        $this->serializers[] = $serializer;
    }
765044
public function importProductList(?DataImporterConfigurationTransfer $dataImporterConfigurationTransfer = null): DataImporterReportTransfer
    {
        return $this->getFactory()
            ->createProductListDataImport()
            ->import($dataImporterConfigurationTransfer);
    }
1012568
public function addImport(Import $import)
    {
        if (isset($this->imports[$import->getClassName()])) {
            return $this;
        }

        $symbol = $import->getAlias() ?: $import->getShortClassName();

        if (isset($this->importedSymbols[$symbol])) {
            throw new RuntimeException(sprintf(
                'The symbol "%s" was imported already.',
                $import->getShortClassName()
            ));
        }

        $this->imports[$import->getClassName()] = $import;
        $this->importedSymbols[$symbol] = true;

        ksort($this->imports);

        return $this;
    }
1156646
protected function _importSuccess(ImportResult $importResult, EntityInterface $entity) : bool
    {
        $table = TableRegistry::get('CsvMigrations.ImportResults');
        Assert::isInstanceOf($table, ImportResultsTable::class);

        $importResult->set('model_id', $entity->get('id'));
        $importResult->set('status', $table::STATUS_SUCCESS);
        $importResult->set('status_message', $table::STATUS_SUCCESS_MESSAGE);

        return (bool)$table->save($importResult);
    }
1294978
public function import(?DataImporterConfigurationTransfer $dataImporterConfigurationTransfer = null): DataImporterReportTransfer
    {
        return $this->getFactory()->createShipmentTableRateImporter()->import($dataImporterConfigurationTransfer);
    }
585186
protected function filterImportFiles(array $imports)
    {
        $config             = $this->config;
        $excludedComponents = $config->getExcludedComponents();
        $includedComponents = $config->getIncludedComponents();

        if (!empty($excludedComponents) && !empty($includedComponents)) {
            throw new Exception\RuntimeException(
                'You may not set both excluded and included components.'
            );
        }

        if (!empty($excludedComponents)) {
            $imports = $this->removeImportFiles($imports, $excludedComponents);
        } elseif (!empty($includedComponents)) {
            $imports = $this->addImportFiles($imports, $includedComponents);
        }

        $imports = $this->setupFontAwesome($imports);

        array_walk($imports, function (&$val) {
            $val = "@import \"$val\";";
        });

        return implode(PHP_EOL, $imports);
    }