. */ namespace SP\Services\Import; use SP\Core\Exceptions\SPException; use SP\Http\Request; use SP\Storage\File\FileException; use SP\Storage\File\FileHandler; use SP\Util\Util; defined('APP_ROOT') || die(); /** * Clase FileImport encargada el leer archivos para su importación * * @package SP */ final class FileImport { /** * @var FileHandler */ private $fileHandler; /** * FileImport constructor. * * @param FileHandler $fileHandler Datos del archivo a importar */ private function __construct(FileHandler $fileHandler) { $this->fileHandler = $fileHandler; } /** * @param string $filename * @param Request $request * * @return FileImport * @throws FileException * @throws SPException */ public static function fromRequest(string $filename, Request $request) { return new self(self::checkFile($request->getFile($filename))); } /** * Leer los datos del archivo. * * @param array $file con los datos del archivo * * @return FileHandler * @throws FileException * @throws SPException */ private static function checkFile($file): FileHandler { if (!is_array($file)) { throw new FileException( __u('File successfully uploaded'), FileException::ERROR, __u('Please check the web server user permissions') ); } try { $fileHandler = new FileHandler($file['tmp_name']); $fileHandler->checkFileExists(); if (!in_array($fileHandler->getFileType(), ImportService::ALLOWED_MIME)) { throw new ImportException( __u('File type not allowed'), ImportException::ERROR, sprintf(__('MIME type: %s'), $fileHandler->getFileType()) ); } return $fileHandler; } catch (FileException $e) { logger('Max. upload size: ' . Util::getMaxUpload()); throw new FileException( __u('Internal error while reading the file'), FileException::ERROR, __u('Please, check PHP configuration for upload files'), $e->getCode(), $e ); } } /** * @param string $path * * @return FileImport */ public static function fromFilesystem(string $path) { return new self(new FileHandler($path)); } /** * @return string */ public function getFilePath() { return $this->fileHandler->getFile(); } /** * @return string * @throws FileException */ public function getFileType() { return $this->fileHandler->getFileType(); } /** * Leer los datos de un archivo subido a un array * * @throws FileException */ public function readFileToArray(): array { $this->autodetectEOL(); return $this->fileHandler->readToArray(); } /** * Activar la autodetección de fin de línea */ protected function autodetectEOL() { ini_set('auto_detect_line_endings', true); } /** * Leer los datos de un archivo subido a una cadena * * @throws FileException */ public function readFileToString(): string { $this->autodetectEOL(); return $this->fileHandler->readToString(); } /** * @return FileHandler */ public function getFileHandler(): FileHandler { return $this->fileHandler; } }