. */ namespace SP\Services\Import; use Exception; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; 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 { const ALLOWED_MIME = [ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel', 'text/plain', 'text/csv', 'text/x-csv', 'application/xml', 'text/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 ContainerExceptionInterface * @throws 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 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(__('Mime type not supported ("%s")'), $fileType), ImportException::ERROR, __u('Please, check the file format') ); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function initialize() { set_time_limit(0); } }