diff --git a/app/code/Magento/ImportExport/Controller/Adminhtml/Import/Download.php b/app/code/Magento/ImportExport/Controller/Adminhtml/Import/Download.php index 33dbba8320051..8910003191729 100644 --- a/app/code/Magento/ImportExport/Controller/Adminhtml/Import/Download.php +++ b/app/code/Magento/ImportExport/Controller/Adminhtml/Import/Download.php @@ -6,6 +6,7 @@ namespace Magento\ImportExport\Controller\Adminhtml\Import; use Magento\Framework\Component\ComponentRegistrar; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\ImportExport\Controller\Adminhtml\Import as ImportController; use Magento\Framework\App\Filesystem\DirectoryList; @@ -36,6 +37,11 @@ class Download extends ImportController */ protected $fileFactory; + /** + * @var \Magento\ImportExport\Model\Import\SampleFileProvider + */ + private $sampleFileProvider; + /** * Constructor * @@ -43,6 +49,7 @@ class Download extends ImportController * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory * @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory + * @param \Magento\ImportExport\Model\Import\SampleFileProvider $sampleFileProvider * @param ComponentRegistrar $componentRegistrar */ public function __construct( @@ -50,7 +57,8 @@ public function __construct( \Magento\Framework\App\Response\Http\FileFactory $fileFactory, \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory, - \Magento\Framework\Component\ComponentRegistrar $componentRegistrar + \Magento\Framework\Component\ComponentRegistrar $componentRegistrar, + \Magento\ImportExport\Model\Import\SampleFileProvider $sampleFileProvider = null ) { parent::__construct( $context @@ -59,6 +67,9 @@ public function __construct( $this->resultRawFactory = $resultRawFactory; $this->readFactory = $readFactory; $this->componentRegistrar = $componentRegistrar; + $this->sampleFileProvider = $sampleFileProvider + ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\ImportExport\Model\Import\SampleFileProvider::class); } /** @@ -68,13 +79,11 @@ public function __construct( */ public function execute() { - $fileName = $this->getRequest()->getParam('filename') . '.csv'; - $moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, self::SAMPLE_FILES_MODULE); - $fileAbsolutePath = $moduleDir . '/Files/Sample/' . $fileName; - $directoryRead = $this->readFactory->create($moduleDir); - $filePath = $directoryRead->getRelativePath($fileAbsolutePath); + $entityName = $this->getRequest()->getParam('filename'); - if (!$directoryRead->isFile($filePath)) { + try { + $fileContents = $this->sampleFileProvider->getFileContents($entityName); + } catch (NoSuchEntityException $e) { /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $this->messageManager->addError(__('There is no sample file for this entity.')); $resultRedirect = $this->resultRedirectFactory->create(); @@ -82,8 +91,8 @@ public function execute() return $resultRedirect; } - $fileSize = isset($directoryRead->stat($filePath)['size']) - ? $directoryRead->stat($filePath)['size'] : null; + $fileSize = $this->sampleFileProvider->getSize($entityName); + $fileName = $entityName . '.csv'; $this->fileFactory->create( $fileName, @@ -95,7 +104,7 @@ public function execute() /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */ $resultRaw = $this->resultRawFactory->create(); - $resultRaw->setContents($directoryRead->readFile($filePath)); + $resultRaw->setContents($fileContents); return $resultRaw; } } diff --git a/app/code/Magento/ImportExport/Model/Import/SampleFileProvider.php b/app/code/Magento/ImportExport/Model/Import/SampleFileProvider.php new file mode 100644 index 0000000000000..980bc110c969b --- /dev/null +++ b/app/code/Magento/ImportExport/Model/Import/SampleFileProvider.php @@ -0,0 +1,127 @@ + 'module_name'] + * @var array + */ + private $samples; + + /** + * @var \Magento\Framework\Component\ComponentRegistrar + */ + private $componentRegistrar; + + /** + * @var \Magento\Framework\Filesystem\Directory\ReadFactory + */ + private $readFactory; + + /** + * @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory + * @param ComponentRegistrar $componentRegistrar + * @param array $samples + */ + public function __construct( + \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory, + \Magento\Framework\Component\ComponentRegistrar $componentRegistrar, + array $samples = [] + ) { + $this->readFactory = $readFactory; + $this->componentRegistrar = $componentRegistrar; + $this->samples = $samples; + } + + /** + * Returns the Size for the given file associated to an Import entity + * + * @param string $fileName + * @throws NoSuchEntityException + * @return int + */ + public function getSize(string $entityName): int + { + $directoryRead = $this->getDirectoryRead($entityName); + $filePath = $this->getPath($entityName); + $fileSize = isset($directoryRead->stat($filePath)['size']) + ? $directoryRead->stat($filePath)['size'] : null; + + return $fileSize; + } + + /** + * Returns Content for the given file associated to an Import entity + * + * @param string $entityName + * @throws NoSuchEntityException + * @return string + */ + public function getFileContents(string $entityName): string + { + $directoryRead = $this->getDirectoryRead($entityName); + $filePath = $this->getPath($entityName); + + return $directoryRead->readFile($filePath); + } + + /** + * @param $entityName + * @return string $entityName + * @throws NoSuchEntityException + */ + private function getPath(string $entityName): string + { + $directoryRead = $this->getDirectoryRead($entityName); + $moduleName = $this->getModuleName($entityName); + $moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName); + $fileAbsolutePath = $moduleDir . '/Files/Sample/' . $entityName . '.csv'; + + $filePath = $directoryRead->getRelativePath($fileAbsolutePath); + + if (!$directoryRead->isFile($filePath)) { + throw new NoSuchEntityException(__("There is no file: %s", $filePath)); + } + + return $filePath; + } + + /** + * @param string $entityName + * @return ReadInterface + */ + private function getDirectoryRead(string $entityName): ReadInterface + { + $moduleName = $this->getModuleName($entityName); + $moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName); + $directoryRead = $this->readFactory->create($moduleDir); + + return $directoryRead; + } + + /** + * @param string $entityName + * @return string + * @throws NoSuchEntityException + */ + private function getModuleName(string $entityName): string + { + if (!isset($this->samples[$entityName])) { + throw new NoSuchEntityException(); + } + + return $this->samples[$entityName]; + } +} diff --git a/app/code/Magento/ImportExport/etc/di.xml b/app/code/Magento/ImportExport/etc/di.xml index 47acf7a356d93..36c76022a41c7 100644 --- a/app/code/Magento/ImportExport/etc/di.xml +++ b/app/code/Magento/ImportExport/etc/di.xml @@ -17,4 +17,16 @@ + + + + Magento_ImportExport + Magento_ImportExport + Magento_ImportExport + Magento_ImportExport + Magento_ImportExport + Magento_ImportExport + + + diff --git a/app/code/Magento/InventoryImportExport/Files/Sample/stock_sources.csv b/app/code/Magento/InventoryImportExport/Files/Sample/stock_sources.csv new file mode 100644 index 0000000000000..7d18527245461 --- /dev/null +++ b/app/code/Magento/InventoryImportExport/Files/Sample/stock_sources.csv @@ -0,0 +1,5 @@ +source_id,sku,status,quantity +1,sku1,1,10 +2,sku2,1,10 +3,sku3,1,10 +4,sku4,1,15 diff --git a/app/code/Magento/InventoryImportExport/etc/di.xml b/app/code/Magento/InventoryImportExport/etc/di.xml index fc773e6f5278b..3b3e0f89b91ec 100644 --- a/app/code/Magento/InventoryImportExport/etc/di.xml +++ b/app/code/Magento/InventoryImportExport/etc/di.xml @@ -44,4 +44,11 @@ + + + + Magento_InventoryImportExport + + +