RequestLogger.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Stdlib\DateTime\DateTime;
  8. use Vertex\Tax\Api\Data\LogEntryInterface;
  9. use Vertex\Tax\Api\Data\LogEntryInterfaceFactory;
  10. use Vertex\Tax\Api\LogEntryRepositoryInterface;
  11. /**
  12. * Performs all the actions necessary for logging a request
  13. */
  14. class RequestLogger
  15. {
  16. /** @var DateTime */
  17. private $dateTime;
  18. /** @var DomDocumentFactory */
  19. private $documentFactory;
  20. /** @var LogEntryInterfaceFactory */
  21. private $factory;
  22. /** @var LogEntryRepositoryInterface */
  23. private $repository;
  24. /**
  25. * @param LogEntryRepositoryInterface $repository
  26. * @param LogEntryInterfaceFactory $logEntryFactory
  27. * @param DateTime $dateTime
  28. * @param DomDocumentFactory $documentFactory
  29. */
  30. public function __construct(
  31. LogEntryRepositoryInterface $repository,
  32. LogEntryInterfaceFactory $logEntryFactory,
  33. DateTime $dateTime,
  34. DomDocumentFactory $documentFactory
  35. ) {
  36. $this->repository = $repository;
  37. $this->factory = $logEntryFactory;
  38. $this->dateTime = $dateTime;
  39. $this->documentFactory = $documentFactory;
  40. }
  41. /**
  42. * Log a Request
  43. *
  44. * @param string $type
  45. * @param string $requestXml
  46. * @param string $responseXml
  47. * @return void
  48. * @throws \Magento\Framework\Exception\CouldNotSaveException
  49. */
  50. public function log($type, $requestXml, $responseXml)
  51. {
  52. /** @var LogEntryInterface $logEntry */
  53. $logEntry = $this->factory->create();
  54. $timestamp = $this->dateTime->date('Y-m-d H:i:s');
  55. $logEntry->setType($type);
  56. $logEntry->setDate($timestamp);
  57. $requestXml = $this->formatXml($requestXml);
  58. $responseXml = $this->formatXml($responseXml);
  59. $logEntry->setRequestXml($requestXml);
  60. $logEntry->setResponseXml($responseXml);
  61. $this->addResponseDataToLogEntry($logEntry, $responseXml);
  62. $this->repository->save($logEntry);
  63. }
  64. /**
  65. * Add data from the response XML to the LogEntry
  66. *
  67. * @param LogEntryInterface $logEntry
  68. * @param string $responseXml
  69. * @return LogEntryInterface
  70. */
  71. private function addResponseDataToLogEntry(LogEntryInterface $logEntry, $responseXml)
  72. {
  73. $dom = $this->documentFactory->create();
  74. if (!empty($responseXml)) {
  75. $dom->loadXML($responseXml);
  76. $totalTaxNodes = $dom->getElementsByTagName('TotalTax');
  77. $totalTaxNode = null;
  78. for ($i = 0; $i < $totalTaxNodes->length; ++$i) {
  79. if ($totalTaxNodes->item($i)->parentNode->localName === 'QuotationResponse') {
  80. $totalTaxNode = $totalTaxNodes->item($i);
  81. break;
  82. }
  83. }
  84. $totalNode = $dom->getElementsByTagName('Total');
  85. $subtotalNode = $dom->getElementsByTagName('SubTotal');
  86. $lookupResultNode = $dom->getElementsByTagName('Status');
  87. $addressLookupFaultNode = $dom->getElementsByTagName('exceptionType');
  88. $total = $totalNode->length > 0 ? $totalNode->item(0)->nodeValue : 0;
  89. $subtotal = $subtotalNode->length > 0 ? $subtotalNode->item(0)->nodeValue : 0;
  90. $totalTax = $totalTaxNode !== null ? $totalTaxNode->nodeValue : 0;
  91. $lookupResult = '';
  92. if ($lookupResultNode->length > 0) {
  93. $lookupResult = $lookupResultNode->item(0)->getAttribute('lookupResult');
  94. } elseif ($addressLookupFaultNode->length > 0) {
  95. $lookupResult = $addressLookupFaultNode->item(0)->nodeValue;
  96. }
  97. $logEntry->setTotalTax($totalTax);
  98. $logEntry->setTotal($total);
  99. $logEntry->setSubTotal($subtotal);
  100. $logEntry->setLookupResult($lookupResult);
  101. }
  102. return $logEntry;
  103. }
  104. /**
  105. * Format a string of XML
  106. *
  107. * @param string $xml
  108. * @return string
  109. */
  110. private function formatXml($xml)
  111. {
  112. if (empty($xml)) {
  113. return '';
  114. }
  115. $dom = $this->documentFactory->create();
  116. $dom->preserveWhiteSpace = false;
  117. $dom->loadXML($xml);
  118. $dom->formatOutput = true;
  119. return $dom->saveXML();
  120. }
  121. }