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