. */ namespace SP\Services\Import; use SP\Services\Service; use SP\Storage\Database; use SP\Storage\DbWrapper; defined('APP_ROOT') || die(); /** * Esta clase es la encargada de importar cuentas. */ class ImportService extends Service { /** * @var ImportParams */ protected $importParams; /** * @var FileImport */ protected $fileImport; /** * Iniciar la importación de cuentas. * * @param ImportParams $importParams * @param FileImport $fileImport * @return int * @throws \Exception * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ public function doImport(ImportParams $importParams, FileImport $fileImport) { $this->importParams = $importParams; $this->fileImport = $fileImport; // $LogMessage->setAction(__('Importar Cuentas', false)); $import = $this->selectImportType(); $db = $this->dic->get(Database::class); try { if (!DbWrapper::beginTransaction($db)) { throw new ImportException(__u('No es posible iniciar una transacción')); } $counter = $import->doImport()->getCounter(); if (!DbWrapper::endTransaction($db)) { throw new ImportException(__u('No es posible finalizar una transacción')); } return $counter; } catch (\Exception $e) { if (DbWrapper::rollbackTransaction($db)) { debugLog('Rollback'); } throw $e; } // $LogMessage->addDescription(__('Importación finalizada', false)); // $LogMessage->addDescription(__('Revise el registro de eventos para más detalles', false)); } /** * @return ImportInterface * @throws ImportException * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ protected function selectImportType() { switch ($this->fileImport->getFileType()) { case 'text/csv': case 'application/vnd.ms-excel': return new CsvImport($this->fileImport, $this->importParams); break; case 'text/xml': return new XmlImport(new XmlFileImport($this->fileImport), $this->importParams); break; } throw new ImportException( sprintf(__('Tipo mime no soportado ("%s")'), $this->fileImport->getFileType()), ImportException::ERROR, __u('Compruebe el formato del archivo') ); } /** * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ protected function initialize() { set_time_limit(0); } }