ExportDataHandler.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Archive;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Filesystem;
  11. use Magento\Framework\Filesystem\Directory\WriteInterface;
  12. /**
  13. * Class for the handling of a new data collection for MBI.
  14. */
  15. class ExportDataHandler implements ExportDataHandlerInterface
  16. {
  17. /**
  18. * Subdirectory path for all temporary files.
  19. *
  20. * @var string
  21. */
  22. private $subdirectoryPath = 'analytics/';
  23. /**
  24. * Filename of archive with collected data.
  25. *
  26. * @var string
  27. */
  28. private $archiveName = 'data.tgz';
  29. /**
  30. * @var Filesystem
  31. */
  32. private $filesystem;
  33. /**
  34. * @var Archive
  35. */
  36. private $archive;
  37. /**
  38. * Resource for write data of reports into separate files.
  39. *
  40. * @var ReportWriterInterface
  41. */
  42. private $reportWriter;
  43. /**
  44. * Resource for encrypting data.
  45. *
  46. * @var Cryptographer
  47. */
  48. private $cryptographer;
  49. /**
  50. * Resource for registration a new file.
  51. *
  52. * @var FileRecorder
  53. */
  54. private $fileRecorder;
  55. /**
  56. * @param Filesystem $filesystem
  57. * @param Archive $archive
  58. * @param ReportWriterInterface $reportWriter
  59. * @param Cryptographer $cryptographer
  60. * @param FileRecorder $fileRecorder
  61. */
  62. public function __construct(
  63. Filesystem $filesystem,
  64. Archive $archive,
  65. ReportWriterInterface $reportWriter,
  66. Cryptographer $cryptographer,
  67. FileRecorder $fileRecorder
  68. ) {
  69. $this->filesystem = $filesystem;
  70. $this->archive = $archive;
  71. $this->reportWriter = $reportWriter;
  72. $this->cryptographer = $cryptographer;
  73. $this->fileRecorder = $fileRecorder;
  74. }
  75. /**
  76. * @inheritdoc
  77. */
  78. public function prepareExportData()
  79. {
  80. try {
  81. $tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
  82. $this->prepareDirectory($tmpDirectory, $this->getTmpFilesDirRelativePath());
  83. $this->reportWriter->write($tmpDirectory, $this->getTmpFilesDirRelativePath());
  84. $tmpFilesDirectoryAbsolutePath = $this->validateSource($tmpDirectory, $this->getTmpFilesDirRelativePath());
  85. $archiveAbsolutePath = $this->prepareFileDirectory($tmpDirectory, $this->getArchiveRelativePath());
  86. $this->pack(
  87. $tmpFilesDirectoryAbsolutePath,
  88. $archiveAbsolutePath
  89. );
  90. $this->validateSource($tmpDirectory, $this->getArchiveRelativePath());
  91. $this->fileRecorder->recordNewFile(
  92. $this->cryptographer->encode($tmpDirectory->readFile($this->getArchiveRelativePath()))
  93. );
  94. } finally {
  95. $tmpDirectory->delete($this->getTmpFilesDirRelativePath());
  96. $tmpDirectory->delete($this->getArchiveRelativePath());
  97. }
  98. return true;
  99. }
  100. /**
  101. * Return relative path to a directory for temporary files with reports data.
  102. *
  103. * @return string
  104. */
  105. private function getTmpFilesDirRelativePath()
  106. {
  107. return $this->subdirectoryPath . 'tmp/';
  108. }
  109. /**
  110. * Return relative path to a directory for an archive.
  111. *
  112. * @return string
  113. */
  114. private function getArchiveRelativePath()
  115. {
  116. return $this->subdirectoryPath . $this->archiveName;
  117. }
  118. /**
  119. * Clean up a directory.
  120. *
  121. * @param WriteInterface $directory
  122. * @param string $path
  123. * @return string
  124. */
  125. private function prepareDirectory(WriteInterface $directory, $path)
  126. {
  127. $directory->delete($path);
  128. return $directory->getAbsolutePath($path);
  129. }
  130. /**
  131. * Remove a file and a create parent directory a file.
  132. *
  133. * @param WriteInterface $directory
  134. * @param string $path
  135. * @return string
  136. */
  137. private function prepareFileDirectory(WriteInterface $directory, $path)
  138. {
  139. $directory->delete($path);
  140. if (dirname($path) !== '.') {
  141. $directory->create(dirname($path));
  142. }
  143. return $directory->getAbsolutePath($path);
  144. }
  145. /**
  146. * Packing data into an archive.
  147. *
  148. * @param string $source
  149. * @param string $destination
  150. * @return bool
  151. */
  152. private function pack($source, $destination)
  153. {
  154. $this->archive->pack(
  155. $source,
  156. $destination,
  157. is_dir($source) ?: false
  158. );
  159. return true;
  160. }
  161. /**
  162. * Validate that data source exist.
  163. *
  164. * Return absolute path in a validated data source.
  165. *
  166. * @param WriteInterface $directory
  167. * @param string $path
  168. * @return string
  169. * @throws LocalizedException If source is not exist.
  170. */
  171. private function validateSource(WriteInterface $directory, $path)
  172. {
  173. if (!$directory->isExist($path)) {
  174. throw new LocalizedException(__('The "%1" source doesn\'t exist.', $directory->getAbsolutePath($path)));
  175. }
  176. return $directory->getAbsolutePath($path);
  177. }
  178. }