SampleFileProvider.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\ImportExport\Model\Import;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\Component\ComponentRegistrar;
  10. use Magento\Framework\Filesystem\Directory\ReadInterface;
  11. /**
  12. * Import Sample File Provider model.
  13. * This class support only *.csv.
  14. */
  15. class SampleFileProvider
  16. {
  17. /**
  18. * Associate an import entity to its module, e.g ['entity_name' => 'module_name']
  19. * @var array
  20. */
  21. private $samples;
  22. /**
  23. * @var \Magento\Framework\Component\ComponentRegistrar
  24. */
  25. private $componentRegistrar;
  26. /**
  27. * @var \Magento\Framework\Filesystem\Directory\ReadFactory
  28. */
  29. private $readFactory;
  30. /**
  31. * @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory
  32. * @param ComponentRegistrar $componentRegistrar
  33. * @param array $samples
  34. */
  35. public function __construct(
  36. \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
  37. \Magento\Framework\Component\ComponentRegistrar $componentRegistrar,
  38. array $samples = []
  39. ) {
  40. $this->readFactory = $readFactory;
  41. $this->componentRegistrar = $componentRegistrar;
  42. $this->samples = $samples;
  43. }
  44. /**
  45. * Returns the Size for the given file associated to an Import entity
  46. *
  47. * @param string $entityName
  48. * @throws NoSuchEntityException
  49. * @return int|null
  50. */
  51. public function getSize(string $entityName)
  52. {
  53. $directoryRead = $this->getDirectoryRead($entityName);
  54. $filePath = $this->getPath($entityName);
  55. $fileSize = isset($directoryRead->stat($filePath)['size'])
  56. ? $directoryRead->stat($filePath)['size'] : null;
  57. return $fileSize;
  58. }
  59. /**
  60. * Returns Content for the given file associated to an Import entity
  61. *
  62. * @param string $entityName
  63. * @throws NoSuchEntityException
  64. * @return string
  65. */
  66. public function getFileContents(string $entityName): string
  67. {
  68. $directoryRead = $this->getDirectoryRead($entityName);
  69. $filePath = $this->getPath($entityName);
  70. return $directoryRead->readFile($filePath);
  71. }
  72. /**
  73. * @return string $entityName
  74. * @throws NoSuchEntityException
  75. */
  76. private function getPath(string $entityName): string
  77. {
  78. $moduleName = $this->getModuleName($entityName);
  79. $directoryRead = $this->getDirectoryRead($entityName);
  80. $moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
  81. $fileAbsolutePath = $moduleDir . '/Files/Sample/' . $entityName . '.csv';
  82. $filePath = $directoryRead->getRelativePath($fileAbsolutePath);
  83. if (!$directoryRead->isFile($filePath)) {
  84. throw new NoSuchEntityException(__("There is no file: %file", ['file' => $filePath]));
  85. }
  86. return $filePath;
  87. }
  88. /**
  89. * @param string $entityName
  90. * @return ReadInterface
  91. */
  92. private function getDirectoryRead(string $entityName): ReadInterface
  93. {
  94. $moduleName = $this->getModuleName($entityName);
  95. $moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
  96. $directoryRead = $this->readFactory->create($moduleDir);
  97. return $directoryRead;
  98. }
  99. /**
  100. * @param string $entityName
  101. * @return string
  102. * @throws NoSuchEntityException
  103. */
  104. private function getModuleName(string $entityName): string
  105. {
  106. if (!isset($this->samples[$entityName])) {
  107. throw new NoSuchEntityException();
  108. }
  109. return $this->samples[$entityName];
  110. }
  111. }