FileRecorder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Filesystem;
  9. use Magento\Framework\Filesystem\Directory\WriteInterface;
  10. /**
  11. * Class for the handling of registration a new file for MBI.
  12. */
  13. class FileRecorder
  14. {
  15. /**
  16. * Resource for managing FileInfo object.
  17. *
  18. * @var FileInfoManager
  19. */
  20. private $fileInfoManager;
  21. /**
  22. * @var FileInfoFactory
  23. */
  24. private $fileInfoFactory;
  25. /**
  26. * Subdirectory path for an encoded file.
  27. *
  28. * @var string
  29. */
  30. private $fileSubdirectoryPath = 'analytics/';
  31. /**
  32. * File name of an encoded file.
  33. *
  34. * @var string
  35. */
  36. private $encodedFileName = 'data.tgz';
  37. /**
  38. * @var Filesystem
  39. */
  40. private $filesystem;
  41. /**
  42. * @param FileInfoManager $fileInfoManager
  43. * @param FileInfoFactory $fileInfoFactory
  44. * @param Filesystem $filesystem
  45. */
  46. public function __construct(
  47. FileInfoManager $fileInfoManager,
  48. FileInfoFactory $fileInfoFactory,
  49. Filesystem $filesystem
  50. ) {
  51. $this->fileInfoManager = $fileInfoManager;
  52. $this->fileInfoFactory = $fileInfoFactory;
  53. $this->filesystem = $filesystem;
  54. }
  55. /**
  56. * Save new encrypted file, register it and remove old registered file.
  57. *
  58. * @param EncodedContext $encodedContext
  59. * @return bool
  60. */
  61. public function recordNewFile(EncodedContext $encodedContext)
  62. {
  63. $directory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  64. $fileRelativePath = $this->getFileRelativePath();
  65. $directory->writeFile($fileRelativePath, $encodedContext->getContent());
  66. $fileInfo = $this->fileInfoManager->load();
  67. $this->registerFile($encodedContext, $fileRelativePath);
  68. $this->removeOldFile($fileInfo, $directory);
  69. return true;
  70. }
  71. /**
  72. * Return relative path to encoded file.
  73. *
  74. * @return string
  75. */
  76. private function getFileRelativePath()
  77. {
  78. return $this->fileSubdirectoryPath . hash('sha256', time())
  79. . '/' . $this->encodedFileName;
  80. }
  81. /**
  82. * Register encoded file.
  83. *
  84. * @param EncodedContext $encodedContext
  85. * @param string $fileRelativePath
  86. * @return bool
  87. */
  88. private function registerFile(EncodedContext $encodedContext, $fileRelativePath)
  89. {
  90. $newFileInfo = $this->fileInfoFactory->create(
  91. [
  92. 'path' => $fileRelativePath,
  93. 'initializationVector' => $encodedContext->getInitializationVector(),
  94. ]
  95. );
  96. $this->fileInfoManager->save($newFileInfo);
  97. return true;
  98. }
  99. /**
  100. * Remove previously registered file.
  101. *
  102. * @param FileInfo $fileInfo
  103. * @param WriteInterface $directory
  104. * @return bool
  105. */
  106. private function removeOldFile(FileInfo $fileInfo, WriteInterface $directory)
  107. {
  108. if (!$fileInfo->getPath()) {
  109. return true;
  110. }
  111. $directory->delete($fileInfo->getPath());
  112. $directoryName = dirname($fileInfo->getPath());
  113. if ($directoryName !== '.') {
  114. $directory->delete($directoryName);
  115. }
  116. return true;
  117. }
  118. }