. */ namespace SP\Services\Import; use Exception; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use SP\Core\Exceptions\SPException; use SP\Services\Service; use SP\Storage\File\FileException; defined('APP_ROOT') || die(); /** * Esta clase es la encargada de importar cuentas. */ final class ImportService extends Service { public const ALLOWED_MIME = [ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel', 'text/plain', 'text/csv', 'text/x-csv', 'application/xml', 'text/xml' ]; protected ?ImportParams $importParams = null; protected ?FileImport $fileImport = null; /** * Iniciar la importación de cuentas. * * @return int Returns the total number of imported items * @throws Exception * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function doImport( ImportParams $importParams, FileImport $fileImport ): int { $this->importParams = $importParams; $this->fileImport = $fileImport; return $this->transactionAware( function () { return $this->selectImportType() ->doImport() ->getCounter(); } ); } /** * @throws ImportException * @throws FileException */ protected function selectImportType(): ImportInterface { $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(__('Mime type not supported ("%s")'), $fileType), SPException::ERROR, __u('Please, check the file format') ); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function initialize(): void { set_time_limit(0); } }