Report.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ImportExport\Helper;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\ImportExport\Model\Import;
  9. /**
  10. * ImportExport history reports helper
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Report extends \Magento\Framework\App\Helper\AbstractHelper
  16. {
  17. /**
  18. * @var \Magento\Framework\Stdlib\DateTime\Timezone
  19. */
  20. protected $timeZone;
  21. /**
  22. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  23. */
  24. protected $varDirectory;
  25. /**
  26. * Construct
  27. *
  28. * @param \Magento\Framework\App\Helper\Context $context
  29. * @param \Magento\Framework\Stdlib\DateTime\Timezone $timeZone
  30. * @param \Magento\Framework\Filesystem $filesystem
  31. */
  32. public function __construct(
  33. \Magento\Framework\App\Helper\Context $context,
  34. \Magento\Framework\Stdlib\DateTime\Timezone $timeZone,
  35. \Magento\Framework\Filesystem $filesystem
  36. ) {
  37. $this->timeZone = $timeZone;
  38. $this->varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  39. parent::__construct($context);
  40. }
  41. /**
  42. * Calculate import time
  43. *
  44. * @param string $time
  45. * @return string
  46. */
  47. public function getExecutionTime($time)
  48. {
  49. $reportTime = $this->timeZone->date($time);
  50. $timeDiff = $reportTime->diff($this->timeZone->date());
  51. return $timeDiff->format('%H:%I:%S');
  52. }
  53. /**
  54. * Get import summary
  55. *
  56. * @param \Magento\ImportExport\Model\Import $import
  57. * @return string
  58. */
  59. public function getSummaryStats(\Magento\ImportExport\Model\Import $import)
  60. {
  61. $message = __(
  62. 'Created: %1, Updated: %2, Deleted: %3',
  63. $import->getCreatedItemsCount(),
  64. $import->getUpdatedItemsCount(),
  65. $import->getDeletedItemsCount()
  66. );
  67. return $message;
  68. }
  69. /**
  70. * Checks imported file exists.
  71. *
  72. * @param string $filename
  73. * @return bool
  74. */
  75. public function importFileExists($filename)
  76. {
  77. return $this->varDirectory->isFile($this->getFilePath($filename));
  78. }
  79. /**
  80. * Get report file output
  81. *
  82. * @param string $filename
  83. * @return string
  84. */
  85. public function getReportOutput($filename)
  86. {
  87. return $this->varDirectory->readFile($this->getFilePath($filename));
  88. }
  89. /**
  90. * Get report absolute path.
  91. *
  92. * @param string $fileName
  93. * @return string
  94. */
  95. public function getReportAbsolutePath($fileName)
  96. {
  97. return $this->varDirectory->getAbsolutePath(Import::IMPORT_HISTORY_DIR . $fileName);
  98. }
  99. /**
  100. * Retrieve report file size
  101. *
  102. * @param string $filename
  103. * @return int|mixed
  104. */
  105. public function getReportSize($filename)
  106. {
  107. return $this->varDirectory->stat($this->getFilePath($filename))['size'];
  108. }
  109. /**
  110. * Get file path.
  111. *
  112. * @param string $filename
  113. * @return string
  114. * @throws \InvalidArgumentException
  115. */
  116. protected function getFilePath($filename)
  117. {
  118. if (preg_match('/\.\.(\\\|\/)/', $filename)) {
  119. throw new \InvalidArgumentException('Filename has not permitted symbols in it');
  120. }
  121. return $this->varDirectory->getRelativePath(Import::IMPORT_HISTORY_DIR . $filename);
  122. }
  123. /**
  124. * Get csv delimiter from request.
  125. *
  126. * @return string
  127. * @since 100.2.2
  128. */
  129. public function getDelimiter()
  130. {
  131. return $this->_request->getParam(Import::FIELD_FIELD_SEPARATOR, ',');
  132. }
  133. }