. */ namespace SP\Services\Import; use SP\Services\Service; defined('APP_ROOT') || die(); /** * Esta clase es la encargada de importar cuentas. */ final class ImportService extends Service { const ALLOWED_EXTS = ['CSV', 'XML']; /** * @var ImportParams */ protected $importParams; /** * @var FileImport */ protected $fileImport; /** * Iniciar la importación de cuentas. * * @param ImportParams $importParams * @param FileImport $fileImport * * @return int Returns the total number of imported items * @throws \Exception * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ public function doImport(ImportParams $importParams, FileImport $fileImport) { $this->importParams = $importParams; $this->fileImport = $fileImport; return $this->transactionAware(function () { return $this->selectImportType() ->doImport() ->getCounter(); }); } /** * @return ImportInterface * @throws ImportException * @throws \SP\Storage\File\FileException */ protected function selectImportType() { $fileType = $this->fileImport->getFileType(); switch ($fileType) { case 'text/plain': return new CsvImport($this->dic, $this->fileImport, $this->importParams); case 'text/xml': case 'application/xml': return new XmlImport($this->dic, new XmlFileImport($this->fileImport), $this->importParams); } throw new ImportException( sprintf(__('Tipo mime no soportado ("%s")'), $fileType), ImportException::ERROR, __u('Compruebe el formato del archivo') ); } /** * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ protected function initialize() { set_time_limit(0); } }