LogEntryExport.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Exception\FileSystemException;
  9. use Magento\Framework\Exception\NotFoundException;
  10. use Magento\Framework\Filesystem;
  11. use Magento\Framework\Filesystem\Directory\WriteInterface;
  12. use Magento\Framework\Stdlib\DateTime;
  13. use Vertex\Tax\Api\Data\LogEntryInterface;
  14. /**
  15. * Write LogEntryInterface data to flat file data.
  16. */
  17. class LogEntryExport
  18. {
  19. /** Open a file and empty its contents if they exists */
  20. const MODE_CLEAN_FILE = 'w';
  21. /** Open a file and append its contents if they exist */
  22. const MODE_APPEND_FILE = 'a';
  23. /** @var DateTime */
  24. private $dateTime;
  25. /** @var WriteInterface */
  26. private $directoryWrite;
  27. /** @var string */
  28. private $file;
  29. /** @var \Magento\Framework\Filesystem\File\WriteInterface */
  30. private $stream;
  31. /**
  32. * @param Filesystem $fileSystem
  33. * @param DateTime $dateTime
  34. * @throws FileSystemException
  35. */
  36. public function __construct(
  37. Filesystem $fileSystem,
  38. DateTime $dateTime
  39. ) {
  40. $this->directoryWrite = $fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  41. $this->dateTime = $dateTime;
  42. }
  43. /**
  44. * Close the export file and return its final path.
  45. *
  46. * @return string
  47. */
  48. public function close()
  49. {
  50. $path = $this->file;
  51. $this->stream->close();
  52. $this->file = '';
  53. return $path;
  54. }
  55. /**
  56. * Open a new export file for writing.
  57. *
  58. * @param string|null $filename
  59. * @param string $mode One of {@see LogEntryArchive::MODE_APPEND_FILE}, {@see LogEntryArchive::MODE_CLEAN_FILE}
  60. * @return void
  61. * @throws FileSystemException
  62. */
  63. public function open($filename = null, $mode = self::MODE_APPEND_FILE)
  64. {
  65. if (!empty($this->file)) {
  66. return;
  67. }
  68. if ($filename === null) {
  69. $filename = $this->getFilename();
  70. }
  71. $this->file = rtrim($this->getBasePath(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
  72. $this->directoryWrite->touch(
  73. $this->directoryWrite->getRelativePath($this->file)
  74. );
  75. $this->stream = $this->directoryWrite->openFile($this->file, $mode);
  76. }
  77. /**
  78. * Write the given log entry record to the current target file.
  79. *
  80. * @param LogEntryInterface|\Vertex\Tax\Model\Data\LogEntry $record
  81. * @return void
  82. * @throws NotFoundException
  83. * @throws FileSystemException
  84. */
  85. public function write(LogEntryInterface $record)
  86. {
  87. if (empty($this->file)) {
  88. throw new NotFoundException(__('Cannot write log entry because no export file is open.'));
  89. }
  90. $this->stream->writeCsv(
  91. [
  92. $record->getType(),
  93. $record->getDate(),
  94. $record->getSubTotal(),
  95. $record->getTotalTax(),
  96. $record->getTotal(),
  97. $record->getRequestXml(),
  98. $record->getResponseXml()
  99. ]
  100. );
  101. }
  102. /**
  103. * Write the header to the current target file
  104. *
  105. * @return void
  106. * @throws FileSystemException
  107. * @throws NotFoundException
  108. */
  109. public function writeHeader()
  110. {
  111. if (empty($this->file)) {
  112. throw new NotFoundException(__('Cannot write log entry because no export file is open.'));
  113. }
  114. $this->stream->writeCsv(
  115. [
  116. 'Request Type',
  117. 'Request Date',
  118. 'Subtotal',
  119. 'Total Tax',
  120. 'Total',
  121. 'Request XML',
  122. 'Response XML'
  123. ]
  124. );
  125. }
  126. /**
  127. * Get the log entry base storage path.
  128. *
  129. * @return string
  130. */
  131. private function getBasePath()
  132. {
  133. return $this->directoryWrite->getAbsolutePath();
  134. }
  135. /**
  136. * Generate a log entry filename.
  137. *
  138. * @return string
  139. */
  140. private function getFilename()
  141. {
  142. return sprintf('vertexlog_%s.csv', $this->dateTime->formatDate(time(), false));
  143. }
  144. }